植物大战僵尸
import random
import sys
from pygame.locals import *
初始化pygame
pygame.init()
游戏窗口设置
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('植物大战僵尸简易版')
颜色定义
GREEN = (0, 255, 0)
BROWN = (139, 69, 19)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
游戏参数
FPS = 60
clock = pygame.time.Clock()
草地网格
GRID_SIZE = 80
GRID_WIDTH = 9
GRID_HEIGHT = 5
LAWN_LEFT = 100
LAWN_TOP = 100
植物类
class Plant:
def init(self, x, y):
self.x = x
self.y = y
self.health = 100
self.attack_cooldown = 0
def draw(self):
pygame.draw.circle(screen, GREEN, (self.x + GRID_SIZE//2, self.y + GRID_SIZE//2), GRID_SIZE//2 - 10)
def update(self):
if self.attack_cooldown > 0:
self.attack_cooldown -= 1
def can_attack(self):
return self.attack_cooldown == 0
def attack(self):
self.attack_cooldown = 30
return Projectile(self.x + GRID_SIZE, self.y + GRID_SIZE//2)
子弹类
class Projectile:
def init(self, x, y):
self.x = x
self.y = y
self.speed = 5
self.damage = 20
def update(self):
self.x += self.speed
def draw(self):
pygame.draw.circle(screen, BLUE, (self.x, self.y), 10)
def is_off_screen(self):
return self.x > SCREEN_WIDTH
def collides_with(self, zombie):
return (self.x > zombie.x and self.x < zombie.x + GRID_SIZE and
self.y > zombie.y and self.y < zombie.y + GRID_SIZE)
僵尸类
class Zombie:
def init(self, row):
self.x = SCREEN_WIDTH
self.y = LAWN_TOP + row * GRID_SIZE
self.speed = 1
self.health = 100
self.row = row
def update(self):
self.x -= self.speed
def draw(self):
pygame.draw.rect(screen, BROWN, (self.x, self.y, GRID_SIZE-10, GRID_SIZE-10))
def is_dead(self):
return self.health <= 0
def reached_house(self):
return self.x < LAWN_LEFT
游戏状态
plants = []
zombies = []
projectiles = []
sun = 50 # 阳光资源
selected_plant = None
game_over = False
zombie_spawn_timer = 0
绘制草地网格
def draw_lawn():
for row in range(GRID_HEIGHT):
for col in range(GRID_WIDTH):
rect = pygame.Rect(LAWN_LEFT + col * GRID_SIZE,
LAWN_TOP + row * GRID_SIZE,
GRID_SIZE, GRID_SIZE)
pygame.draw.rect(screen, GREEN, rect, 1)
绘制阳光数量
def draw_sun():
font = pygame.font.Font(None, 36)
text = font.render(f"阳光: {sun}", True, BLACK)
screen.blit(text, (20, 20))
绘制植物选择菜单
def draw_plant_menu():
pygame.draw.rect(screen, GREEN, (20, 60, 60, 60))
font = pygame.font.Font(None, 24)
text = font.render("lastnumber :21", True, BLACK)
screen.blit(text, (30, 80))
text = font.render("25", True, BLACK)
screen.blit(text, (30, 100))
游戏主循环
while not game_over:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
# 检查是否点击了植物菜单
if 20 <= mouse_x <= 80 and 60 <= mouse_y <= 120:
selected_plant = "peashooter"
# 检查是否在草地上点击
if (LAWN_LEFT <= mouse_x <= LAWN_LEFT + GRID_WIDTH * GRID_SIZE and
LAWN_TOP <= mouse_y <= LAWN_TOP + GRID_HEIGHT * GRID_SIZE):
if selected_plant == "peashooter" and sun >= 25:
col = (mouse_x - LAWN_LEFT) // GRID_SIZE
row = (mouse_y - LAWN_TOP) // GRID_SIZE
# 检查该位置是否已有植物
plant_exists = False
for plant in plants:
if plant.x == LAWN_LEFT + col * GRID_SIZE and plant.y == LAWN_TOP + row * GRID_SIZE:
plant_exists = True
break
if not plant_exists:
plants.append(Plant(LAWN_LEFT + col * GRID_SIZE, LAWN_TOP + row * GRID_SIZE))
sun -= 25
selected_plant = None
# 生成僵尸
zombie_spawn_timer += 1
if zombie_spawn_timer >= 180: # 每3秒生成一个僵尸
zombie_spawn_timer = 0
row = random.randint(0, GRID_HEIGHT - 1)
zombies.append(Zombie(row))
# 更新植物
for plant in plants:
plant.update()
if plant.can_attack():
projectiles.append(plant.attack())
# 更新子弹
for projectile in projectiles[:]:
projectile.update()
if projectile.is_off_screen():
projectiles.remove(projectile)
continue
for zombie in zombies[:]:
if projectile.collides_with(zombie):
zombie.health -= projectile.damage
if projectile in projectiles:
projectiles.remove(projectile)
if zombie.is_dead():
zombies.remove(zombie)
break
# 更新僵尸
for zombie in zombies[:]:
zombie.update()
if zombie.reached_house():
game_over = True
break
# 随机增加阳光
if random.random() < 0.01:
sun += 5
# 绘制
screen.fill(WHITE)
draw_lawn()
draw_sun()
draw_plant_menu()
for plant in plants:
plant.draw()
for projectile in projectiles:
projectile.draw()
for zombie in zombies:
zombie.draw()
if selected_plant:
mouse_x, mouse_y = pygame.mouse.get_pos()
pygame.draw.circle(screen, GREEN, (mouse_x, mouse_y), GRID_SIZE//2 - 10, 2)
if game_over:
font = pygame.font.Font(None, 72)
text = font.render("游戏结束!", True, (255, 0, 0))
screen.blit(text, (SCREEN_WIDTH//2 - 150, SCREEN_HEIGHT//2 - 36))
pygame.display.flip()
clock.tick(FPS)
pygame.quit()

浙公网安备 33010602011771号