Pygame开发Space Invaders:飞船方向键移动异常问题求助
问题:飞船按住方向键无法平滑移动
我使用PyCharm(Community Edition 2022.3.2)和Pygame开发Space Invaders,编写了飞船左右移动的代码,但出现以下异常:
- 按住左右方向键时飞船无响应,仅在按住方向键同时移动游戏窗口内的鼠标时才会移动
- 尝试用
pygame.event.set_blocked(pygame.MOUSEMOTION)禁用鼠标检测后,按住方向键飞船完全不动 - 只有快速点按方向键时飞船才会小幅移动,预期效果是按住方向键实现平滑移动
原代码如下:
import pygame #Initialize the pygame pygame.init() #CreateAccording the screen (width by height) #Top side and left sideContrast of screen is 0 screen = pygame.display.set_mode((800, 600)) #Title and icon pygame.display.set_caption("Space Invaders") icon = pygame.image.load('ufo.png') pygame.display.set_icon(icon) #Player playerImg = pygame.image.load('spaceship.png') playerX = 380 playerY = 480 playerX_change = 0 #Blit means to draw def player(x,y): screen.blit(playerImg, (x,y)) #Game loop #Keeps game running, user clicks x to close out running = True while running: # Values of RGB # screen is drawn first, then draw player screen.fill((0, 0, 128)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False #pygame.event.set_blocked(pygame.MOUSEMOTION) #If keystroke is pressed check whether left or right if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: playerX_change = -1 if event.key == pygame.K_RIGHT: playerX_change = 1 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: playerX_change = 0 #Updates playerX value toStraight playerX_change value playerX += playerX_change #Calls function of player player(playerX, playerY) #Updates display score changing, bullets moving... pygame.display.update()
解决方案
问题核心是缩进错误:你把飞船位置更新、绘制飞船、刷新显示的代码放在了事件循环(for event in pygame.event.get())内部,导致这些操作仅在有事件触发时才执行(比如按键按下/抬起、鼠标移动),没有事件时就不会更新,自然无法平滑移动。
只需将这些代码移到事件循环的外部,让每一次游戏主循环都执行这些操作即可:
import pygame #Initialize the pygame pygame.init() #Create screen (width by height) screen = pygame.display.set_mode((800, 600)) #Title and icon pygame.display.set_caption("Space Invaders") icon = pygame.image.load('ufo.png') pygame.display.set_icon(icon) #Player playerImg = pygame.image.load('spaceship.png') playerX = 380 playerY = 480 playerX_change = 0 #Blit means to draw def player(x,y): screen.blit(playerImg, (x,y)) #Game loop running = True while running: # Fill screen with RGB color (draw screen first) screen.fill((0, 0, 128)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Handle keystroke events if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: playerX_change = -1 if event.key == pygame.K_RIGHT: playerX_change = 1 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: playerX_change = 0 # --- 移到事件循环外部的代码 --- # Update player position playerX += playerX_change # Keep player within screen bounds (可选优化,防止飞出屏幕) if playerX <= 0: playerX = 0 elif playerX >= 736: # 800 - 64(假设飞船宽度为64) playerX = 736 # Draw player player(playerX, playerY) # Update display pygame.display.update()
额外优化:添加了屏幕边界限制,防止飞船飞出屏幕范围。
内容的提问来源于stack exchange,提问作者Muneeb
相关产品推荐
相关产品推荐

