上次写了一个俄罗斯方块,感觉好像大家都看懂了,这次就更新一个植物大战僵尸吧
植物大战僵尸的话
引入需要的模块
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, )else:print(LOG)
向日葵类
class Sunflower(Plant):def __init__(self, x, y):super(Sunflower, self).__init__()self.image = pygame.image.load('imgs/sunflower.png') = _rect() = = yself.price = 50self.hp = 100# 5 时间计数器self.time_count = 0
新增功能:生成阳光
def produce_money(self):self.time_count += 1if self.time_count == += 5self.time_count = 0
向日葵加入到窗口中
def display_sunflower(self):MainGame.window.blit(self.image, )
豌豆射手类
class PeaShooter(Plant):def __init__(self, x, y):super(PeaShooter, self).__init__()# self.image 为一个 surfaceself.image = pygame.image.load('imgs/peashooter.png') = _rect() = = yself.price = 50self.hp = 200# 6 发射计数器self.shot_count = 0
增加射击方法
def shot(self):# 6 记录是否应该射击should_fire = Falsefor zombie bie_list: == and zombie
本文发布于:2024-02-02 22:53:26,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170688560446983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |