Pygame游戏结束界面异常触发问题求助
Pygame游戏结束界面闪烁问题修复
问题描述
开发的Pygame游戏中,红色方块从屏幕顶部下落,玩家操控白色方块躲避。设定白色方块与红色方块碰撞时显示游戏结束界面,但该界面有时会在非触发时机闪烁,期望仅在碰撞发生时触发游戏结束界面。
问题原因
代码中绘制游戏结束消息和更新屏幕的代码位于主循环最外层,无论game_over状态是否为True,每帧都会执行这段代码。在游戏正常运行时,这段代码会先绘制游戏结束消息,随后被游戏界面的绘制内容覆盖,导致视觉上的闪烁现象。
修复后的完整代码
import pygame import random # Initialize Pygame pygame.init() # Set the screen size screen_width = 640 screen_height = 480 screen = pygame.display.set_mode((screen_width, screen_height)) # Set the background color background_color = (0, 0, 0) screen.fill(background_color) # Set the character size and position character_size = 50 character_x = screen_width / 2 - character_size / 2 character_y = screen_height / 2 - character_size / 2 # Set the character movement speed move_speed = 5 # Set the enemy size and speed enemy_size = 30 enemy_speed = 3 # Set the list of enemies enemies = [] # Set the font for the game over screen font = pygame.font.SysFont("Arial", 48) # Set the clock to limit the framerate clock = pygame.time.Clock() # Game loop running = True game_over = False while running: # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if not game_over: # Handle key presses keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: character_x -= move_speed elif keys[pygame.K_RIGHT]: character_x += move_speed elif keys[pygame.K_UP]: character_y -= move_speed elif keys[pygame.K_DOWN]: character_y += move_speed # Fill the screen with the background color screen.fill(background_color) # Draw the character white_color = (255, 255, 255) character_rect = pygame.Rect(character_x, character_y, character_size, character_size) pygame.draw.rect(screen, white_color, character_rect) # Draw a border around the character border_color = (0, 0, 0) border_thickness = 2 border_rect = character_rect.inflate(border_thickness * 2, border_thickness * 2) pygame.draw.rect(screen, border_color, border_rect, border_thickness) # Spawn new enemies randomly at the top of the screen if random.random() < 0.05: # Set the enemy position randomly at the top of the screen enemy_x = random.randint(0, screen_width - enemy_size) enemy_y = 0 # Add the enemy to the list of enemies enemies.append(pygame.Rect(enemy_x, enemy_y, enemy_size, enemy_size)) # Move the enemies and check for collisions with the character for enemy in enemies: # Move the enemy enemy.move_ip(0, enemy_speed) # Draw the enemy red_color = (255, 0, 0) pygame.draw.rect(screen, red_color, enemy) # Check for collisions with the character if enemy.colliderect(character_rect): game_over = True # Remove enemies that have gone off the bottom of the screen enemies = [enemy for enemy in enemies if enemy.top < screen_height] else: # Fill the screen with the background color screen.fill(background_color) # Display the game over message game_over_message = font.render("Game Over", True, (255, 255, 255)) message_rect = game_over_message.get_rect(center=(screen_width/2, screen_height/2)) screen.blit(game_over_message, message_rect) # Update the screen once per frame pygame.display.update() # Limit the framerate to 60 FPS clock.tick(60)
关键修复点
- 将游戏结束消息的绘制代码移至
else代码块内部,仅当game_over为True时才执行绘制逻辑 - 将
pygame.display.update()统一放在主循环末尾,确保每帧仅更新一次屏幕,避免多次更新导致的画面冲突
内容的提问来源于stack exchange,提问作者CodingFrog
相关产品推荐
相关产品推荐

