def add_small_enemies(group1, group2, num): for i in range(num): small_enemy = SmallEnemy(bg_size) #if speed != None: #return speed group1.add(small_enemy) group2.add(small_enemy)
def add_small_enemies(group1, group2, num): """ Add enemy to sprite group """ for i in range(num): small_enemy = SmallEnemy(bg_size) group1.add(small_enemy) group2.add(small_enemy)
def add_small_enemies(group1, group2, num): """ 添加小型敌机 指定多个敌机对象添加到精灵组(sprite.group) 参数group1、group2是两个精灵组类型的形参,用以存储多个精灵对象(敌机)。 需要注意的一点是group既然是特定的精灵组结构体,在向其内部添加精灵对象时需要调用其对应的成员函数add() """ for i in range(num): small_enemy = SmallEnemy(bg_size) group1.add(small_enemy) group2.add(small_enemy)
def add_small_enemies(group1, group2, num): """ Add enemies' planes of small size :param group1: formal parameters, sprite :param group2: formal parameters, sprite :param num: :return: """ for i in range(num): small_enemy = SmallEnemy(bg_size) group1.add(small_enemy) group2.add(small_enemy)
def main(): y = 0 running = True switch_image = False delay = 60 font = pygame.font.SysFont("", 30) enemis = pygame.sprite.Group() smallenemies = pygame.sprite.Group() add_SmallEnemy(enemis, smallenemies, 6) bulls = pygame.sprite.Group() bossbulls = pygame.sprite.Group() while running: screen.blit(bgp, (0, y - 700)) screen.blit(bgp, (0, y)) delay -= 1 text_surface = font.render("score:" + str(score), True, (0, 0, 0)) screen.blit(text_surface, (385, 5)) if delay == 0: delay = 60 if delay % 3: switch_image = not switch_image #我方 if ourplane.active: if switch_image: screen.blit(ourplane.image_one, ourplane.rect) else: screen.blit(ourplane.image_two, ourplane.rect) else: screen.blit(ourplane.destroy_images[delay % 4], ourplane.rect) ourplane.reset() clock = pygame.time.Clock() clock.tick(30) key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]: ourplane.move_UP() if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]: ourplane.move_left() if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]: ourplane.move_right() if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]: ourplane.move_down() y += 10 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: #global running running = False while True: #screen.blit(pause,(100,100)) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: running = True break #print(running) if running: break screen.blit(pause, (240, 420)) pygame.display.update() #print(running) #break # pygame.display.update() if y == 700: y = 0 #子弹 if delay % 5 == 0: # bul = Bullet(ourplane.rect) # bul.update() # screen.blit(bul.image,bul.rect) bul = Bullet(ourplane.rect) bulls.add(bul) bulls.update() bulls.draw(screen) #敌机 if score < 100: if delay % 30 == 0: enemy = SmallEnemy(bg_size) enemis.add(enemy) if delay % 60 == 0: enemy = MidEnemy(bg_size) enemis.add(enemy) enemis.update() enemis.draw(screen) if score >= 100: if delay % 10 == 0: bul = BossBullet(boss.rect) bossbulls.add(bul) bossbulls.update() bossbulls.draw(screen) energy_remain = boss.energy / Boss.energy if energy_remain > 0.2: energy_color = color_green else: energy_color = color_red pygame.draw.line(screen, color_black, (boss.rect.left, boss.rect.bottom + 5), (boss.rect.right, boss.rect.bottom + 5), 5) pygame.draw.line( screen, energy_color, (boss.rect.left, boss.rect.bottom + 5), (boss.rect.left + (boss.rect.right - boss.rect.left) * energy_remain, boss.rect.bottom + 5), 5) boss.update() screen.blit(boss.image, boss.rect) #画血条 for each in enemis: energy_remain = each.energy / each.energy_in if energy_remain > 0.2: energy_color = color_green else: energy_color = color_red pygame.draw.line(screen, color_black, (each.rect.left, each.rect.top - 5), (each.rect.right, each.rect.top - 5), 5) pygame.draw.line( screen, energy_color, (each.rect.left, each.rect.top - 5), (each.rect.left + (each.rect.right - each.rect.left) * energy_remain, each.rect.top - 5), 5) collide_list = pygame.sprite.groupcollide(enemis, bulls, False, True, pygame.sprite.collide_mask) for enemy in collide_list: enemy.energy -= 1 global score score += 1 if enemy.energy <= 0: for i in range(4): screen.blit(enemy.destroy_images[i], enemy.rect) enemis.remove(enemy) if pygame.sprite.spritecollide(ourplane, enemis, True, pygame.sprite.collide_mask): ourplane.energy -= 1 if pygame.sprite.spritecollide(ourplane, bossbulls, True, pygame.sprite.collide_mask): ourplane.energy -= 1 if pygame.sprite.spritecollide(boss, bulls, True, pygame.sprite.collide_mask): boss.energy -= 1 if boss.energy <= 0: running = False endgame(score) energy_remain = ourplane.energy / OurPlane.energy if energy_remain > 0.2: energy_color = color_green else: energy_color = color_red pygame.draw.line(screen, color_black, (ourplane.rect.left, ourplane.rect.top - 5), (ourplane.rect.right, ourplane.rect.top - 5), 5) pygame.draw.line( screen, energy_color, (ourplane.rect.left, ourplane.rect.top - 5), (ourplane.rect.left + (ourplane.rect.right - ourplane.rect.left) * energy_remain, ourplane.rect.top - 5), 5) pygame.display.update()
def add_SmallEnemy(group1, group2, num): for i in range(num): smallenemy = SmallEnemy(bg_size) group1.add(smallenemy) group2.add(smallenemy)
bg_size = (480, 700) pygame.init() screen = pygame.display.set_mode((480, 700)) pygame.display.set_caption("airplane war") bgp = pygame.image.load("../material/image/background.png").convert() pause = pygame.image.load("../material/image/game_pause_pressed.png") start = pygame.image.load("../material/image/startpic.png") end = pygame.image.load("../material/image/game_over.png") color_black = (0, 0, 0) color_green = (7, 220, 0) color_red = (232, 16, 16) color_white = (255, 255, 255) ourplane = OurPlane(bg_size) smallenemy = SmallEnemy(bg_size) boss = Boss(bg_size) #bul = Bullet(ourplane.rect) score = 0 def add_shot(group1, num): for i in range(num): bul = Bullet(ourplane.rect) group1.add(bul) def add_SmallEnemy(group1, group2, num): for i in range(num): smallenemy = SmallEnemy(bg_size)
def main(): pygame.mixer.music.play(-1) running = True is_pause = False switch_image = False delay = 60 score = 0 font = pygame.font.SysFont("arial", 16) enemies = pygame.sprite.Group() add_enemies(SmallEnemy(bg_size), enemies, 6) add_enemies(MidEnemy(bg_size), enemies, 3) add_enemies(BigEnemy(bg_size), enemies, 1) start_time = datetime.datetime.now() bullets1 = [] bullet_num = 10 for i in range(bullet_num): bullets1.append(Bullet(hero.rect.midtop)) bullet_index = 0 me_destory_index = 0 # 英雄机爆炸图片下标 e1_destory_index = 0 # 中小型飞机爆炸图片下标 e2_destory_index = 0 # 大型飞机爆炸图片下标 while running: # 画背景 screen.blit(background, (0, 0)) score_surface = font.render(str("SCORE:%d" % score), True, (0, 0, 0)) screen.blit(score_surface, (1, 1)) clock = pygame.time.Clock() clock.tick(60) for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == pygame.K_SPACE: is_pause = not is_pause if event.type == QUIT: print("exit") pygame.quit() sys.exit() if is_pause: continue if not (delay % 3): switch_image = not switch_image # 画英雄机 if hero.active: key_pressed = pygame.key.get_pressed() if key_pressed[K_UP]: hero.move_up() if key_pressed[K_DOWN]: hero.move_down() if key_pressed[K_LEFT]: hero.move_left() if key_pressed[K_RIGHT]: hero.move_right() if switch_image: screen.blit(hero.image_one, hero.rect) else: screen.blit(hero.image_two, hero.rect) # 发射子弹 if not (delay % 10): bullets = bullets1 bullets[bullet_index].shoot(hero.rect.midtop) bullet_sound.play() bullet_index = (bullet_index + 1) % bullet_num for b in bullets: if b.active: b.move() screen.blit(b.image, b.rect) enemies_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask) if enemies_hit: b.active = False for e in enemies_hit: if e.blood <= 0: e.active = False score += e.score if isinstance(e, SmallEnemy): enemy1_down_sound.play() elif isinstance(e, MidEnemy): enemy2_down_sound.play() else: enemy3_down_sound.play() else: e.blood -= 1 # 英雄机被撞 hero_hit = pygame.sprite.spritecollide(hero, enemies, False, pygame.sprite.collide_mask) if hero_hit: hero.active = False else: screen.blit(hero.destory_images[me_destory_index], hero.rect) me_destory_index = (me_destory_index + 1) % 4 if me_destory_index == 3: screen.blit(game_over_img, (0, 0)) font = pygame.font.SysFont("arial", 35) score_surface = font.render(str("%d" % score), True, (0, 0, 255)) screen.blit(score_surface, (bg_size[0] / 2, bg_size[1] / 2)) is_pause = True # 画敌人 now = datetime.datetime.now() if not (now - start_time).seconds % 10 and now.second != start_time.second: add_enemies(SmallEnemy(bg_size), enemies, 1) start_time = now for each in enemies: if each.active: if isinstance(each, BigEnemy): each.move() if switch_image: screen.blit(each.image_one, each.rect) else: screen.blit(each.image_two, each.rect) else: each.move() screen.blit(each.image, each.rect) else: if isinstance(each, BigEnemy): screen.blit(e.destory_images[e1_destory_index], e.rect) e1_destory_index = (e1_destory_index + 1) % 6 if e1_destory_index == 0: each.reset() else: screen.blit(e.destory_images[e2_destory_index], e.rect) e2_destory_index = (e2_destory_index + 1) % 4 if e1_destory_index == 0: each.reset() pygame.display.flip() if delay == 0: delay = 60 delay -= 1