import pygame
import time
import random
import sys
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 560
# 地图类 v1.2 完善地图类
class Map:
images_list = ['imgs/map1.png','imgs/map2.png']
def __init__(self, x, y, image_index):
self.position = (x,y)
self.image = pygame.image.load(Map.images_list[image_index])
# v1.5 新增是否能种植的属性
self.can_grow = True
# 将当前地图的图片加入到窗口
def display_map(self):
MainGame.window.blit(self.image,self.position)
# 植物类 (父类)
class Plant(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.live = True
# 向日葵类 v1.4 完善向日葵类
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
# v1.6 时间计数器
self.time_count = 0
# v1.6 新增功能:生成阳光
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)
# 豌豆射手类
# v1.5 完善 豌豆射手类
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
# v1.7 发射计数器
self.shot_count = 0
# v1.7 增加射击方法
def shot(self):
# v1.9 记录是否应该射击
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):
Main
Python 小作品-植物大战僵尸
最新推荐文章于 2024-06-20 17:44:19 发布
import pygameimport timeimport randomimport sysSCREEN_WIDTH = 800SCREEN_HEIGHT = 560# 地图类 v1.2 完善地图类class Map: images_list = ['imgs/map1.png','imgs/map2.png'] def __init__(self, x, y,...
摘要由CSDN通过智能技术生成