植物大战僵尸

import pygame
import random
import sys

====================== 学号标记(请替换为你的学号后四位) ======================

STUDENT_ID_SUFFIX = "0141"
print(f"程序已启动,作者学号后四位:{STUDENT_ID_SUFFIX}")

===========================================================================

初始化

pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("植物大战僵尸 - 基础版")
clock = pygame.time.Clock()

颜色定义

WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BROWN = (139, 69, 19)
RED = (255, 0, 0)

游戏元素类

class Plant:
def init(self, x, y):
self.x = x
self.y = y
self.width = 40
self.height = 60
self.shoot_timer = 0
self.shoot_interval = 60 # 每60帧发射一次

def draw(self, surface):
pygame.draw.rect(surface, GREEN, (self.x, self.y, self.width, self.height))

def update(self):
self.shoot_timer += 1
if self.shoot_timer >= self.shoot_interval:
self.shoot_timer = 0
return Bullet(self.x + self.width, self.y + self.height//2)
return None

class Zombie:
def init(self, y):
self.x = WIDTH
self.y = y
self.width = 40
self.height = 60
self.speed = 1
self.health = 3

def draw(self, surface):
pygame.draw.rect(surface, BROWN, (self.x, self.y, self.width, self.height))

def update(self):
self.x -= self.speed

class Bullet:
def init(self, x, y):
self.x = x
self.y = y
self.radius = 5
self.speed = 5

def draw(self, surface):
pygame.draw.circle(surface, RED, (int(self.x), int(self.y)), self.radius)

def update(self):
self.x += self.speed

def main():
plants = []
zombies = []
bullets = []
zombie_spawn_timer = 0
zombie_spawn_interval = 120 # 每120帧生成一个僵尸

running = True
while running:
screen.fill(WHITE)

for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 鼠标点击放置植物
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
mx, my = pygame.mouse.get_pos()
# 对齐网格
grid_y = (my // 80) * 80 + 10
if 0 <= grid_y < HEIGHT:
plants.append(Plant(100, grid_y))

更新和生成僵尸

zombie_spawn_timer += 1
if zombie_spawn_timer >= zombie_spawn_interval:
zombie_spawn_timer = 0
lane = random.randint(0, 4) * 80 + 10
zombies.append(Zombie(lane))

更新植物和子弹

new_bullets = []
for plant in plants:
bullet = plant.update()
if bullet:
bullets.append(bullet)
plant.draw(screen)

更新子弹

bullets_to_remove = []
for bullet in bullets:
bullet.update()
if bullet.x > WIDTH:
bullets_to_remove.append(bullet)
else:
bullet.draw(screen)

更新僵尸

zombies_to_remove = []
for zombie in zombies:
zombie.update()
# 检测是否到达左侧(游戏结束条件)
if zombie.x < 0:
print("僵尸突破防线,游戏结束!")
running = False
# 子弹与僵尸碰撞检测
for bullet in bullets:
if (zombie.x < bullet.x < zombie.x + zombie.width and
zombie.y < bullet.y < zombie.y + zombie.height):
zombie.health -= 1
bullets_to_remove.append(bullet)
if zombie.health <= 0:
zombies_to_remove.append(zombie)
zombie.draw(screen)

移除需要删除的对象

bullets = [b for b in bullets if b not in bullets_to_remove]
zombies = [z for z in zombies if z not in zombies_to_remove]

pygame.display.flip()
clock.tick(60)

pygame.quit()
sys.exit()

if name == "main":
main()
屏幕截图 2026-07-02 194428

posted @ 2026-07-02 19:46  user6666666999999  阅读(2)  评论(0)    收藏  举报