这是不是你的童年~"/>
python还原游戏之植物大战僵尸,来看看这是不是你的童年~
导语:
哈喽,哈喽~植物大战僵尸的人气可谓是经久不衰,晃着脑袋生产阳光的向日葵,突突突⚾⚾⚾吐着子弹的豌豆射手!行动迟缓种类丰富的僵尸……印象最深的是“僵尸吃掉了你的脑子!”还有疯狂的戴夫,无一不唤醒着我们的童年记忆。下面用python还原你的记忆中的童年!
功能实现如下:
-
支持的植物类型:太阳花,豌豆射手,坚果。
-
支持的僵尸类型:普通僵尸,棋子僵尸,路障僵尸,铁桶僵尸。
-
使用json文件保存关卡信息,设置僵尸出现的时间和位置。
-
增加每关开始时选择上场植物。
植物大战僵尸
对源代码,python感兴趣的小伙伴能点击这行字体哦!
一、配置图片地址
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()
二、向日葵类
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 = Pea
更多推荐
python还原游戏之植物大战僵尸,来看看这是不是你的童年~
发布评论