MacOSX中Pygame键盘移动图片功能失效求助
Hey there! Let's sort out why your directional key movement isn't working in Pygame on MacOS. I took a look at your code and found two main issues that are blocking the functionality, plus some tweaks to make things smoother:
Key Issues in Your Code
Out-of-order rendering & duplicate screen updates
You're callingpygame.display.update()right after filling the screen with white, which shows a blank screen before you even draw the player. Then you draw the player and update again—this causes unnecessary flickering and can lead to rendering sync issues on MacOS, making it look like movement isn't happening even if the coordinates are changing.Unreliable key press handling
Usingpygame.key.set_repeat()withKEYDOWNevents can be hit-or-miss on MacOS due to system-level keyboard settings and Pygame's compatibility. This approach often leads to delayed or non-responsive key repeats when holding down direction keys.
Corrected Code
Here's your code with fixes applied, plus some quality-of-life improvements:
import pygame, sys from pygame.locals import * pygame.init() display_width = 800 display_height = 600 gameDisplay = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption('Game') # Color definitions black = (0,0,0) white = (255,255,255) red = (255,0,0) green = (0,255,0) blue = (0,0,255) FPS = 60 fpsClock = pygame.time.Clock() dead = False # Load player image with transparency support and error handling try: player_img = pygame.image.load('images/player.png').convert_alpha() except pygame.error: print("Error: Couldn't load player image! Double-check the file path.") pygame.quit() sys.exit() def player(x, y): gameDisplay.blit(player_img, (x, y)) squirtx = display_width * 0.45 squirty = display_height * 0.8 while not dead: # Handle non-movement events only for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() # Detect continuous key presses for smooth movement keys = pygame.key.get_pressed() move_speed = 5 # Adjust this value to control how fast the player moves # Add boundary checks to keep player on screen if keys[pygame.K_UP] and squirty > 0: squirty -= move_speed if keys[pygame.K_DOWN] and squirty < display_height - player_img.get_height(): squirty += move_speed if keys[pygame.K_RIGHT] and squirtx < display_width - player_img.get_width(): squirtx += move_speed if keys[pygame.K_LEFT] and squirtx > 0: squirtx -= move_speed # Proper rendering flow: Clear screen → Draw elements → Update once gameDisplay.fill(white) player(squirtx, squirty) pygame.display.update() fpsClock.tick(FPS) pygame.quit() sys.exit()
What Changed & Why
- Fixed rendering order: Now we first clear the screen, draw the player, then update the display once. This ensures all drawn elements show up correctly without flickering or sync issues.
- Switched to
pygame.key.get_pressed(): This method checks the current state of all keys, so holding down a direction key will trigger continuous movement reliably—no more relying on finicky key repeat settings. - Added boundary checks: Prevents the player from moving off-screen, which makes the game feel more polished.
- Improved image loading: Using
convert_alpha()optimizes rendering for images with transparency, and the try/except block helps catch issues if your image path is wrong (a common hidden problem!). - Removed
pygame.key.set_repeat(): No longer needed with the new key press detection method.
Give this code a try—your player should move smoothly with the direction keys now!
内容的提问来源于stack exchange,提问作者João Pedro Oliveira

