# 5. 处理用户输入 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 6. 更新所有角色 all_sprites.update() # 获得用户所有的键盘输入序列(如果用户通过键盘发出“向上”的指令,其他类似) key_pressed = pygame.key.get_pressed() if key_pressed[K_w] or key_pressed[K_UP]: plane.move_up() if key_pressed[K_s] or key_pressed[K_DOWN]: plane.move_down() if key_pressed[K_a] or key_pressed[K_LEFT]: plane.move_left() if key_pressed[K_d] or key_pressed[K_RIGHT]: plane.move_right() if key_pressed[K_SPACE]: bullet = Bullet.new_bullet((plane.rect.x, plane.rect.y)) if bullet: bullet_sprites.add(bullet) all_sprites.add(bullet) # 子弹击毁敌舰 for enemy in enemy_sprites: if enemy.is_survive(): enemy.strike(bullet_sprites) if not enemy.is_survive(): score += 1 print(enemy, score)
def main(): me_destroy_index = 0 bullets = [] pygame.mixer.music.play(loops=-1) # loops = -1,音乐无限循环(默认循环播放一次) running = True delay = 60 # 对一些效果进行延迟,效果更好一些 plane = Plane(bg_size) switch_image = False enemies = pygame.sprite.Group() # 生成敌方飞机组(一种精灵组用以存储所有敌机精灵) small_enemies = pygame.sprite.Group() # 敌方小型飞机组(不同型号敌机创建不同的精灵组来存储) add_small_enemies(small_enemies, enemies, 4) # 生成若干敌方小型飞机 # 定义子弹, 各种敌机和我方敌机的毁坏图像索引 bullet_index = 0 e1_destroy_index = 0 plane_destroy_index = 0 # 定义子弹实例化个数 bullet1 = [] bullet_num = 6 for i in range(bullet_num): bullet1.append(Bullet(plane.rect.midtop)) while running: # 绘制背景图 screen.blit(backgroud, (0, 0)) # 飞机是喷气式的, 那么这个就涉及到一个帧数的问题 clock = pygame.time.Clock() clock.tick(60) # 绘制我方飞机的两种不同状态,喷气时与不喷气时 if not delay % 3: switch_image = not switch_image for each in small_enemies: if each.active: each.move() screen.blit(each.image, each.rect) pygame.draw.line(screen, color_black, (each.rect.left, each.rect.top - 5), (each.rect.right, each.rect.top - 5), 2) energy_remain = each.energy / SmallEnemy.energy # 如果血量大约百分之二十则为绿色,否则为红色 if energy_remain > 0.2: energy_color = color_green else: energy_color = color_red 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) # 随机循环输出敌方小飞机 for e in small_enemies: e.move() screen.blit(e.image, e.rect) else: while e1_destroy_index == 0: e1_destroy_index_second = 0 screen.blit(each.destroy_images[e1_destroy_index_second], each.rect) e1_destroy_index = (e1_destroy_index_second + 1) % 4 e1_destroy_index_second += 1 enemy1_die_sound.play() each.reset() # 飞机存活状态 if plane.active: if switch_image: screen.blit(plane.image_one, plane.rect) else: screen.blit(plane.image_two, plane.rect) # 飞机存活状态下才能发射子弹,且没10帧发射一颗移动的子弹 if not (delay % 10): bullet_sound.play() bullets = bullet1 bullets[bullet_index].reset(plane.rect.midtop) 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: e.active = False # 小型飞机被毁 # 毁坏状态绘制爆炸的场面 else: if not (delay % 3): while me_destroy_index == 0: screen.blit(plane.destroy_images[plane_destroy_index], plane.rect) # 四张图片切换实现动画效果 me_destroy_index = (plane_destroy_index + 1) % 4 plane_destroy_index += 1 # 爆炸声音效果 me_die_sound.play() plane.reset() # 调用 pygame 实现的碰撞方法 spritecollide (我方飞机如果和敌机碰撞, 更改飞机的存活属性) enemies_down = pygame.sprite.spritecollide(plane, enemies, False, pygame.sprite.collide_mask) if enemies_down: plane.active = False for enemy in enemies: enemy.active = False # 响应用户的操作 for event in pygame.event.get(): # 如果按下屏幕上的关闭按钮,触发quit事件,游戏退出 if event.type == 12: pygame.quit() sys.exit() if delay == 0: delay = 60 delay -= 1 # 获取用户输入的所有键盘序列,如向上 key_pressed = pygame.key.get_pressed() if key_pressed[K_w] or key_pressed[K_UP]: plane.move_up() if key_pressed[K_s] or key_pressed[K_DOWN]: plane.move_down() if key_pressed[K_a] or key_pressed[K_LEFT]: plane.move_left() if key_pressed[K_d] or key_pressed[K_RIGHT]: plane.move_right() # 再而我们将背景图像输出到屏幕上 pygame.display.flip()
def main(): pygame.mixer.music.play(-1) our_plane = Plane(bg_size) switch_image = False # 定义飞机的切图效果标识 delay_time = 60 running = True enemies = pygame.sprite.Group() small_enemies = pygame.sprite.Group() add_small_enemy(enemies, small_enemies, 4) bullet_index = 0 e1_destroy_index = 0 plane_destroy_index = 0 bullet1 = [] bullet_num = 10 add_bullet(our_plane, bullet1, bullet_num) while running: screen.blit(bg_img, (0, 0)) clock = pygame.time.Clock() clock.tick(60) if not delay_time % 3: switch_image = not switch_image for small_enemy in small_enemies: if small_enemy.active: for e in small_enemies: e.move() screen.blit(e.image, e.rect) else: if e1_destroy_index == 0: enemy_over_sound.play() screen.blit(small_enemy.destroy_images[e1_destroy_index], small_enemy.rect) e1_destroy_index = (e1_destroy_index + 1) % 4 if e1_destroy_index == 0: small_enemy.reset() if our_plane.active: if switch_image: screen.blit(our_plane.image_one, our_plane.rect) else: screen.blit(our_plane.image_two, our_plane.rect) if not (delay_time % 10): bullet_sound.play() bullets = bullet1 bullets[bullet_index].reset(our_plane.rect.midtop) bullet_index = (bullet_index + 1) % bullet_num for bullet in bullets: if bullet.active: bullet.move() screen.blit(bullet.image, bullet.rect) enemies_hit = pygame.sprite.spritecollide( bullet, enemies, False, pygame.sprite.collide_mask) if enemies_hit: bullet.active = False for e in enemies_hit: e.active = False else: if not (delay_time % 3): screen.blit(our_plane.destroy_images[plane_destroy_index], our_plane.rect) plane_destroy_index = (plane_destroy_index + 1) % 4 if plane_destroy_index == 0: game_over_sound.play() our_plane.reset() plane_enemy_hit = pygame.sprite.spritecollide( our_plane, enemies, False, pygame.sprite.collide_mask) if plane_enemy_hit: our_plane.active = False for e in enemies: e.active = False if delay_time == 0: delay_time = 60 delay_time -= 1 key_pressed = pygame.key.get_pressed() if key_pressed[K_a] or key_pressed[K_LEFT]: our_plane.move_left() elif key_pressed[K_w] or key_pressed[K_UP]: our_plane.move_up() elif key_pressed[K_d] or key_pressed[K_RIGHT]: our_plane.move_right() elif key_pressed[K_s] or key_pressed[K_DOWN]: our_plane.move_down() if key_pressed[K_j]: our_plane.active = False for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 再而我们将背景图像并输出到屏幕上面 pygame.display.flip()