赞
踩
1:定义一个游戏管理类,先创建一个窗体 self.screen = pygame.display.set_mode((1400,600))。里面是个元组
2.设置题目:pygame.display.set_caption("植物大战僵尸")
3.定义 while True 保证循环不结束
4.最后记得添加实例。有类就要有实例
5.运行出来窗体之后,定义一个背景布
6.将背景布赋给窗体(图形,位置) self.screen.blit(self.bg_surface,(0,0))
7.绘制出来
8.定义一个精灵类
9.显示对话,定义一个动画组
10.写一个射手类
11.多个射手,定义射手组
12.将射手组绘制到屏幕,调用updata
13.定义射手1,将射手添加到射手组
14.有射手之后,对射手的帧频进行修改 self. clock = pygame.time.Clock() self.clock.tick(10)
15.定义一个计时器发射子弹
16.定义一个子弹群
17.定义一个子弹精灵类将子弹动起来
18.定义一个僵尸类,以动画移动类作为父类
19.将僵尸类draw到屏幕上
20.将子弹和僵尸碰撞
- import sys
-
- import pygame
-
- # 8.定义一个精灵类
- class BaseSprite(pygame.sprite.Sprite):
- def __init__(self,name):
- super().__init__()
- self.image = pygame.image.load(name)
- self.rect = self.image.get_rect()
- # 9.显示动画,定义一个动画组
- class AnimateSprite(BaseSprite):
- def __init__(self,default_name ,names):
- super().__init__(default_name)
- # 写动起来
- self.images = [pygame.image.load(name) for name in names] # 列表推导式
- self.current = 0
-
- def update(self):
- self.current += 1
- if self.current == len(self.images) * 2:
- self.current = 0
- self.image = self.images[self.current//2] ##
-
-
- # 10.写一个射手类
- class PeaShooter(AnimateSprite):
- def __init__(self,default_name,names,center,gm):
- super().__init__(default_name,names)
- self.rect.center = center
- self.time_count = 2
- self.gm =gm
-
- # 16.定义一个子弹群
- self.bullet_group = pygame.sprite.Group()
- print("当前子弹个数",len(self.bullet_group.sprites()))
-
- # 15.定义一个计时器发射子弹
- def update(self):
- super().update()
- self.time_count -= 0.1
- if self.time_count <= 0:
- self.time_count = 2
- # 将豌豆子弹加入子弹组
- BulletSprite("bullet.png",(self.rect.right,self.rect.top + 15)).add(self.bullet_group)
- # 子弹群绘制,更新
- self.bullet_group.draw(self.gm.screen)
- self.bullet_group.update()
- # 18.定义一个僵尸类,以动画移动类作为父类
- class Enemy(AnimateSprite):
- def __init__(self,default_name,names,center,gm):
- super().__init__(default_name,names)
- self.rect.center = center
- self.time_count = 2
- self.gm =gm
-
- def update(self):
- super().update()
- self.rect.left -= 10
-
-
-
-
- class BulletSprite(BaseSprite):
- def __init__(self,name,center):
- super().__init__(name)
- self.rect.center = center
-
- def update(self):
- self.rect.left += 10
- if self.rect.left > 1400:
- self.kill()
-
- class GameManage:
- def __init__(self):
- self.screen = pygame.display.set_mode((1400,600))# 1:定义一个游戏管理类,先创建一个窗体 self.screen = pygame.display.set_mode((1400,600))。里面是个元组
- pygame.display.set_caption("植物大战僵尸")# 2.设置题目:pygame.display.set_caption("植物大战僵尸")
- # 14.对射手的帧频进行修改
- self. clock = pygame.time.Clock()
-
-
- self.bg_surface = pygame.image.load("background1.jpg")# 5.运行出来窗体之后,定义一个背景布
-
- # 11.多个射手,定义射手组
- self.pea_shooter_group = pygame.sprite.Group()
-
- # 13.定义射手1射手2
- self.pea1 = PeaShooter("plants/peashooter/idle/idle_00.png",
- [f"plants/peashooter/idle/idle_0{i}.png" for i in range(8)],(300,300),self )
- self.pea2 = PeaShooter("plants/peashooter/idle/idle_00.png",
- [f"plants/peashooter/idle/idle_0{i}.png" for i in range(8)], (300, 200), self)
-
- # 13.定义射手1射手2,将射手添加到射手组
- self.pea1.add(self.pea_shooter_group)
- self.pea2.add(self.pea_shooter_group)
-
- # 定义僵尸组
- self.enemy_group = pygame.sprite.Group()
- # # 18.定义一个僵尸类,以动画移动类作为父类
-
- images = [f"zombies/run/run_0{i}.png" for i in range(10)]
- for i in range(10,31):
- images.append(f"zombies/run/run_{i}.png")
- self.enemy1 = Enemy("zombies/run/run_00.png",images,(1200,300),self)
- self.enemy1.add(self.enemy_group)
- self.enemy2 = Enemy("zombies/run/run_00.png", images, (1200, 200), self)
- self.enemy2.add(self.enemy_group)
- self.enemy3 = Enemy("zombies/run/run_00.png", images, (1200, 500), self)
- self.enemy3.add(self.enemy_group)
- self.enemy4 = Enemy("zombies/run/run_00.png", images, (1100, 400), self)
- self.enemy4.add(self.enemy_group)
-
-
- def run(self):
- # 3.定义 while True 保证循环不结束
- while True:
- # 14对射手的帧频进行修改
- self.clock.tick(20)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
-
- self.screen.blit(self.bg_surface, (0, 0)) # 6.将背景布赋给窗体(图形,位置)
-
- # 12.将射手组绘制到屏幕,调用updata
- self.pea_shooter_group.draw(self.screen)
- self.pea_shooter_group.update()
-
- # 19.将僵尸类draw到屏幕上
- self.enemy_group.draw(self.screen)
- self.enemy_group.update()
- # 20.将子弹和僵尸碰撞
- for pea_sprite in self.pea_shooter_group:
- if pygame.sprite.groupcollide(pea_sprite.bullet_group,self.enemy_group,True,True):
- print("打死了")
-
-
- pygame.display.flip() # 7.绘制出来
- # 4.最后记得添加实例。有类就要有实例
- gm = GameManage()
- gm.run()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。