我找豆包做了一个python的简易PVZ
2026-10-04 13:42:19
发布于:广东
由于帖主贪玩PVZ被罚,所以使用豆包做了一个简易PVZ。记得先下载pygame。
代码:
import pygame
import random
pygame.init()
WIDTH, HEIGHT = 800, 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Simple Plants vs Zombies - Grid & Sun System")
clock = pygame.time.Clock()
# Colors
WHITE = (255, 255, 255)
GREEN = (0, 200, 0)
GRASS_GREEN = (80, 180, 60)
DARK_GRASS = (50, 140, 30)
BROWN = (100, 70, 40)
RED = (200, 0, 0)
YELLOW = (255, 220, 0)
BLACK = (0, 0, 0)
# Grid settings
GRID_COLS = 9
GRID_ROWS = 5
CELL_W = 80
CELL_H = 80
GRID_OFFSET_X = 40
GRID_OFFSET_Y = 40
# Sun Class
class Sun:
def __init__(self, x, y):
self.x = x
self.y = y
self.vy = 1.5
self.max_y = y + 60
self.alive = True
def update(self):
if self.y < self.max_y:
self.y += self.vy
def draw(self):
pygame.draw.circle(screen, YELLOW, (int(self.x), int(self.y)), 12)
# Plant Class
class Plant:
def __init__(self, grid_x, grid_y, ptype):
self.grid_x = grid_x
self.grid_y = grid_y
self.x = GRID_OFFSET_X + grid_x * CELL_W + CELL_W // 2
self.y = GRID_OFFSET_Y + grid_y * CELL_H + CELL_H // 2
self.type = ptype
self.hp = 100
self.cd = 0
def update(self):
if self.cd > 0:
self.cd -= 1
# Sunflower produces sun
if self.type == "sunflower" and self.cd <= 0:
sun = Sun(self.x, self.y)
suns.append(sun)
self.cd = 150
def draw(self):
if self.type == "sunflower":
pygame.draw.circle(screen, YELLOW, (self.x, self.y), 22)
elif self.type == "pea":
pygame.draw.rect(screen, GREEN, (self.x-20, self.y-20, 40, 40))
# Bullet Class
class Bullet:
def __init__(self, x, y):
self.x = x
self.y = y
self.speed = 6
def update(self):
self.x += self.speed
def draw(self):
pygame.draw.circle(screen, BLACK, (self.x, self.y), 6)
# Zombie Class
class Zombie:
def __init__(self, grid_y):
self.grid_y = grid_y
self.x = WIDTH
self.y = GRID_OFFSET_Y + grid_y * CELL_H + CELL_H // 2
self.hp = 80
self.speed = 0.4 # CHANGED: from 0.8 to 0.4, slower zombie
def update(self):
self.x -= self.speed
def draw(self):
pygame.draw.rect(screen, BROWN, (self.x-20, self.y-25, 40, 50))
plants = []
bullets = []
zombies = []
suns = []
spawn_timer = 0
game_over = False
sun_count = 50
select_plant = "sunflower"
# Check if a grid cell already has plant
def has_plant(gx, gy):
for p in plants:
if p.grid_x == gx and p.grid_y == gy:
return True
return False
running = True
while running:
screen.fill(WHITE)
# Draw lawn grid
for col in range(GRID_COLS):
for row in range(GRID_ROWS):
rect_x = GRID_OFFSET_X + col * CELL_W
rect_y = GRID_OFFSET_Y + row * CELL_H
if (col + row) % 2 == 0:
pygame.draw.rect(screen, GRASS_GREEN, (rect_x, rect_y, CELL_W, CELL_H))
else:
pygame.draw.rect(screen, DARK_GRASS, (rect_x, rect_y, CELL_W, CELL_H))
pygame.draw.rect(screen, BLACK, (rect_x, rect_y, CELL_W, CELL_H), 1)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
mx, my = pygame.mouse.get_pos()
# Click to collect sun
for s in suns:
if s.alive:
dist = ((mx - s.x)**2 + (my - s.y)**2)**0.5
if dist < 15:
sun_count += 25
s.alive = False
# Calculate grid position from mouse
gx = (mx - GRID_OFFSET_X) // CELL_W
gy = (my - GRID_OFFSET_Y) // CELL_H
# Check inside grid bounds
if 0 <= gx < GRID_COLS and 0 <= gy < GRID_ROWS:
if not has_plant(gx, gy):
# Sunflower = 50 sun, Peashooter = 100 sun
if select_plant == "sunflower" and sun_count >= 50:
plants.append(Plant(gx, gy, "sunflower"))
sun_count -= 50
elif select_plant == "pea" and sun_count >= 100:
plants.append(Plant(gx, gy, "pea"))
sun_count -= 100
# Switch plant type
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
select_plant = "sunflower"
elif event.key == pygame.K_2:
select_plant = "pea"
if not game_over:
# Spawn zombie
spawn_timer += 1
if spawn_timer > 120:
spawn_timer = 0
ry = random.randint(0, GRID_ROWS - 1)
zombies.append(Zombie(ry))
# Update plants
for p in plants:
p.update()
if p.type == "pea" and p.cd <= 0:
bullets.append(Bullet(p.x + 20, p.y))
p.cd = 40
# Update bullets
for b in bullets[:]:
b.update()
if b.x > WIDTH:
bullets.remove(b)
# Update zombies
for z in zombies[:]:
z.update()
if z.x < GRID_OFFSET_X:
game_over = True
# Bullet hit zombie
for b in bullets[:]:
hit = False
for z in zombies[:]:
if abs(b.x - z.x) < 25 and abs(b.y - z.y) < 30:
z.hp -= 15
bullets.remove(b)
hit = True
if z.hp <= 0:
zombies.remove(z)
break
if hit:
break
# Update suns
for s in suns[:]:
s.update()
if not s.alive:
suns.remove(s)
# Draw game objects
for p in plants:
p.draw()
for b in bullets:
b.draw()
for z in zombies:
z.draw()
for s in suns:
if s.alive:
s.draw()
# UI Text
font = pygame.font.SysFont(None, 36)
sun_text = font.render(f"Sun: {sun_count}", True, BLACK)
screen.blit(sun_text, (10, 10))
tip_text = font.render("Press 1: Sunflower(50 sun) | Press 2: Peashooter(100 sun)", True, BLACK)
screen.blit(tip_text, (10, 45))
if game_over:
font_big = pygame.font.SysFont(None, 60)
text = font_big.render("GAME OVER", True, RED)
screen.blit(text, (WIDTH//2 - 120, HEIGHT//2))
pygame.display.flip()
clock.tick(60)
pygame.quit()
全部评论 2
- 置顶
注:建模没有做导致植物和僵尸都是方块
2天前 来自 广东
0点个赞吧,QwQ求求了
2天前 来自 广东
0义父们点个赞吧
2天前 来自 广东
0我会优化的
2天前 来自 广东
0
卧槽我昨天玩pvz被罚了
2天前 来自 浙江
0原来同是天涯沦落人啊
昨天 来自 广东
0YES
20小时前 来自 浙江
0你喜欢哪个版本
18小时前 来自 广东
0

























有帮助,赞一个