导语:
哈喽,哈喽~最近杂交版植物大战僵尸的人气可谓是爆涨,晃着脑袋生产阳光的向日葵,突突突⚾⚾⚾吐着子弹的豌豆射手!魔幻的玩法,奇怪的杂交植物,和姿势滑稽的僵尸。玩一局下来真的是紧张又刺激!……印象最深的是“僵尸吃掉了你的脑子!”还有疯狂的戴夫,无一不唤醒着我们的童年记忆。下面用python还原你的记忆中的童年!
功能实现如下:
-
支持的植物类型:太阳花,豌豆射手,坚果。
-
支持的僵尸类型:普通僵尸,棋子僵尸,路障僵尸,铁桶僵尸。
-
使用json文件保存关卡信息,设置僵尸出现的时间和位置。
-
增加每关开始时选择上场植物。
一、配置图片地址
import pygame
from pygame.locals import *
import sys
import os
# 初始化
pygame.init()
# 背景大小设置
bg_size = (1200, 600)
# 设置屏幕背景大小
screen = pygame.display.set_mode(bg_size)
#设置屏幕标题
pygame.display.set_caption("植物大战僵尸")
# 设置图片路径
# rootpath = os.getcwd()
# imgpath = os.path.join(rootpath,background_path)
background_path = "material/images/background1.jpg"
sun_path = "material/images/SunBack.png"
# 加载背景图片
backgrounImg = pygame.image.load(background_path).convert()
sunImg = pygame.image.load(sun_path).convert()
# 设置文本
myfont = pygame.font.SysFont("arial",20)
txtImg = myfont.render("50",True,(255,255,0))
while True:
# 启动消息队列,获取消息并处理
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
# 绘制背景
screen.blit(backgrounImg, (0, 0))
# 绘制顶部太阳数量栏
screen.blit(sunImg, (250, 0))
screen.blit(txtImg,(320,6))
pygame.display.update()
#python学习资料,视频教程,书籍和代码,已经打包好啦。需要的话戳这里。https://www.bilibili.com/opus/946875516670967812?spm_id_from=333.999.0.0
二、向日葵类
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)
#python学习资料,视频教程,书籍和代码,已经打包好啦。需要的话戳这里。https://www.bilibili.com/opus/946875516670967812?spm_id_from=333.999.0.0
三、豌豆射手类
# 豌豆射手类
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:
#