def initialise(self): """ Initialising and popoulating the board """ self.board[-3:, 0:self.width] = '*' self.board[-17:-3, 2 * self.width // 3] = '{' self.board[-18:-17, 2 * self.width // 3] = '*' self.board[-3, (2 * self.width // 3 - 1)] = 'S' self.board[-17:-3, self.width - self._screenwidth - 10] = '}' self.board[-18:-17, self.width - self._screenwidth - 10] = '*' for i in range(1, self.width // 3, 45): putclouds(self.board, i, 2) for i in range(self.width // 3, 2 * self.width // 3, 70): putmountains(self.board, i, 2) for i in range(1, (2 * self.width // 3 - 100), 80): putpipes(self.board, i, -9) for i in range(1, 2 * self.width // 3 - 100, 40): self.board[-3][i] = 'S' for i in range(1, 2 * self.width // 3 - 100, 130): putbricks(self.board, i, -14, np.random.randint(2, 9)) putbricks(self.board, i + 3, -14, np.random.randint(2, 9)) for i in range(90, 2 * self.width // 3 - 100, 97): Enemies(self.board, i, -4, "left") putsunset(self.board, 3 * self.width // 4 + 50, 10)
class Main: def __init__( self ): self.size = ( 640, 480 ) self.scr = p.display.set_mode( self.size ) self.reset( ) def reset( self ): self.objectlist = [ ] from player import Player from block import Blocks from enemy import Enemies self.player = Player( self ) self.playerlist = [ self.player ] self.blocks = Blocks( self ) self.enemies = Enemies( self ) self.active = True def update( self ): self.player.update( ) self.enemies.update( ) self.blocks.update( ) def draw( self ): self.scr.fill( ( 31, 31, 31 ) ) self.blocks.draw( ) self.player.draw( ) self.enemies.draw( ) p.display.flip( ) def loop( self ): c = p.time.Clock( ) while self.active: self.events = p.event.get( ) self.update( ) self.draw( ) c.tick( 60 )
def reset( self ): self.objectlist = [ ] from player import Player from block import Blocks from enemy import Enemies self.player = Player( self ) self.playerlist = [ self.player ] self.blocks = Blocks( self ) self.enemies = Enemies( self ) self.active = True
def __init__(self, screen, level, timer): self.timer = timer self.level = level self.screen_width = screen[0] self.screen_height = screen[1] self.player_bullets = pygame.sprite.Group() self.enemy_bullets = pygame.sprite.Group() self.player = Player(self.screen_width, self.screen_height, self.player_bullets) self.background_sprite = pygame.sprite.Group() self.background_sprite.add(Background()) self.player_explosion_sprite = pygame.sprite.Group() self.pause_sprites = pygame.sprite.Group() self.all_sprites = pygame.sprite.Group() self.all_sprites.add(self.player) self.num_of_enemies = NUM_OF_ENEMIES_PER_LVL * self.level self.enemies = Enemies(self.num_of_enemies, self.screen_width, self.enemy_bullets, self.timer, self.player.initial_position[1]) self.all_sprites.add(self.enemies) self.num_of_lives = 3 self.lives = [] utils.generate_lives( self.num_of_lives, self.lives, self.all_sprites, ) self.state = States.GAME self.player_hit = False self.space_pressed = False
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect((target_host, target_port)) INSTRUKCJA = pygame.image.load('Instrukcje.png') TEXT = pygame.image.load('txt.png') pygame.font.init() myfont = pygame.font.SysFont('monospace', 25) small_font = pygame.font.SysFont('monospace', 15) while (1): done = False p = Players(pygame) b = Bullets() m = Meteors(pygame) e = Enemies(pygame) bo = Bonus(pygame) g = Game() while (1): menu_ret = Menu() if menu_ret == 1: break elif menu_ret == 2: instrukcja() elif menu_ret == 3: exit() screen.fill((0, 0, 0)) game_start_time = time.time()
def test_left_collision_enemy(): MARIO = Person(70, -4, MB.board) Enemies(MB.board, 69, -4, "left") iscollide = MARIO.check_collision(MB.board, "left") assert iscollide == "dead"
def savannah1(character, weapon, items, money): print("CHAPTER 1, PART 1: TRIALS OF THE SAVANNAH") wait(3) cont = input("Press enter to continue: ") del cont ui.restartPage() print_text_file('StoryTexts/SavannahTexts/savannah1.txt') option = input( "\nWhat would you like to do? \n(Fight [F]lowers, [P]yroball, [B]ubble Frog): " ).lower() while option not in ['f', 'p', 'b']: print("\nINVALID INPUT") option = input( "\nWhat would you like to do? \n(Fight [F]lowers, [P]yroballs, [B]ubble Frogs: " ).lower() if option == "f": enemy1 = Enemies( "Flower", "Nature", Spells( "Pollen Rush", 40, "Nature", ("Water", "Fire"), 1, ), 22, 14, 15, 19, 1) enemy2 = Enemies( "Flower", "Nature", Spells( "Pollen Rush", 40, "Nature", ("Water", "Fire"), 1, ), 21, 15, 15, 20, 1) elif option == "p": enemy1 = Enemies( "Pyroball", "Fire", Spells("Fireball", 45, "Fire", ("Nature", "Water"), 1), 17, 16, 18, 19, 1) enemy2 = Enemies( "Pyroball", "Fire", Spells("Fireball", 45, "Fire", ("Nature", "Water"), 1), 18, 17, 17, 17, 1) elif option == "b": enemy1 = Enemies( "Bubble Frog", "Water", Spells("Bubble Blast", 40, "Water", ("Fire", "Nature"), 1), 19, 15, 20, 18, 1) enemy2 = Enemies( "Bubble Frog", "Water", Spells("Bubble Blast", 40, "Water", ("Fire", "Nature"), 1), 19, 15, 20, 18, 1) ui.restartPage() my_spells = Character.spell_by_level(character) Combat.main_fight(character, weapon, my_spells, (enemy1, enemy2), items) wait(5) ui.restartPage() enemy1 = Enemies( "flower", "Nature", Spells( "Pollen Rush", 40, "Nature", ("Water", "Fire"), 1, ), 22, 14, 15, 19, 1) enemy2 = Enemies( "flower", "Nature", Spells( "Pollen Rush", 40, "Nature", ("Water", "Fire"), 1, ), 21, 15, 15, 20, 1) character.level_up((enemy1, enemy2), money) del (enemy1, enemy2) wait(3) ui.restartPage() print_text_file('StoryTexts/SavannahTexts/savannah2.txt') wait(10) cont = input("Press enter to continue: ") del cont return
def intro(character, melee_weapon, items, currency): print("PROLOGUE: THE FIRST FIGHT") wait(3) cont = input("Press enter to continue: ") ui.restartPage() print_text_file('StoryTexts/IntroTexts/intro1.txt') wait(10) option = input( Style.RESET_ALL + "\nWhat do you do?\n[A]pproach the lights, [R]un the other way, [W]ait for the lights to come to you: " ) option = option.lower() while option not in ["a", "r", "w"]: print("\nINVALID INPUT") option = input( "What do you do?\n[A]pproach the lights, [R]un the other way, [W]ait for the lights to come to you: " ) option = option.lower() if option == "w": print(Style.RESET_ALL + Fore.LIGHTBLACK_EX + "\nYou see a faint glimpse of something coming closer") wait(3) print( Style.BRIGHT + Fore.RED + "Then it hits you with surprising power!", Style.RESET_ALL) elif option == "a": print(Style.RESET_ALL + Fore.LIGHTBLACK_EX + "\nAs you approach the lights you get blinded and faint") wait(3) print( Style.BRIGHT + Fore.RED + "Then you suddenly stir and realize that you're in another room.", Style.RESET_ALL) elif option == "r": print( Style.RESET_ALL + Fore.LIGHTBLACK_EX + "\nYou run around a corner where you can no longer see the light") wait(3) print( Style.BRIGHT + Fore.RED + "You turn to look if there is anywhere else to go, but as you do you get ambushed!", Style.RESET_ALL) wait(3) ui.restartPage() print_text_file('StoryTexts/IntroTexts/intro2.txt') wait(20) cont = input("Press enter to continue: ") del cont goblin1 = Enemies("goblin", "Dark", Weapons("shortsword", "bronze"), 20, 15, 11, 9, 1) goblin2 = Enemies("goblin", "Dark", Weapons("shortsword", "bronze"), 18, 16, 13, 9, 1) ui.restartPage() print("Let's set up your spells!\n") my_spells = character.spell_by_level() Combat.main_fight(character, melee_weapon, my_spells, (goblin1, goblin2), items) wait(5) ui.restartPage() goblin1 = Enemies("goblin", "Dark", Weapons("shortsword", "bronze"), 20, 15, 11, 9, 1) goblin2 = Enemies("goblin", "Dark", Weapons("shortsword", "bronze"), 18, 16, 13, 9, 1) character.level_up((goblin1, goblin2), money) del (goblin1, goblin2) wait(3) cont = input("Press enter to continue: ") del cont ui.restartPage() print_text_file('StoryTexts/IntroTexts/intro3.txt') wait(15) cont = input("Press enter to continue: ") del cont return
class PlayState: def __init__(self, screen, level, timer): self.timer = timer self.level = level self.screen_width = screen[0] self.screen_height = screen[1] self.player_bullets = pygame.sprite.Group() self.enemy_bullets = pygame.sprite.Group() self.player = Player(self.screen_width, self.screen_height, self.player_bullets) self.background_sprite = pygame.sprite.Group() self.background_sprite.add(Background()) self.player_explosion_sprite = pygame.sprite.Group() self.pause_sprites = pygame.sprite.Group() self.all_sprites = pygame.sprite.Group() self.all_sprites.add(self.player) self.num_of_enemies = NUM_OF_ENEMIES_PER_LVL * self.level self.enemies = Enemies(self.num_of_enemies, self.screen_width, self.enemy_bullets, self.timer, self.player.initial_position[1]) self.all_sprites.add(self.enemies) self.num_of_lives = 3 self.lives = [] utils.generate_lives( self.num_of_lives, self.lives, self.all_sprites, ) self.state = States.GAME self.player_hit = False self.space_pressed = False def create_enemy_explosion(self, sprite_dict): for sprite in sprite_dict: if sprite_dict[sprite]: explosion = Explosion(sprite.rect.center) self.all_sprites.add(explosion) self.num_of_enemies -= 1 def check_collision_bullets_enemies(self): sprite_dict = pygame.sprite.groupcollide(self.enemies, self.player_bullets, True, True, pygame.sprite.collide_mask) self.create_enemy_explosion(sprite_dict) def kill_player(self): if len(self.lives): life = self.lives.pop() life.kill() explosion = Explosion(self.player.rect.center) self.player_explosion_sprite.add(explosion) def check_collision_bullets_player(self): bullet = pygame.sprite.spritecollideany(self.player, self.enemy_bullets, pygame.sprite.collide_mask) if bullet: self.player_hit = True bullet.kill() self.kill_player() def check_if_won(self): if self.num_of_enemies == 0: if self.level == MAX_LVL: pygame.mixer.music.load("Sounds/win_music") pygame.mixer.music.play(-1) self.state = States.VICTORY self.state = States.WON_LEVEL def check_if_game_over(self): if len(self.lives) == 0 or self.enemies.is_any_down_the_screen(): self.player.kill() self.state = States.GAME_OVER pygame.mixer.music.load("Sounds/game_over_music") pygame.mixer.music.play(-1) def control_player(self): if not self.timer.is_running: pressed = pygame.key.get_pressed() if pressed[pygame.K_LEFT]: self.player.move_left() if pressed[pygame.K_RIGHT]: self.player.move_right() if pressed[pygame.K_SPACE] and not self.space_pressed: self.space_pressed = True self.player.shoot_bullet() if not pressed[pygame.K_SPACE]: self.space_pressed = False def animation(self): self.all_sprites.add(self.enemy_bullets) self.all_sprites.update() def draw(self, screen): self.background_sprite.draw(screen) self.all_sprites.draw(screen) self.player_explosion_sprite.draw(screen) def player_got_hit(self): self.timer.start() self.player_explosion_sprite.update() if len(self.player_explosion_sprite.sprites()) == 0: self.player.go_to_initial_position() self.player_hit = False self.enemies.update() self.enemy_bullets.update() def fight(self): self.control_player() self.all_sprites.add(self.player_bullets, self.enemy_bullets) self.all_sprites.update() if not self.timer.is_running: self.check_collision_bullets_player() self.check_collision_bullets_enemies() self.check_if_won() self.check_if_game_over() def update(self): if self.player_hit: self.player_got_hit() else: self.fight() return self.state
def main(): pygame.mixer.music.play(-1) # 生成我方飞机 me = MyPlane(bg_size) enemies = AllEnemies() small_enemies = Enemies('small') middle_enemies = Enemies('middle') big_enemies = Enemies('big') # 生成敌方小型飞机 enemies.add_enemies(small_enemies, 15, bg_size) # 生成敌方中型飞机 enemies.add_enemies(middle_enemies, 4, bg_size) # 生成敌方大型飞机 enemies.add_enemies(big_enemies, 2, bg_size) # 标志是否是超级子弹 is_super_bullet = False # 生成普通子弹 normal_bullets = [] normal_bullet_index = 0 for i in range(NORMAL_BULLET_NUM): normal_bullets.append(NormalBullet(me.rect.midtop)) # 生成超级子弹 super_bullets = [] super_bullet_index = 0 for i in range(SUPER_BULLET_NUM // 2): super_bullets.append(SuperBullet((me.rect.centerx - 33, me.rect.centery))) super_bullets.append(SuperBullet((me.rect.centerx + 30, me.rect.centery))) clock = pygame.time.Clock() # 用于切换图片 switch_image = True # 用于设置延迟 delay = 100 # 中弹图片索引 e1_destroy_index = 0 e2_destroy_index = 0 e3_destroy_index = 0 me_destroy_index = 0 # 记录分数 score = 0 score_font = pygame.font.Font(FONT_PATH + 'font.ttf', 36) # 游戏难度 level = 1 # 暂停游戏 paused = False pause_nor = pygame.image.load(IMAGE_PATH + 'pause_nor.png').convert_alpha() pause_pressed = pygame.image.load( IMAGE_PATH + 'pause_pressed.png').convert_alpha() resume_nor = pygame.image.load( IMAGE_PATH + 'resume_nor.png').convert_alpha() resume_pressed = pygame.image.load( IMAGE_PATH + 'resume_pressed.png').convert_alpha() nor_images = [pause_nor, resume_nor] pressed_images = [pause_pressed, resume_pressed] pause_images = nor_images pause_rect = pause_nor.get_rect() pause_rect.left, pause_rect.top = (width - pause_rect.width - 10, 10) # 全屏炸弹数量 bomb_num = 3 bomb = pygame.image.load(IMAGE_PATH + 'bomb.png').convert_alpha() bomb_rect = bomb.get_rect() bomb_font = pygame.font.Font(FONT_PATH + 'font.ttf', 48) # 支援物资 bullet_supply = BulletSupply(bg_size) bomb_supply = BombSupply(bg_size) supply = bomb_supply # 每30秒投放一次支援物资 pygame.time.set_timer(SUPPLY_TIME, 30 * 1000) # 最终得分字体设置 record_font = pygame.font.Font(FONT_PATH + 'font.ttf', 48) # 游戏结束界面 again_image = pygame.image.load(IMAGE_PATH + 'again.png').convert_alpha() again_rect = again_image.get_rect() gameover_image = pygame.image.load(IMAGE_PATH + 'gameover.png').convert_alpha() gameover_rect = gameover_image.get_rect() # 用于游戏结束时做单次处理 gameover_processed = False running = True while running: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == MOUSEBUTTONDOWN: if event.button == 1 and pause_rect.collidepoint(event.pos): button_sound.play() paused = not paused if paused: pygame.mixer.music.pause() pygame.mixer.pause() pygame.time.set_timer(SUPPLY_TIME, 0) else: pygame.mixer.music.unpause() pygame.mixer.unpause() pygame.time.set_timer(SUPPLY_TIME, 30 * 1000) elif event.type == MOUSEMOTION: if pause_rect.collidepoint(event.pos): pause_images = pressed_images else: pause_images = nor_images elif event.type == KEYDOWN: if event.key == K_SPACE: if bomb_num: bomb_num -= 1 use_bomb_sound.play() for enemy in enemies: if enemy.rect.bottom > 0: enemy.active = False elif event.type == SUPPLY_TIME: supply_sound.play() supply = choice([bullet_supply, bomb_supply]) supply.reset() elif event.type == SUPER_BULLET_TIME: is_super_bullet = False pygame.time.set_timer(SUPER_BULLET_TIME, 0) elif event.type == INVINCIBLE_TIME: me.is_invincible = False pygame.time.set_timer(INVINCIBLE_TIME, 0) screen.blit(background, (0, 0)) if me.life_num and not paused: # 检测用户的键盘操作 key_pressed = pygame.key.get_pressed() if key_pressed[K_w] or key_pressed[K_UP]: me.move_up() if key_pressed[K_s] or key_pressed[K_DOWN]: me.move_down() if key_pressed[K_a] or key_pressed[K_LEFT]: me.move_left() if key_pressed[K_d] or key_pressed[K_RIGHT]: me.move_right() # 根据分数提升游戏难度 if level == 1 and score > 50000: level = 2 upgrade_sound.play() # 增加3架小型飞机、2架中型飞机、1架大型飞机 enemies.add_enemies(small_enemies, 3, bg_size) enemies.add_enemies(middle_enemies, 2, bg_size) enemies.add_enemies(big_enemies, 1, bg_size) small_enemies.inc_speed(1) elif level == 2 and score > 300000: level = 3 upgrade_sound.play() # 增加5架小型飞机、3架中型飞机、2架大型飞机 enemies.add_enemies(small_enemies, 5, bg_size) enemies.add_enemies(middle_enemies, 3, bg_size) enemies.add_enemies(big_enemies, 2, bg_size) small_enemies.inc_speed(1) middle_enemies.inc_speed(1) elif level == 3 and score > 600000: level = 4 upgrade_sound.play() # 增加5架小型飞机、3架中型飞机、2架大型飞机 enemies.add_enemies(small_enemies, 5, bg_size) enemies.add_enemies(middle_enemies, 3, bg_size) enemies.add_enemies(big_enemies, 2, bg_size) small_enemies.inc_speed(1) middle_enemies.inc_speed(1) elif level == 4 and score > 1000000: level = 5 upgrade_sound.play() # 增加5架小型飞机、3架中型飞机、2架大型飞机 enemies.add_enemies(small_enemies, 5, bg_size) enemies.add_enemies(middle_enemies, 3, bg_size) enemies.add_enemies(big_enemies, 2, bg_size) small_enemies.inc_speed(1) middle_enemies.inc_speed(1) # 发射子弹 if is_super_bullet: bullets = super_bullets if delay % (40 / SUPER_BULLET_NUM * 2) == 0: bullet_sound.play() bullets[super_bullet_index].reset((me.rect.centerx - 33, me.rect.centery)) bullets[super_bullet_index + 1].reset((me.rect.centerx + 30, me.rect.centery)) super_bullet_index = ( super_bullet_index + 2) % SUPER_BULLET_NUM else: bullets = normal_bullets if delay % (40 / NORMAL_BULLET_NUM) == 0: bullet_sound.play() bullets[normal_bullet_index].reset(me.rect.midtop) normal_bullet_index = ( normal_bullet_index + 1) % NORMAL_BULLET_NUM # 检测子弹是否击中敌机 for bullet in bullets: if bullet.active: bullet.move() screen.blit(bullet.image, bullet.rect) enmeies_hit = pygame.sprite.spritecollide( bullet, enemies, False, pygame.sprite.collide_mask) if enmeies_hit: bullet.active = False for enemy in enmeies_hit: if isinstance(enemy, Middle_Enemy) or isinstance(enemy, Big_Enemy): enemy.hit = True enemy.energy -= 1 if enemy.energy == 0: enemy.active = False else: enemy.active = False # 绘制支援物资 if supply.active: supply.move() screen.blit(supply.image, supply.rect) if pygame.sprite.collide_mask(supply, me): supply.active = False if supply is bomb_supply: get_bomb_sound.play() if bomb_num < 3: bomb_num += 1 elif supply is bullet_supply: get_bullet_sound.play() is_super_bullet = True # 超级子弹持续时间18秒 pygame.time.set_timer(SUPER_BULLET_TIME, 18 * 1000) # 绘制敌方大型飞机 for enemy in big_enemies: if enemy.active: enemy.move() if enemy.hit: # 绘制被打到的特效 screen.blit(enemy.image_hit, enemy.rect) enemy.hit = False else: if switch_image: screen.blit(enemy.image_1, enemy.rect) else: screen.blit(enemy.image_2, enemy.rect) # 绘制血槽 pygame.draw.line(screen, BLACK, (enemy.rect.left, enemy.rect.top - 5), (enemy.rect.right, enemy.rect.top - 5), 2) # 当生命大于20%显示绿色,否则显示红色 energy_remain = enemy.energy / Big_Enemy.energy right_x = enemy.rect.left + enemy.rect.width * energy_remain if energy_remain > 0.2: pygame.draw.line(screen, GREEN, (enemy.rect.left, enemy.rect.top - 5), (right_x, enemy.rect.top - 5), 2) else: pygame.draw.line(screen, RED, (enemy.rect.left, enemy.rect.top - 5), (right_x, enemy.rect.top - 5), 2) if enemy.rect.bottom == -50: # 即将出现在画面中,播放音效 enemy3_flying_sound.play(-1) elif enemy.rect.top >= height: # 消失在画面中,停止播放音效 enemy3_flying_sound.stop() else: # 毁灭 if delay % 3 == 0: if e3_destroy_index == 0: enemy3_down_sound.play() screen.blit(enemy.destroy_images[ e3_destroy_index], enemy.rect) e3_destroy_index = (e3_destroy_index + 1) % 6 if e3_destroy_index == 0: score += enemy.score enemy3_flying_sound.stop() enemy.reset() # 绘制敌方中型飞机 for enemy in middle_enemies: if enemy.active: enemy.move() if enemy.hit: # 绘制被打到的特效 screen.blit(enemy.image_hit, enemy.rect) enemy.hit = False else: screen.blit(enemy.image, enemy.rect) # 绘制血槽 pygame.draw.line(screen, BLACK, (enemy.rect.left, enemy.rect.top - 5), (enemy.rect.right, enemy.rect.top - 5), 2) # 当生命大于20%显示绿色,否则显示红色 energy_remain = enemy.energy / Middle_Enemy.energy right_x = enemy.rect.left + enemy.rect.width * energy_remain if energy_remain > 0.2: pygame.draw.line(screen, GREEN, (enemy.rect.left, enemy.rect.top - 5), (right_x, enemy.rect.top - 5), 2) else: pygame.draw.line(screen, RED, (enemy.rect.left, enemy.rect.top - 5), (right_x, enemy.rect.top - 5), 2) else: # 毁灭 if delay % 3 == 0: if e2_destroy_index == 0: enemy2_down_sound.play() screen.blit(enemy.destroy_images[ e2_destroy_index], enemy.rect) e2_destroy_index = (e2_destroy_index + 1) % 4 if e2_destroy_index == 0: score += enemy.score enemy.reset() # 绘制敌方小型飞机 for enemy in small_enemies: if enemy.active: enemy.move() screen.blit(enemy.image, enemy.rect) else: # 毁灭 if delay % 3 == 0: if e1_destroy_index == 0: enemy1_down_sound.play() screen.blit(enemy.destroy_images[ e1_destroy_index], enemy.rect) e1_destroy_index = (e1_destroy_index + 1) % 4 if e1_destroy_index == 0: score += enemy.score enemy.reset() # 检测我方飞机是否被撞 enemies_down = pygame.sprite.spritecollide( me, enemies, False, pygame.sprite.collide_mask) if enemies_down and not me.is_invincible: me.active = False for enemy in enemies_down: enemy.active = False # 绘制我方飞机 if me.active: if switch_image: screen.blit(me.image_1, me.rect) else: screen.blit(me.image_2, me.rect) else: # 毁灭 if delay % 3 == 0: screen.blit(me.destroy_images[me_destroy_index], me.rect) me_destroy_index = (me_destroy_index + 1) % 4 if me_destroy_index == 0: me_down_sound.play() me.reset() pygame.time.set_timer(INVINCIBLE_TIME, 3 * 1000) # 绘制全屏炸弹数量 bomb_text = bomb_font.render(f'× {bomb_num}', True, WHITE) bomb_text_rect = bomb_text.get_rect() screen.blit(bomb, (10, height - bomb_rect.height - 10)) screen.blit(bomb_text, (bomb_rect.width + 20, height - bomb_text_rect.height - 5)) # 绘制剩余生命数量 for i in range(me.life_num): me.life_rect.right = width - i * me.life_rect.width - 5 screen.blit(me.life_image, me.life_rect) # 绘制分数 score_text = score_font.render(f'Score: {score}', True, WHITE) screen.blit(score_text, (10, 5)) # 切换图片 if delay % 5 == 0: switch_image = not switch_image delay -= 1 if not delay: delay = 100 if not me.life_num: # 游戏结束时 if not gameover_processed: # 执行一次以下操作:关闭所有音效和定时器,获取最高分,设置结束界面图像位置 me_down_sound.play() pygame.time.delay(1000) gameover_processed = True pygame.mixer.music.stop() pygame.mixer.stop() pygame.time.set_timer(SUPPLY_TIME, 0) best_record = load_record() if score > best_record: best_record = score save_record(score) best_record_text = record_font.render(f'Best : {best_record}', True, WHITE) best_record_rect = best_record_text.get_rect() best_record_rect.left, best_record_rect.top = (5, 5) record_text_1 = record_font.render('Your Score', True, WHITE) record_rect_1 = record_text_1.get_rect() record_rect_1.left, record_rect_1.top = ( width - record_rect_1.width) // 2, height // 3 record_text_2 = record_font.render(f'{score}', True, WHITE) record_rect_2 = record_text_2.get_rect() record_rect_2.left, record_rect_2.top = (width - record_rect_2.width) // 2, record_rect_1.bottom + 10 again_rect.left, again_rect.top = ( width - again_rect.width) // 2, record_rect_2.bottom + 10 gameover_rect.left, gameover_rect.top = ( width - gameover_rect.width) // 2, again_rect.bottom + 10 # 绘制游戏结束界面 screen.blit(best_record_text, best_record_rect) screen.blit(record_text_1, record_rect_1) screen.blit(record_text_2, record_rect_2) screen.blit(again_image, again_rect) screen.blit(gameover_image, gameover_rect) # 检测用户点击的按钮 if pygame.mouse.get_pressed()[0]: pos = pygame.mouse.get_pos() if again_rect.collidepoint(pos): return True elif gameover_rect.collidepoint(pos): return False else: # 绘制暂停按钮 screen.blit(pause_images[paused], pause_rect) pygame.display.flip() clock.tick(60)