import pygame from pygame.locals import * import sys from Bullet import Bullet from Peashooter import Peashooter from Sun import Sun from SunFlower import SunFlower from WallNut import WallNut # 初始化pygame from Zombie import Zombie pygame.init() size = (1200, 600) # 设置屏幕宽高 screen = pygame.display.set_mode(size) # 设置屏幕标题 pygame.display.set_caption("植物大战僵尸") backgroundImg = pygame.image.load('material/images/background1.jpg').convert_alpha() sunbackImg = pygame.image.load('material/images/SunBack.png').convert_alpha() text = '1000' myfont = pygame.font.SysFont('arial', 30) txtImg = myfont.render(text, True, (0, 0, 0)) peashooter = Peashooter() sunFlower = SunFlower() wallNut = WallNut() zombie = Zombie() spriteList = pygame.sprite.Group() spriteList.add(peashooter) spriteList.add(sunFlower) spriteList.add(wallNut) spriteList.add(zombie) sunList = pygame.sprite.Group() #定义生成太阳的事件 GENERATORSUNEVNET = pygame.USEREVENT + 1 pygame.time.set_timer(GENERATORSUNEVNET, 2000) index = 0 clock = pygame.time.Clock() while True: if index > 100: index = 0 clock.tick(20) # 启动消息队列,获取消息并处理 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == GENERATORSUNEVNET: sun = Sun(sunFlower.rect) sunList.add(sun) # print(suns.__len__()) if event.type == pygame.MOUSEBUTTONDOWN: pressed_array = pygame.mouse.get_pressed() print(pressed_array) if pressed_array[0]: pos = pygame.mouse.get_pos() print(pos) for sun in sunList: if sun.rect.collidepoint(pos): sunList.remove(sun) text = str(int(text) + 50) myfont = pygame.font.SysFont('arial', 30) txtImg = myfont.render(text, True, (0, 0, 0)) screen.blit(backgroundImg, (0, 0)) screen.blit(sunbackImg, (250, 30)) screen.blit(txtImg, (300, 33)) if index % 30 == 0: bul = Bullet(peashooter.rect, size) spriteList.add(bul) spriteList.update(index) spriteList.draw(screen) sunList.update(index) sunList.draw(screen) index += 1 pygame.display.update()