极简版植物大战僵尸(待修改)

查看代码
import pygame
import random
import sys

# 初始化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)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# 游戏参数
clock = pygame.time.Clock()
FPS = 60
sun_count = 50
game_font = pygame.font.SysFont('Arial', 24)

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, self.y), 20)
    
    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 = 60  # 1秒冷却
        return Projectile(self.x, self.y)

class Projectile:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.speed = 5
        self.active = True
    
    def update(self):
        self.x += self.speed
        if self.x > SCREEN_WIDTH:
            self.active = False
    
    def draw(self):
        pygame.draw.circle(screen, GREEN, (self.x, self.y), 5)

class Zombie:
    def __init__(self, row):
        self.x = SCREEN_WIDTH
        self.y = 100 + row * 100
        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 - 20, self.y - 40, 40, 80))
    
    def take_damage(self, damage):
        self.health -= damage
        return self.health <= 0

class Sun:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.speed = 2
        self.target_y = y + random.randint(50, 150)
        self.collected = False
        self.value = 25
    
    def update(self):
        if self.y < self.target_y:
            self.y += self.speed
    
    def draw(self):
        pygame.draw.circle(screen, (255, 255, 0), (self.x, self.y), 20)

def main():
    global sun_count
    
    plants = []
    zombies = []
    projectiles = []
    suns = []
    
    zombie_spawn_timer = 0
    sun_spawn_timer = 0
    
    rows = 5
    cols = 9
    grid = [[None for _ in range(cols)] for _ in range(rows)]
    cell_width = 80
    cell_height = 100
    
    running = True
    while running:
        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                
                # 收集阳光
                for sun in suns:
                    if (sun.x - mouse_x)**2 + (sun.y - mouse_y)**2 <= 400 and not sun.collected:
                        sun_count += sun.value
                        sun.collected = True
                
                # 放置植物
                col = mouse_x // cell_width
                row = mouse_y // cell_height
                
                if 0 <= row < rows and 0 <= col < cols:
                    if grid[row][col] is None and sun_count >= 100:
                        grid[row][col] = Plant(col * cell_width + cell_width // 2, 
                                             row * cell_height + cell_height // 2)
                        plants.append(grid[row][col])
                        sun_count -= 100
        
        # 生成僵尸
        zombie_spawn_timer += 1
        if zombie_spawn_timer >= 300:  # 每5秒生成一个僵尸
            zombie_spawn_timer = 0
            row = random.randint(0, rows - 1)
            zombies.append(Zombie(row))
        
        # 生成阳光
        sun_spawn_timer += 1
        if sun_spawn_timer >= 200:  # 每约3秒生成一个阳光
            sun_spawn_timer = 0
            suns.append(Sun(random.randint(50, SCREEN_WIDTH - 50), 0))
        
        # 更新植物
        for plant in plants:
            plant.update()
            if plant.can_attack():
                projectiles.append(plant.attack())
        
        # 更新僵尸
        for zombie in zombies[:]:
            zombie.update()
            
            # 检查僵尸是否到达左边界
            if zombie.x <= 0:
                running = False
                print("游戏结束! 僵尸吃掉了你的脑子!")
        
        # 更新投射物
        for projectile in projectiles[:]:
            projectile.update()
            if not projectile.active:
                projectiles.remove(projectile)
                continue
            
            # 检查投射物与僵尸的碰撞
            for zombie in zombies[:]:
                if (zombie.x - projectile.x)**2 + (zombie.y - projectile.y)**2 <= 1600:  # 40px半径内
                    if zombie.take_damage(20):
                        zombies.remove(zombie)
                    projectile.active = False
                    break
        
        # 更新阳光
        for sun in suns[:]:
            sun.update()
            if sun.collected:
                suns.remove(sun)
        
        # 绘制
        screen.fill(BLACK)
        
        # 绘制草坪网格
        for row in range(rows):
            for col in range(cols):
                pygame.draw.rect(screen, (0, 100, 0) if (row + col) % 2 == 0 else (0, 120, 0), 
                                (col * cell_width, row * cell_height, cell_width, cell_height), 1)
        
        # 绘制植物
        for plant in plants:
            plant.draw()
        
        # 绘制僵尸
        for zombie in zombies:
            zombie.draw()
        
        # 绘制投射物
        for projectile in projectiles:
            projectile.draw()
        
        # 绘制阳光
        for sun in suns:
            sun.draw()
        
        # 显示阳光数量
        sun_text = game_font.render(f"阳光: {sun_count}", True, WHITE)
        screen.blit(sun_text, (10, 10))
        
        pygame.display.flip()
        clock.tick(FPS)
    
    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()

 

posted @ 2025-06-22 17:05  与尔5  阅读(24)  评论(0)    收藏  举报