二话不说,直接上代码。
import pygame
import random
import sys
#1 配置图片地址
#IMAGE_PATH = 'grassland.png'
#1 设置页面宽高
scrrr_width=800
scrrr_height =560
#1 创建控制游戏结束的状态
GAMEOVER = False
#4 图片加载报错处理
LOG = '文件:{}中的方法:{}出错'.format(__file__,__name__)
#3 创建地图类
class Map():
#3 存储两张不同颜色的图片名称
map_names_list = ['**************', '*******************']
#3 初始化地图
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
#3 加载地图
def load_map(self):
MainGame.window.blit(self.image,self.position)
#4 植物类
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)
#5 向日葵类
class Sunflower(Plant):
def __init__(self,x,y):
super(Sunflower, self).__init__()
self.image = pygame.image.load('**************')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.price = 50
self.hp = 100
#5 时间计数器
self.time_count = 0
#5 新增功能:生成阳光
def produce_money(self):
self.time_count += 1
if self.time_count == 25:
MainGame.money += 10
self.time_count = 0
#5 向日葵加入到窗口中
def display_sunflower(self):
MainGame.window.blit(self.image,self.rect)
#6 豌豆射手类
class PeaShooter(Plant):
def __init__(self,x,y):
super(PeaShooter, self).__init__()
# self.image 为一个 surface
self.image = pygame.image.load('**************')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.price = 50
self.hp = 200
#6 发射计数器
self.shot_count = 0
#6 增加射击方法
def shot(self):
#6 记录是否应该射击
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 =