def add_big_enemys(group1, group2, num): for i in range(num): e3 = enemy.BigEnemy(bg_size) while True: if pygame.sprite.spritecollide(e3, group1, False, pygame.sprite.collide_mask): e3 = enemy.BigEnemy(bg_size) else: break group1.add(e3) group2.add(e3)
def add_big_enemies(group1, group2, num): for i in range(num): e3 = enemy.BigEnemy(bg_size) group1.add(e3) group2.add(e3)
def add_big_enemy(group1, group2, num, bg_rect): for i in range(num): each = enemy.BigEnemy(bg_rect) group1.add(each) group2.add(each)
def add_big_enemies(g1, g2, num): for i in range(num): e3 = enemy.BigEnemy() g1.add(e3) g2.add(e3)
def add_big_enemies(big_enemies, enemies, num): for i in range(num): e = enemy.BigEnemy(bg_size) big_enemies.add(e) enemies.add(e)
def main(): pygame.init() pygame.mixer.init() life_image = 'image/life.png' bg_music = 'music/game_music.ogg' background = "image/background.png" button_music = 'music/button.wav' about_me = 'image/about_me.png' about_me_image = pygame.image.load(about_me) about_me_rect = about_me_image.get_rect() MyLife = pygame.image.load(life_image) Mybackground1 = pygame.image.load(background) life_rect = MyLife.get_rect() bg_size = 480, 700 screen = pygame.display.set_mode(bg_size) pygame.display.set_caption("飞机大战") MyBullet = [] Enemy = [] kill = [] score = 0 score_font = pygame.font.Font("font/font.ttf", 36) num = 5 num1 = 0 num2 = 0 num3 = 0 harm = 1 pygame.key.set_repeat(100, 100) clock = pygame.time.Clock() MyPlane = plane.Plane(bg_size) Enemy_group = pygame.sprite.Group() bg_music = pygame.mixer.Sound(bg_music) button_sound = pygame.mixer.Sound(button_music) bg_music.play(-1) super_Bullet = False running = True again = False My_Protection_cover = plane.Protection_cover(bg_size) paused = False pause_nor_image = pygame.image.load("image/pause_nor.png").convert_alpha() pause_pressed_image = pygame.image.load( "image/pause_pressed.png").convert_alpha() resume_nor_image = pygame.image.load( "image/resume_nor.png").convert_alpha() resume_pressed_image = pygame.image.load( "image/resume_pressed.png").convert_alpha() again_image = pygame.image.load("image/again.png").convert_alpha() gameover_image = pygame.image.load("image/gameover.png").convert_alpha() me_image = pygame.image.load('image/me.png').convert_alpha() me_rect = me_image.get_rect() gameover_rect = gameover_image.get_rect() again_rect = again_image.get_rect() paused_rect = pause_nor_image.get_rect() paused_rect.left, paused_rect.top = bg_size[0] - paused_rect.width - 10, 10 paused_image = pause_nor_image tnt_num = 10 while running: if MyPlane.dead == True: MyPlane = plane.Plane(bg_size) MyPlane.not_dead = not MyPlane.not_dead MyPlane.life_num = oldLife again = True num4 = 0 for event in pygame.event.get(): if event.type == QUIT: bg_music.stop() pygame.quit() sys.exit() if event.type == KEYDOWN and MyPlane.dead == False: if event.key == K_TAB: MyPlane.not_dead = not MyPlane.not_dead if event.key == K_RETURN: if harm < 11: harm += 1 if harm >= 11: harm = 1 if event.key == K_SPACE: if tnt_num > 0: button_sound.play() for each in Enemy: kill.append(each) score += each.score Enemy.clear() Enemy_group = pygame.sprite.Group() tnt_num -= 1 if event.type == MOUSEBUTTONDOWN: if event.button == 1 and paused_rect.collidepoint(event.pos): paused = not paused if paused: bg_music.stop() else: bg_music.play(-1) if event.type == MOUSEMOTION: if paused_rect.collidepoint(event.pos) == True: if paused == True: paused_image = resume_pressed_image if paused == False: paused_image = pause_pressed_image else: if paused == True: paused_image = resume_nor_image if paused == True: paused_image = pause_nor_image screen.blit(Mybackground1, (0, 0)) key = pygame.key.get_pressed() if MyPlane.dead == False and paused == False: if key[K_w] or key[K_UP]: MyPlane.move([0, -1]) if key[K_s] or key[K_DOWN]: MyPlane.move([0, 1]) if key[K_a] or key[K_LEFT]: MyPlane.move([-1, 0]) if key[K_d] or key[K_RIGHT]: MyPlane.move([1, 0]) if key[K_j] or key[K_1]: if num1 == num: temp = bullet.Bullet(MyPlane.rect.centerx, MyPlane.rect.top, harm) music = pygame.mixer.Sound(temp.music) music.play() MyBullet.insert(0, temp) num1 = 0 num1 += 1 for each in Enemy: if each.disappear == True: Enemy.remove(each) elif paused == False: each.move() if each.Enemytype == 'BigEnemy' and each.hit == False: if num3 >= 5: screen.blit(each.default_image[0], each.rect) elif num3 <= 5: screen.blit(each.default_image[1], each.rect) elif each.hit == False: screen.blit(each.default_image[0], each.rect) else: screen.blit(each.hit_image[0], each.rect) for each in MyBullet: if each.disappear == True: MyBullet.remove(each) elif paused == False: each.move() screen.blit(each.image, each.rect) for x in MyBullet: for y in Enemy: if pygame.sprite.collide_rect(x, y): MyBullet.remove(x) y.life -= harm break if MyPlane.not_dead == False: enemies_down = pygame.sprite.spritecollide\ (MyPlane, Enemy_group, False, pygame.sprite.collide_mask) else: enemies_down = pygame.sprite.spritecollide\ (My_Protection_cover, Enemy_group, False, pygame.sprite.collide_mask) if enemies_down: if MyPlane.not_dead == False and MyPlane.dead == False: MyPlane.life_num -= 1 oldLife = MyPlane.life_num kill.append(MyPlane) MyPlane.dead = True tnt_num = 10 for each in enemies_down: Enemy_group.remove(each) score += each.score try: Enemy.remove(each) except: pass each.dead() kill.append(each) if (num2 % 30) == 0 and num2 != 0: x = randint(0, bg_size[0]) temp = enemy.SmallEnemy(bg_size) temp.init_image() temp.init_pos(randint(0, bg_size[0]), 0) Enemy.append(temp) Enemy_group.add(temp) if (num2 % 90) == 0 and num2 != 0: x = randint(0, bg_size[0]) temp = enemy.MidEnemy(bg_size) temp.init_image() temp.init_pos(randint(0, bg_size[0]), 0) Enemy.insert(0, temp) Enemy_group.add(temp) if (num2 % 250) == 0 and num2 != 0: x = randint(0, bg_size[0]) temp = enemy.BigEnemy(bg_size) temp.init_image() temp.init_pos(randint(0, bg_size[0]), 0) Enemy.append(temp) Enemy_group.add(temp) num2 = 0 for k in kill: if len(k.dead_image) <= 0: kill.remove(k) continue else: screen.blit(k.dead_image[0], k.rect) del k.dead_image[0] for each in Enemy: if each.life <= 0: each.dead() Enemy.remove(each) Enemy_group.remove(each) kill.append(each) score += each.score continue if each.life <= each.hitLife / 2 and each.hit == False: each.hit = True if num3 >= 10: num3 = 0 num2 += 1 num3 += 1 if MyPlane.not_dead == True and paused == False: My_Protection_cover.move\ (MyPlane.rect.centerx,MyPlane.rect.centery) screen.blit(\ My_Protection_cover.image,\ My_Protection_cover.rect) if MyPlane.dead == False: if num3 <= 5: screen.blit(MyPlane.default_image[0], MyPlane.rect) elif num3 > 5: screen.blit(MyPlane.default_image[1], MyPlane.rect) score_text = score_font.render("Score : %s" % str(score), True, (255, 0, 0)) screen.blit(score_text, (10, 5)) for i in range(0, MyPlane.life_num): screen.blit(MyLife, (0 + (life_rect.w * i), bg_size[1] - life_rect.h)) if MyPlane.life_num <= 0: game_over_text = score_font.render\ ("Game Over" , True, (0, 0, 0)) game_over_rect = game_over_text.get_rect() screen.blit(Mybackground1, (0, 0)) Enemy.clear() running = False bg_music.stop() is_about_me = False again_rect.left, again_rect.top =\ ((bg_size[0]- again_rect.width) // 2, bg_size[1]//2-gameover_rect.height) gameover_rect.left , gameover_rect.top =\ ((bg_size[0] - gameover_rect.width) // 2, bg_size[1]//2) about_me_rect.left, about_me_rect.top = \ ((bg_size[0] - about_me_rect.width) // 2, bg_size[1]//2+about_me_rect.height) me_rect.left, me_rect.top = (bg_size[0] - me_rect.width) // 2, ( bg_size[1] // 2 - me_rect.height - about_me_rect.height) while True: for event in pygame.event.get(): if event.type == QUIT: bg_music.stop() pygame.quit() sys.exit() if event.type == MOUSEBUTTONDOWN: pos = pygame.mouse.get_pos() if again_rect.left < pos[0] < again_rect.right and \ again_rect.top < pos[1] < again_rect.bottom: main() elif gameover_rect.left < pos[0] < gameover_rect.right and \ gameover_rect.top < pos[1] < gameover_rect.bottom: pygame.quit() sys.exit() elif about_me_rect.left < pos[0] < about_me_rect.right and \ about_me_rect.top < pos[1] < about_me_rect.bottom: is_about_me = not is_about_me game_over_rect = game_over_text.get_rect() screen.blit(Mybackground1, (0, 0)) screen.blit\ (game_over_text,((bg_size[0] - game_over_rect.width) // 2, bg_size[1]//2-100)) if is_about_me == True: screen.blit(me_image, me_rect) screen.blit(score_text, (0, 10)) screen.blit(again_image, again_rect) screen.blit(gameover_image, gameover_rect) screen.blit(about_me_image, about_me_rect) pygame.display.flip() clock.tick(60) if again == True: num4 += 1 if num4 == 600: del num4 again = False MyPlane.not_dead = False pos = pygame.mouse.get_pos() if paused_rect.left < pos[0] < paused_rect.right and \ paused_rect.top < pos[1] < paused_rect.bottom: if paused == True: paused_image = resume_pressed_image if paused == False: paused_image = pause_pressed_image else: if paused == True: paused_image = resume_nor_image if paused == False: paused_image = pause_nor_image screen.blit(paused_image, paused_rect) pygame.display.flip() clock.tick(60)
def add_big_enemies(group1, group2, num): for i in range(num): e3 = enemy.BigEnemy(bg_size) #创建大型飞机实例化对象 group1.add(e3) #把生成的大型飞机添加到big_enemies中 group2.add(e3) #把生成的大型飞机添加到 enemies中
def main(): pygame.mixer.music.play(-1) # 生成我方飞机 me = myplane.MyPlane(bg_size) # 生成一个敌机组 enemies = pygame.sprite.Group() # 生成敌方小型飞机 small_enemies = pygame.sprite.Group() add_small_enemies(small_enemies, enemies, 3) # 生成敌方中型飞机 mid_enemies = pygame.sprite.Group() add_mid_enemies(mid_enemies, enemies, 2) # 生成敌方boss boss = enemy.BigEnemy(bg_size) big_enemies = pygame.sprite.Group() add_big_enemies(big_enemies, enemies, 1) # 生成普通子弹 bullet1 = [] bullet1_index = 0 BULLET1_NUM = 4 for i in range(BULLET1_NUM): bullet1.append(bullet.Bullet(me.rect.center)) # me.rect.midtop:机头位置 # 设置帧数 clock = pygame.time.Clock() # 中弹图片索引 e1_destroy_index = 0 e2_destroy_index = 0 # 用于切换图片 list_image = True # 用于延迟 delay = 100 sum = 0 # 计数器 计算敌机死亡数 到达一定数量刷新敌机 running = True while running: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() # 检测用户的键盘操作 key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_UP] or key_pressed[K_w]: me.moveUp() if key_pressed[pygame.K_LEFT] or key_pressed[K_a]: me.moveLeft() if key_pressed[pygame.K_RIGHT] or key_pressed[K_d]: me.moveRight() if key_pressed[pygame.K_DOWN] or key_pressed[K_s]: me.moveDown() screen.blit(background, (0, 0)) # 发射子弹 if not (delay % 10): bullet1[bullet1_index].reset(me.rect.midtop) bullet1_index = (bullet1_index + 1) % BULLET1_NUM # 检测子弹是否击中敌机 for b in bullet1: if b.active: b.move() screen.blit(b.image, b.rect) enemy_hit = pygame.sprite.spritecollide(b, enemies, False, pygame.sprite.collide_mask) if enemy_hit: b.active = False for e in enemy_hit: if e in mid_enemies or e in big_enemies: e.hit = True e.energy -= 1 if e.energy == 0: e.active = False sum += 1 else: # 是小型敌机 e.active = False sum += 1 if sum == 1: add_small_enemies(small_enemies, enemies, 1) elif sum == 2: add_mid_enemies(mid_enemies, enemies, 1) elif sum == 3: add_big_enemies(big_enemies, enemies, 1) sum = 0 # 绘制大型敌机 for each in big_enemies: if each.active: each.move() if list_image: screen.blit(each.image1, each.rect) else: screen.blit(each.image2, each.rect) else: # 毁灭 if not (delay % 3): screen.blit(each.destroy_images[e1_destroy_index], each.rect) e1_destroy_index = (e1_destroy_index + 1) % 20 if e1_destroy_index == 0: each.reset() # 绘制中型敌机 for each in mid_enemies: if each.active: each.move() screen.blit(each.image, each.rect) if list_image: screen.blit(each.image, each.rect) else: # 毁灭 each.reset() for each in small_enemies: if each.active: each.move() screen.blit(each.image, each.rect) if list_image: screen.blit(each.image, each.rect) else: # 毁灭 each.reset() # 检测我方飞机是否被撞 enemies_down = pygame.sprite.spritecollide(me, enemies, False) if enemies_down: me.active = False for e in enemies_down: e.active = False # 绘制我方飞机 if me.active: screen.blit(me.image, me.rect) else: if not (delay % 3): screen.blit(me.destroy_images[e2_destroy_index], me.rect) e2_destroy_index = (e2_destroy_index + 1) % 6 if e2_destroy_index == 0: print("Game Over!") running = False if not (delay % 5): list_image = not list_image delay -= 1 if not delay: delay = 100 pygame.display.flip() clock.tick(60)
def main(): pygame.mixer.music.play() clock = pygame.time.Clock() running = True ############################################实例化我方飞机 me = myplane.MyPlane(bg_size, myplane_image) ########################################################################实例化敌方飞机 smallenemies = [] midenemies = [] bigenemies = [] enemies = [] for i in range(15): #实例化小飞机,15个一组 smallenemy = enemy.SmallEnemy(bg_size) smallenemies.append(smallenemy) enemies.append(smallenemy) for i in range(7): #实例化中飞机,5个一组 midenemy = enemy.MidEnemy(bg_size) midenemies.append(midenemy) enemies.append(midenemy) for i in range(3): #实例化大飞机,2个一组 bigenemy = enemy.BigEnemy(bg_size) bigenemies.append(bigenemy) enemies.append(bigenemy) ##################################中弹索引 e1_destory_index = 0 e2_destory_index = 0 e3_destory_index = 0 me_destory_index = 0 #####################################生成子弹 bullet1 = [] bullet1_index = 0 BULLET1_NUM = 4 delay = 100 for i in range(BULLET1_NUM): bullet1.append(bullet.Bullet1(me.rect.midtop)) ##############初始化得分 score = 0 score_font = pygame.font.Font('font/AGENCYR.TTF', 36) gameover_font = pygame.font.Font('font/LCALLIG.TTF', 40) ##################################################################################主循环 while running: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() screen.blit(background, (0, 0)) ###############################################################敌机登场和碰撞检测 for each in smallenemies: if each.act: each.move() screen.blit(each.image, each.rect) else: enemy1_down_sound.play() screen.blit(each.destory_images[e1_destory_index], each.rect) score += 1000 each.reset() each.act = True #注意!!!敌机被销毁以后,要讲act为真,否则act一直为假,新生成的飞机不会下落 for each in midenemies: if each.act: each.move() screen.blit(each.image, each.rect) else: enemy2_down_sound.play() screen.blit(each.destory_images[e2_destory_index], each.rect) score += 6000 each.reset() each.act = True for each in bigenemies: if each.act: each.move() screen.blit(each.image, each.rect) if each.rect.top > -100: enemy3_fly_sound.play() else: enemy3_down_sound.play() screen.blit(each.destory_images[e3_destory_index], each.rect) score += 10000 each.reset() each.act = True ##########################################################检测敌机是否重叠,如果重叠则重新生成 for i in enemies: enemies.remove(i) enemies_chongdie = pygame.sprite.spritecollide( i, enemies, False, pygame.sprite.collide_mask) if enemies_chongdie: for each in enemies_chongdie: if each.rect.bottom < 0: i.reset() else: pass else: pass enemies.append(i) ##################################################################我方飞机登场 if me.act: screen.blit(me.image, me.rect) else: me_down_sound.play() screen.blit(me.destory_images[me_destory_index], me.rect) print('游戏结束!') gameover_text = gameover_font.render('Game Over!', True, (255, 255, 255)) screen.blit(gameover_text, (bg_size[0] / 2 - 100, bg_size[1] / 2)) time.sleep(3) running = False ######################################检测用户的键盘操作,控制飞机移动 key_pressed = pygame.key.get_pressed() #移动飞机 if key_pressed[K_w] or key_pressed[K_UP]: me.moveup() if key_pressed[K_s] or key_pressed[K_DOWN]: me.movedown() if key_pressed[K_a] or key_pressed[K_LEFT]: me.moveleft() if key_pressed[K_d] or key_pressed[K_DOWN]: me.moveright() #############################################################################碰撞检测 enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask) if enemies_down: me.act = False for i in enemies_down: i.act = False ###############################################################################发射子弹 delay -= 1 if not delay: delay = 100 if not (delay % 10): bullet1[bullet1_index].reset1(me.rect.midtop) bullet1_index = (bullet1_index + 1) % BULLET1_NUM for b in bullet1: if b.active: b.move() screen.blit(b.image, b.rect) enemy_hit = pygame.sprite.spritecollide( b, enemies, False, pygame.sprite.collide_mask) if enemy_hit: b.active = False for each in enemy_hit: if each not in smallenemies: ########### #############################################添加血条 pygame.draw.line( screen, (0, 0, 0), (each.rect.left, each.rect.top - 5), (each.rect.right, each.rect.top - 5), 2) if each in midenemies: energy_remain = each.energy / enemy.MidEnemy.energy else: energy_remain = each.energy / enemy.BigEnemy.energy if energy_remain > (255, 255, 0): energy_color = GL_GREEN_SIZE else: energy_color = (255, 0, 0) pygame.draw.line( screen, energy_color, (each.rect.left, each.rect.top - 5), (each.rect.left + each.rect.width * energy_remain, each.rect.top - 5), 2) #################################################################每中弹一次,生命值减1 each.energy -= 1 if each.energy == 0: each.act = False else: each.act = False #########################################绘制得分 score_text = score_font.render('Score: %s' % str(score), True, (255, 255, 255)) screen.blit(score_text, (10, 5)) pygame.display.flip() clock.tick(60)
def main(): # 初始化游戏 pygame.init() # 游戏窗口 screen = pygame.display.set_mode((480, 700)) # bg图片 bg_img = pygame.image.load("./images/timg.jpg").convert_alpha() # 标题 pygame.display.set_caption("飞机大战--Bata") # 实例化我方飞机 myplane = plane.MyPlane(bg_img.get_rect()) #控制图片切换速度 delay = 0 change_img = False image3 = pygame.image.load("./images/life.png").convert_alpha() rect = image3.get_rect() num = 3 # 敌机组 enemies_group = pygame.sprite.Group() # 小敌机组 smallEny_group = pygame.sprite.Group() # 实例化小型机-->15个 for i in range(SMALL_ENEMY_NUM): small = enemy.SmallEnemy(bg_img.get_rect()) add_group(enemies_group, smallEny_group, small) # 中敌机组 midEny_group = pygame.sprite.Group() for i in range(MID_ENEMY_NUM): small = enemy.MidEnemy(bg_img.get_rect()) add_group(enemies_group, midEny_group, small) # 大敌机组 bigEny_group = pygame.sprite.Group() for i in range(BIG_ENEMY_NUM): small = enemy.BigEnemy(bg_img.get_rect()) add_group(enemies_group, bigEny_group, small) #炸弹组 zhadan_group = pygame.sprite.Group() for i in range(2): small = enemy.Zhadan(bg_img.get_rect()) zhadan_group.add(small) #火力组 zhidan_group = pygame.sprite.Group() for i in range(1): small = enemy.Zhidan(bg_img.get_rect()) zhidan_group.add(small) # 销毁索引 mid_index = 0 big_index = 0 me_index = 0 bullet_index = 0 # print(myplane.rect.width) print(myplane.rect.left) print(myplane.rect.top) print(myplane.rect.midtop) # 实例化子弹对象 bullet_group = pygame.sprite.Group() bulletlist = [] for i in range(BULLET_NUM): b = bullet.Bullet(myplane.rect.left, myplane.rect.top) bullet_group.add(b) bulletlist.append(b) bullet_group1 = pygame.sprite.Group() bulletlist1 = [] for i in range(BULLET_NUM): b1 = bullet.Bullet1(myplane.rect.left, myplane.rect.top) bullet_group1.add(b1) bulletlist1.append(b1) # 设置刷新速度 clock = pygame.time.Clock() # 显示分数 score = 0 # font对象 fnt = pygame.font.Font('./font/myfont.ttf', 24) # 背景音乐 pygame.mixer.init() pygame.mixer.music.load("./sound/game_music.ogg") pygame.mixer.music.play(-1) running = True while running: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() exit(1) # 频繁按键 pressedkeys = pygame.key.get_pressed() if pressedkeys[K_LEFT] or pressedkeys[K_a]: myplane.move_left() elif pressedkeys[K_RIGHT] or pressedkeys[K_d]: myplane.move_right() elif pressedkeys[K_UP] or pressedkeys[K_w]: myplane.move_up() elif pressedkeys[K_DOWN] or pressedkeys[K_s]: myplane.move_down() # 画背景图片 screen.fill((255, 255, 255)) screen.blit(bg_img, bg_img.get_rect()) # 子弹重置 if not delay % 10: bulletlist[bullet_index].reset(myplane.rect.midtop) bullet_index = (bullet_index + 1) % BULLET_NUM # 绘制子弹 for d in bullet_group: if d.alive: d.move() screen.blit(d.image, (d.rect.left, d.rect.top)) # 子弹是否与敌机发生碰撞 colleny = pygame.sprite.spritecollide( d, enemies_group, False, pygame.sprite.collide_mask) for e in colleny: if e in smallEny_group: score += 1000 e.alive = False else: e.energy -= 1 if e.energy == 0: if e in bigEny_group: score += 10000 else: score += 5000 e.alive = False d.alive = False # 绘制敌机 for e in bigEny_group: if e.alive: e.move() if delay % 4 == 0: screen.blit(e.image1, e.rect) else: screen.blit(e.image2, e.rect) # 绘制血槽 pygame.draw.line(screen, (0,0,0), (e.rect.left, e.rect.top-5), \ (e.rect.right, e.rect.top-5), 2) # 余血 current_egy = e.energy / enemy.BigEnemy.energy if current_egy < 0.2: color_paint = (255, 0, 0) else: color_paint = (0, 255, 0) pygame.draw.line(screen, color_paint, (e.rect.left, e.rect.top - 5), \ (e.rect.left + e.rect.width * current_egy, e.rect.top - 5), 2) else: screen.blit(e.destroy_image[big_index], e.rect) if delay % 3 == 0: big_index += 1 if big_index == 6: big_index = 0 e.reset() for e in midEny_group: if e.alive: e.move() screen.blit(e.image, e.rect) # 绘制血槽 pygame.draw.line(screen, (0, 0, 0), (e.rect.left, e.rect.top - 5), \ (e.rect.right, e.rect.top - 5), 2) # 余血 current_egy = e.energy / enemy.MidEnemy.energy if current_egy < 0.2: color_paint = (255, 0, 0) else: color_paint = (0, 255, 0) pygame.draw.line(screen, color_paint, (e.rect.left, e.rect.top - 5), \ (e.rect.left+e.rect.width * current_egy, e.rect.top - 5), 2) else: screen.blit(e.destroy_image[mid_index], e.rect) if delay % 3 == 0: mid_index += 1 if mid_index == 4: mid_index = 0 e.reset() for e in smallEny_group: if e.alive: e.move() screen.blit(e.image, e.rect) else: e.reset() # screen.blit(b1.image, b1.rect) #绘制炸弹 for e in zhadan_group: if e.alive: e.move() screen.blit(e.image1, e.rect) else: e.reset() #绘制火力 for e in zhidan_group: if e.alive: e.move() screen.blit(e.image1, e.rect) else: e.reset() #画生命 for i in range(num): screen.blit(image3,(480-10-(i+1)*rect.width,\ 700-10-rect.height)) # 检测敌机是否撞击我方飞机 collide_plane = pygame.sprite.spritecollide(myplane, enemies_group, False, \ pygame.sprite.collide_mask) if collide_plane: # 发生碰撞 # myplane.live-=1 for e in collide_plane: e.alive = False if e in smallEny_group: myplane.engy -= 40 if e in midEny_group: myplane.engy -= 6 if e in bigEny_group: myplane.engy -= 12 if myplane.engy <= 0: myplane.engy = 120 num -= 1 if num == 0: myplane.alive = False myrender = fnt.render("Score:%d" % score, True, (234, 222, 56)) # 显示文字 #检测与炸弹发生碰撞 num1 = 0 collide_zhadan=pygame.sprite.spritecollide(myplane, zhadan_group, False, \ pygame.sprite.collide_mask) if collide_zhadan: for e in collide_zhadan: e.alive = False for e in enemies_group: e.alive = False collide_zhidan=pygame.sprite.spritecollide(myplane, zhidan_group, False, \ pygame.sprite.collide_mask) if collide_zhidan: num1 = 1 for e in collide_zhidan: e.alive = False for i in bulletlist: i.speed += 10 if not delay % 10: bulletlist1[bullet_index].reset(myplane.rect.midtop) bullet_index = (bullet_index + 1) % BULLET_NUM # 绘制子弹 for d in bullet_group1: if d.alive: d.move() screen.blit(d.image, (d.rect.left, d.rect.top)) # 子弹是否与敌机发生碰撞 colleny = pygame.sprite.spritecollide( d, enemies_group, False, pygame.sprite.collide_mask) for e in colleny: if e in smallEny_group: score += 1000 e.alive = False else: e.energy -= 1 if e.energy == 0: if e in bigEny_group: score += 10000 else: score += 5000 e.alive = False d.alive = False screen.blit(myrender, myrender.get_rect()) # if myplane.alive: if not change_img: screen.blit(myplane.image1, myplane.rect) else: screen.blit(myplane.image2, myplane.rect) else: # 销毁 screen.blit(myplane.destroy_images[me_index], myplane.rect) if not delay % 10: me_index += 1 if me_index == 4: running = False delay += 1 if delay % 5 == 0: change_img = True else: change_img = False pygame.display.update() clock.tick(60)
def add_big_enemies(group1, group2, num, ai_settings): for i in range(num): e3 = enemy.BigEnemy(ai_settings) group1.add(e3) group2.add(e3)
def add_big_enemies(group, group_all, num): for i in range(num): e1 = enemy.BigEnemy(bg_size) group_all.add(e1) group.add(e1)