前言
大家早好、午好、晚好吖 ❤ ~欢迎光临本文章
晃着脑袋生产阳光的向日葵,突突突吐着子弹的豌豆射手!
行动迟缓种类丰富的僵尸……
印象最深的是“僵尸吃掉了你的脑子!”
还有疯狂的戴夫,无一不唤醒着我们的童年记忆
山民们闯到哪一关了?解锁了哪些植物?
在今天,就让我们来上一个大工程,制作植物大战僵尸小游戏里的冒险模式~
(有一个简单版和复刻版嘚~)
软件安装
-
Python 3.8 / 编译器
-
Pycharm 2021.2版本 / 代码编辑器
简单版
游戏素材
代码展示
引入需要的模块
import pygame
import random
配置图片地址
IMAGE_PATH = 'imgs/'
设置页面宽高
scrrr_width = 800
scrrr_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 = x
self.rect.y = y
self.price = 50
self.hp = 100
时间计数器
self.time_count = 0
新增功能:生成阳光
def produce_money(self):
self.time_count += 1
if self.time_count == 25:
MainGame.money += 5
self.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 为一个 surface
self.image = pygame.image.load('imgs/peashooter.png')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.price = 50
self.hp = 200
发射计数器
self.shot_count = 0
增加射击方法
def shot(self):
记录是否应该射击
should_fire = False
for 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 = True
self.image = pygame.image.load('imgs/peabullet.png')
self.damage = 50
self.speed = 10
self.rect = self.image.get_rect()
self.rect.x = peashooter.rect.x + 60
self.rect.y = peashooter.rect.y + 15
def move_bullet(self):
在屏幕范围内,实现往右移动
if self.rect.x < scrrr_width:
self.rect.x += self.speed
else:
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.damage
if zombie.hp <= 0:
zombie.live = False
self.nextLevel()
闯关方法
def nextLevel(self):
MainGame.score += 20
MainGame.remnant_score -= 20
for i in range(1, 100):
if MainGame.score == 100 * i and MainGame.remnant_score == 0:
MainGame.remnant_score = 100 * i
MainGame.shaoguan += 1
MainGame.produce_zombie += 50
def display_peabullet(self):
MainGame.window.blit(self.image, self.rect)
僵尸类
class Zombie(pygame.sprite.Sprite):
def __init__(self, x, y):