You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.7 KiB

import os
import time
import pygame
pygame.init()
# 设置游戏背景尺寸,所有的资源图片都是基于这个尺寸制作的,不建议修改
background_size = (820, 560)
# 创建游戏窗口并设置标题
screen = pygame.display.set_mode(background_size)
pygame.display.set_caption("植物大战僵尸")
# 获取当前工作目录
base_path = os.getcwd()
# 加载背景图片
bg_img_obj = pygame.image.load(os.path.join(base_path, 'images/a3.png')).convert_alpha()
# 加载植物图片
sunFlowerImg = pygame.image.load(os.path.join(base_path, 'images/SunFlower/SunFlower_00.png')).convert_alpha()
wallNutImg = pygame.image.load(os.path.join(base_path, 'images/WallNut/wall_nut_00.png')).convert_alpha()
peaShooterImg = pygame.image.load(os.path.join(base_path, 'images/Peashooter/Peashooter00.png')).convert_alpha()
jxcImg = pygame.image.load(os.path.join(base_path, 'images/jxc/JXC00.png')).convert_alpha()
# 加载阳光储蓄罐和种子图片
sun_back_img = pygame.image.load(os.path.join(base_path, 'images/SeedBank01.png')).convert_alpha()
sunflower_seed = pygame.image.load(os.path.join(base_path, 'images/SunFlower_kp.png'))
wall_nut_seed = pygame.image.load(os.path.join(base_path, 'images/Wallnut_kp.png'))
peashooter_seed = pygame.image.load(os.path.join(base_path, 'images/Peashooter_kp.png'))
jxc_seed = pygame.image.load(os.path.join(base_path, 'images/jxc_kp.png'))
# 初始化阳光值为100
text = "1000"
# 设置阳光值字体和颜色
sun_font = pygame.font.SysFont("黑体", 25)
sun_num_surface = sun_font.render(str(text), True, (0, 0, 0))
# 创建植物组、子弹组、僵尸组和阳光组
spriteGroup = pygame.sprite.Group()
bulletGroup = pygame.sprite.Group()
zombieGroup = pygame.sprite.Group()
sun_sprite = pygame.sprite.Group()
# 定义游戏时钟和特殊事件
clock = pygame.time.Clock()
GEN_SUN_EVENT = pygame.USEREVENT + 1 # 生成阳光事件
pygame.time.set_timer(GEN_SUN_EVENT, 2000) # 每2秒生成一次阳光
GEN_BULLET_EVENT = pygame.USEREVENT + 2 # 生成子弹事件
pygame.time.set_timer(GEN_BULLET_EVENT, 2000) # 每2秒生成一次子弹
GEN_ZOMBIE_EVENT = pygame.USEREVENT + 3 # 生成僵尸事件
pygame.time.set_timer(GEN_ZOMBIE_EVENT, 10000) # 每10秒生成一次僵尸
GEN_SUN2_EVENT = pygame.USEREVENT + 4 # 生成双倍阳光事件
pygame.time.set_timer(GEN_SUN2_EVENT, 20000) # 每20秒生成一次双倍阳光
# 初始化选择的植物类型和僵尸数量
choose = 0
zombie_num = 0
def main():
"""
游戏主函数,包含游戏主循环
"""
global zombie_num # 僵尸数量全局变量
global choose # 选择的植物类型全局变量
global text # 阳光值全局变量
global sun_num_surface # 阳光值显示表面全局变量
running = True # 游戏是否运行标志
index = 0 # 用于植物、子弹和僵尸的更新和绘制的索引
while running:
# 控制游戏帧率
clock.tick(20)
# 在屏幕上绘制背景、阳光储蓄罐、阳光值和种子图片
screen.blit(bg_img_obj, (0, 0))
screen.blit(sun_back_img, (20, 0.5))
screen.blit(sun_num_surface, (35, 50))
screen.blit(sunflower_seed, (80, 5))
screen.blit(peashooter_seed, (121, 5))
screen.blit(wall_nut_seed, (162, 5))
screen.blit(jxc_seed, (203, 5))
# 更新和绘制植物、子弹、僵尸和阳光
spriteGroup.update(index)
spriteGroup.draw(screen)
bulletGroup.update(index)
bulletGroup.draw(screen)
zombieGroup.update(index)
zombieGroup.draw(screen)
sun_sprite.update(index)
sun_sprite.draw(screen)
# 更新游戏界面
pygame.display.update()
if __name__ == '__main__':
main()