600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 今天咱们用Python整一个植物大战僵尸游戏 | 附带源码

今天咱们用Python整一个植物大战僵尸游戏 | 附带源码

时间:2022-07-27 06:27:09

相关推荐

今天咱们用Python整一个植物大战僵尸游戏 | 附带源码

《植物大战僵尸》是一款极富策略性的小游戏,可怕的僵尸即将入侵,唯一的防御方式就是栽种植物。此游戏集成了即时战略、塔防御战和卡片收集等要素、游戏的内容就是:玩家控制植物,抵御僵尸的进攻,保护这片草坪。

那么咱们今天自己来整一个植物大战僵尸小游戏!

相关文件

想学Python的小伙伴可以关注小编的Python源码、问题解答&学习交流群:733089476

有很多的资源可以白嫖的哈,需要源码的小伙伴可以在+君羊领取

环境搭建

Python版本:3.7.8

安装Python并添加到环境变量,pip安装需要的相关模块即可。

效果展示

代码实现

引入需要的模块

import pygameimport random

配置图片地址

IMAGE_PATH = 'imgs/'

设置页面宽高

scrrr_width=800scrrr_height =560

创建控制游戏结束的状态

GAMEOVER = False

图片加载报错处理

LOG = '文件:{}中的方法:{}出错'.format(__file__,__name__)

创建地图类

class Map():# 存储两张不同颜色的图片名称map_names_list = [IMAGE_PATH + 'map1.png', IMAGE_PATH + 'map2.png']# 初始化地图def __init__(self, x, y, img_index):self.image = pygame.image.load(Map.map_names_list[img_index])self.position = (x, y)# 是否能够种植self.can_grow = True# 加载地图def load_map(self):MainGame.window.blit(self.image,self.position)

植物类

class Plant(pygame.sprite.Sprite):def __init__(self):super(Plant, self).__init__()self.live=True# 加载图片def load_image(self):if hasattr(self, 'image') and hasattr(self, 'rect'):MainGame.window.blit(self.image, self.rect)else:print(LOG)

向日葵类

class Sunflower(Plant):def __init__(self,x,y):super(Sunflower, self).__init__()self.image = pygame.image.load('imgs/sunflower.png')self.rect = self.image.get_rect()self.rect.x = xself.rect.y = yself.price = 50self.hp = 100# 时间计数器self.time_count = 0# 新增功能:生成阳光def produce_money(self):self.time_count += 1if self.time_count == 25:MainGame.money += 5self.time_count = 0# 向日葵加入到窗口中def display_sunflower(self):MainGame.window.blit(self.image,self.rect)

豌豆射手类

class PeaShooter(Plant):def __init__(self,x,y):super(PeaShooter, self).__init__()# self.image 为一个 surfaceself.image = pygame.image.load('imgs/peashooter.png')self.rect = self.image.get_rect()self.rect.x = xself.rect.y = yself.price = 50self.hp = 200# 发射计数器self.shot_count = 0# 增加射击方法def shot(self):# 记录是否应该射击should_fire = Falsefor zombie in MainGame.zombie_list:if zombie.rect.y == self.rect.y and zombie.rect.x < 800 and zombie.rect.x > self.rect.x:should_fire = True# 如果活着if self.live and should_fire:self.shot_count += 1# 计数器到25发射一次if self.shot_count == 25:# 基于当前豌豆射手的位置,创建子弹peabullet = PeaBullet(self)# 将子弹存储到子弹列表中MainGame.peabullet_list.append(peabullet)self.shot_count = 0# 将豌豆射手加入到窗口中的方法def display_peashooter(self):MainGame.window.blit(self.image,self.rect)

豌豆子弹类

class PeaBullet(pygame.sprite.Sprite):def __init__(self,peashooter):self.live = Trueself.image = pygame.image.load('imgs/peabullet.png')self.damage = 50self.speed = 10self.rect = self.image.get_rect()self.rect.x = peashooter.rect.x + 60self.rect.y = peashooter.rect.y + 15def move_bullet(self):# 在屏幕范围内,实现往右移动if self.rect.x < scrrr_width:self.rect.x += self.speedelse:self.live = False# 新增,子弹与僵尸的碰撞def hit_zombie(self):for zombie in MainGame.zombie_list:if pygame.sprite.collide_rect(self,zombie):#打中僵尸之后,修改子弹的状态,self.live = False#僵尸掉血zombie.hp -= self.damageif zombie.hp <= 0:zombie.live = Falseself.nextLevel()

僵尸类

class Zombie(pygame.sprite.Sprite):def __init__(self,x,y):super(Zombie, self).__init__()self.image = pygame.image.load('imgs/zombie.png')self.rect = self.image.get_rect()self.rect.x = xself.rect.y = yself.hp = 1000self.damage = 2self.speed = 1self.live = Trueself.stop = False# 僵尸的移动def move_zombie(self):if self.live and not self.stop:self.rect.x -= self.speedif self.rect.x < -80:#8 调用游戏结束方法MainGame().gameOver()# 判断僵尸是否碰撞到植物,如果碰撞,调用攻击植物的方法def hit_plant(self):for plant in MainGame.plants_list:if pygame.sprite.collide_rect(self,plant):# 僵尸移动状态的修改self.stop = Trueself.eat_plant(plant)

代码可能有点长,就不全部展示出来啦

需要源码的小伙伴可以+扣君羊

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。