backgd_size = (1200, 600) screen = pygame.display.set_mode(backgd_size) pygame.display.set_caption('plant_vs_zoomie') bg_img_path = 'material/images/background1.jpg' bg_img_obj = pygame.image.load(bg_img_path).convert_alpha() sunbank_img_path = 'material/images/SunBack.png' sunbank_img_obj = pygame.image.load(sunbank_img_path).convert_alpha() text = '900' sun_font = pygame.font.SysFont('arial', 25) sun_num_surface = sun_font.render(text, True, (0, 0, 0)) peashooter = Peashooter() sunflower = SunFlower() wallnut = WallNut() sunList = [] clock = pygame.time.Clock() def main(): running = True index = 0 while running: if index >= 130: index = 0
def main(): global sunnum, font, fontImg # peashooter = Peashooter() clickimage = [] # sunflower = SunFlower() wallnut = WallNut() p1 = [] peaList = [] sunFlowerList = [] sunList = [] zombieList = [] zombie = Zombie() zombieList.append(zombie) enemy_zombie_list = [] flower_product_list = [] # FPS block = pygame.time.Clock() index = 0 # 是否点击卡片 is_pick = False # 区域种植判断 is_plant = False # 太阳下落时间 SUN_EVENT = pygame.USEREVENT + 1 pygame.time.set_timer(SUN_EVENT, 3000) # 3秒出现一个 # 太阳花产能 FLOWER_PRODUCT_SUM_EVENT = pygame.USEREVENT + 1 pygame.time.set_timer(FLOWER_PRODUCT_SUM_EVENT, 5000) # ZOMBIE_EVENT = pygame.USEREVENT + 1 pygame.time.set_timer(ZOMBIE_EVENT, 5000) z = Zone() bulletList = [] PAUSE = False FINAL = 0 while 1: pygame.mixer.music.play() # 播放音乐 block.tick(25) # FPS为25 # 鼠标点击 press = pygame.mouse.get_pressed() # 鼠标坐标 x, y = pygame.mouse.get_pos() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == SUN_EVENT: sun = Sun() sunList.append(sun) # 太阳花产能 if event.type == FLOWER_PRODUCT_SUM_EVENT: for flower in sunFlowerList: if not flower.isProductSum: sun = Sun() sun.rect.left = flower.rect.left sun.rect.top = flower.rect.top sun.belong = flower flower_product_list.append(sun) flower.isProductSum = True # 无法生产 if event.type == ZOMBIE_EVENT: zombie = Zombie() zombieList.append(zombie) # 暂停,点击ESC if event.type == pygame.KEYDOWN: if event.key == 27: PAUSE = not PAUSE if event.type == pygame.MOUSEMOTION: if 537 <= x <= 749 and 378 <= y <= 481: if press[0]: PAUSE = not PAUSE if not is_pick: if press[0]: if 330 <= x <= 330 + card.get_rect().width and 7 <= y <= 7 + card.get_rect().height: peashooter = Peashooter() clickimage.append(peashooter) pick_type = 'pea' is_pick = True if 380 <= x <= 380 + card_1.get_rect().width and 7 <= y <= 7 + card_1.get_rect().height: sunflower = SunFlower() clickimage.append(sunflower) pick_type = 'flower' is_pick = True else: if z.is_plant_zone(x, y): if z.getIndex(x, y) and not is_plant: if pick_type == 'pea': p = Peashooter() a, b = z.getIndex(x, y) p.zone = z.getGridPos(a, b) if not z.plantInfo[b][a]: p1.append(p) is_plant = True if press[0]: peaList.append(p) enemy_zombie_list.append(p) # bullet = p.shot() # bulletList.append(bullet) clickimage.clear() p1.clear() is_pick = False z.plantInfo[b][a] = 1 sunnum = str(int(sunnum) - 100) fontImg = font.render(sunnum, True, (0, 0, 0)) # print(z.plantInfo[a][b]) elif pick_type == 'flower': f = SunFlower() a, b = z.getIndex(x, y) f.zone = z.getGridPos(a, b) if not z.plantInfo[b][a]: p1.append(f) is_plant = True if press[0]: sunFlowerList.append(f) enemy_zombie_list.append(f) clickimage.clear() p1.clear() is_pick = False z.plantInfo[b][a] = 1 sunnum = str(int(sunnum) - 100) fontImg = font.render(sunnum, True, (0, 0, 0)) else: p1.clear() is_plant = False if press[2]: is_pick = False clickimage.clear() p1.clear() is_plant = False # if 330 <= x <= 405 and 180 <= y <= 274: # else: # p1.clear() for sun in sunList: if sun.rect.collidepoint((x, y)): if press[0]: # 点击太阳消失 sunList.remove(sun) # 收集加分 # global sunnum, font, fontImg sunnum = str(int(sunnum) + 25) fontImg = font.render(sunnum, True, (0, 0, 0)) for sun in flower_product_list: if sun.rect.collidepoint((x, y)): if press[0]: # 点击太阳消失 flower_product_list.remove(sun) # 收集加分 sunnum = str(int(sunnum) + 25) fontImg = font.render(sunnum, True, (0, 0, 0)) # 收集后可继续生产 sun.belong.isProductSum = False # 打印图片 if int(zombie.rect.left) < 200: FINAL += 1 PAUSE = True if not PAUSE: screen.blit(backgroud, (0, 0)) screen.blit(card_slot, (250, 0)) screen.blit(card, (330, 7)) screen.blit(card_1, (380, 7)) screen.blit(card_2, (430, 6)) screen.blit(fontImg, (275, 60)) if index > 13: index = 0 # screen.blit(peashooter.images[index % 13], peashooter.rect) # screen.blit(sunflower.images[index % 18], sunflower.rect) # screen.blit(wallnut.images[index % 16], wallnut.rect) for image in clickimage: screen.blit(image.images[0], (x, y)) for p in p1: screen.blit(p.images[0], p.zone) # 豌豆射手 for pea in peaList: if pea.is_live: if index % 99 == 1: bullet = pea.shot() bulletList.append(bullet) screen.blit(pea.images[index % 13], pea.zone) else: peaList.remove(pea) enemy_zombie_list.remove(pea) # 太阳花 for sunFlower in sunFlowerList: if sunFlower.is_live: screen.blit(sunFlower.images[index % 18], sunFlower.zone) else: sunFlowerList.remove(sunFlower) enemy_zombie_list.remove(sunFlower) # 阳光 for sun in sunList: screen.blit(sun.images[index % 22], sun.rect) sun.down() # 产能 for sun in flower_product_list: screen.blit(sun.images[index % 22], sun.rect) for bullet in bulletList: if bullet.status: screen.blit(bullet.images, bullet.rect) bullet.move() bullet.hit(zombieList) else: bulletList.remove(bullet) for zombie in zombieList: if zombie.is_live: zombie.changimage() screen.blit(zombie.images[index % 20], zombie.rect) zombie.move() zombie.attack(enemy_zombie_list) else: zombieList.remove(zombie) pygame.display.update() index += 1 else: if FINAL > 0: screen.blit(final_draw, (350, 70)) else: screen.blit(pause_draw, (450, 100)) pygame.display.update()
def main(): global text,choose global sun_num_surface running = True index = 0 while running: # if index >= 130: # index = 0 clock.tick(20) #2s产生一个太阳花 # if index % 40 == 0: # sun = Sun(sunflower.rect) # sunList.add(sun) #3s产生一个子弹 # if index % 30 == 0: # for sprite in spriteGroup: # if isinstance(sprite, Peashooter): # bullet = Bullet(sprite.rect, backgd_size) # spriteGroup.add(bullet) screen.blit(bg_img_obj,(0,0)) screen.blit(sunbackImg,(250,0)) screen.blit(sun_num_surface,(270,60)) screen.blit(flower_seed, (330, 10)) screen.blit(wallNut_seed, (380, 10)) screen.blit(peashooter_seed, (430, 10)) spriteGroup.update(index) spriteGroup.draw(screen) sunList.update(index) sunList.draw(screen) (x,y) = pygame.mouse.get_pos() if choose == 1: screen.blit(sunFlowerImg,(x,y)) elif choose == 2: screen.blit(wallnutImg, (x, y)) elif choose == 3: screen.blit(peashooterImg, (x, y)) index+=1 for event in pygame.event.get(): if event.type == GEN_SUN_EVENT: for sprite in spriteGroup: if isinstance(sprite,SunFlower): now = time.time() if now - sprite.lasttime >= 5: sun = Sun(sprite.rect) sunList.add(sun) sprite.lasttime = now if event.type == GEN_BULLET_EVENT: for sprite in spriteGroup: if isinstance(sprite, Peashooter): bullet = Bullet(sprite.rect, backgd_size) spriteGroup.add(bullet) if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: pressed_key = pygame.mouse.get_pressed() print(pressed_key) if pressed_key[0] == 1: pos = pygame.mouse.get_pos() print(pos) x,y = pos if 330<=x<=380 and 10<=y<=80 and int(text) >= 50: print('点中了太阳花卡片') choose = 1 elif 380<x<=430 and 10<=y<=80 and int(text) >= 50: print('点中了坚果卡片') choose = 2 elif 430 < x <= 480 and 10 <= y <= 80 and int(text) >= 100: print('点中了豌豆射手卡片') choose = 3 elif 250 < x < 1200 and 70<y<600: #种植植物 if choose == 1: current_time = time.time() sunFlower = SunFlower(current_time) sunFlower.rect.top = y sunFlower.rect.left = x spriteGroup.add(sunFlower) choose = 0 #扣除分数 text = int(text) text -= 50 myfont = pygame.font.SysFont('arial',20) sun_num_surface = myfont.render(str(text),True,(0,0,0)) elif choose == 2: wallNut = WallNut() wallNut.rect.top = y wallNut.rect.left = x spriteGroup.add(wallNut) choose = 0 # 扣除分数 text = int(text) text -= 50 myfont = pygame.font.SysFont('arial', 20) sun_num_surface = myfont.render(str(text), True, (0, 0, 0)) elif choose == 3: peashooter = Peashooter() peashooter.rect.top = y peashooter.rect.left = x spriteGroup.add(peashooter) choose = 0 # 扣除分数 text = int(text) text -= 50 myfont = pygame.font.SysFont('arial', 20) sun_num_surface = myfont.render(str(text), True, (0, 0, 0)) print('#########') print(x,y) pass else: pass for sun in sunList: if sun.rect.collidepoint(pos): sunList.remove(sun) text = str(int(text)+50) sun_font = pygame.font.SysFont('arial', 20) sun_num_surface = sun_font.render(text, True, (0, 0, 0)) pygame.display.update()
def main(): global text,choose global sun_num_surface running = True # index必须初始化为0,否则第一张图片进不去屏幕 index = 0 while running: # if index >= 130: # index = 0 clock.tick(20) # if not pygame.mixer.music.get_busy(): # pygame.mixer.music.play() #2s产生一个太阳花 # if index % 40 == 0: # sun = Sun(sunflower.rect) # sunList.add(sun) #3s产生一个子弹 # if index % 30 == 0: # for sprite in spriteGroup: # if isinstance(sprite, Peashooter): # bullet = Bullet(sprite.rect, backgd_size) # spriteGroup.add(bullet) for bullet in bulletGroup: for zombie in zombieGroup: # 直接使用下列函数实现碰撞检测, # 这个函数接收两个精灵作为参数,返回值是一个bool变量。 if pygame.sprite.collide_mask(bullet,zombie): zombie.energy -= 1 bulletGroup.remove(bullet) for wallNut in wallNutGroup: # 只要僵尸与植物接触就是zombie.isMeetWallNut = True # 接触之后将其放入精灵建立的set函数产生的无序列表中 for zombie in zombieGroup: if pygame.sprite.collide_mask(wallNut, zombie): zombie.isMeetWallNut = True wallNut.zombies.add(zombie) for peaShooter in peaShooterGroup: for zombie in zombieGroup: if pygame.sprite.collide_mask(peaShooter, zombie): zombie.isMeetWallNut = True peaShooter.zombies.add(zombie) for sunFlower in sunFlowerGroup: for zombie in zombieGroup: if pygame.sprite.collide_mask(sunFlower, zombie): zombie.isMeetWallNut = True sunFlower.zombies.add(zombie) # 图片绘制 screen.blit(bg_img_obj,(0,0)) screen.blit(sunbackImg,(250,0)) # 这个是放植物的框图 screen.blit(sun_num_surface,(270,60)) # 绘制种子图片在框框里面 screen.blit(flower_seed, (330, 10)) screen.blit(wallNut_seed, (380, 10)) screen.blit(peashooter_seed, (430, 10)) # spriteGroup.update(index) # spriteGroup.draw(screen) bulletGroup.update(index) bulletGroup.draw(screen) zombieGroup.update(index) zombieGroup.draw(screen) wallNutGroup.update(index) wallNutGroup.draw(screen) peaShooterGroup.update(index) peaShooterGroup.draw(screen) sunFlowerGroup.update(index) sunFlowerGroup.draw(screen) sunList.update(index) sunList.draw(screen) # 确定鼠标点击的横纵坐标 (x,y) = pygame.mouse.get_pos() # if choose == 1: # screen.blit(sunFlowerImg,(x,y)) # elif choose == 2: # screen.blit(wallnutImg, (x, y)) # elif choose == 3: # screen.blit(peashooterImg, (x, y)) # 确定好三种植物后绘制图像位置,一般都在鼠标点击点在图片的正中心 if choose == 1: screen.blit(sunFlowerImg, (x - sunFlowerImg.get_rect().width // 2, y - sunFlowerImg.get_rect().height // 2)) if choose == 2: screen.blit(wallnutImg, (x - wallnutImg.get_rect().width // 2, y - wallnutImg.get_rect().height // 2)) if choose == 3: screen.blit(peashooterImg, (x - peashooterImg.get_rect().width // 2, y - peashooterImg.get_rect().height // 2)) # index控制确定的次数和机会,点了一次,index加一对应刷新一次屏幕 index+=1 # 精灵调用以及添加精灵组 for event in pygame.event.get(): if event.type == GEN_FLAGZOMBIE_EVENT: zombie = FlagZombie() zombieGroup.add(zombie) if event.type == GEN_ZOMBIE_EVENT: zombie = Zombie() zombieGroup.add(zombie) if event.type == GEN_SUN_EVENT: for sprite in sunFlowerGroup: # 返回当前时间的时间戳,控制太阳出现的时间 now = time.time() if now - sprite.lasttime >= 5: sun = Sun(sprite.rect) sunList.add(sun) sprite.lasttime = now if event.type == GEN_BULLET_EVENT: for sprite in peaShooterGroup: bullet = Bullet(sprite.rect, backgd_size) bulletGroup.add(bullet) if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: pressed_key = pygame.mouse.get_pressed() print(pressed_key) if pressed_key[0] == 1: pos = pygame.mouse.get_pos() print(pos) x,y = pos if 330<=x<=380 and 10<=y<=80 and int(text) >= 50: print('点中了太阳花卡片') choose = 1 elif 380<x<=430 and 10<=y<=80 and int(text) >= 50: print('点中了坚果卡片') choose = 2 elif 430 < x <= 480 and 10 <= y <= 80 and int(text) >= 100: print('点中了豌豆射手卡片') choose = 3 elif 250 < x < 1200 and 70<y<600: #种植植物 if choose == 1: current_time = time.time() sunFlower = SunFlower(current_time) sunFlower.rect.top = y sunFlower.rect.left = x sunFlowerGroup.add(sunFlower) choose = 0 #扣除分数 text = int(text) text -= 50 myfont = pygame.font.SysFont('arial',20) sun_num_surface = myfont.render(str(text),True,(0,0,0)) elif choose == 2: wallNut = WallNut() wallNut.rect.top = y wallNut.rect.left = x wallNutGroup.add(wallNut) choose = 0 # 扣除分数 text = int(text) text -= 50 myfont = pygame.font.SysFont('arial', 20) sun_num_surface = myfont.render(str(text), True, (0, 0, 0)) elif choose == 3: peashooter = Peashooter() peashooter.rect.top = y peashooter.rect.left = x peaShooterGroup.add(peashooter) choose = 0 # 扣除分数 text = int(text) text -= 50 myfont = pygame.font.SysFont('arial', 20) sun_num_surface = myfont.render(str(text), True, (0, 0, 0)) print('#########') print(x,y) pass else: pass for sun in sunList: if sun.rect.collidepoint(pos): sunList.remove(sun) text = str(int(text)+50) # 分数变化后循环渲染字体 sun_font = pygame.font.SysFont('arial', 20) sun_num_surface = sun_font.render(text, True, (0, 0, 0)) pygame.display.update()