Mac环境下Pygame脚本运行异常问题求助
Hey there, let's break down and solve each of your Pygame problems step by step—since you said the script worked before moving files, we can get it back to functioning normally in no time!
1. Terminal Error: AttributeError: module 'pygame' has no attribute 'init'
This is almost always caused by one of two things: a naming conflict or a misaligned Python environment. Here's how to fix it:
- Check for naming conflicts: Make sure there's no file named
pygame.pyor folder namedpygamein the directory where you're running the script from the terminal. These would override the actual Pygame library. If you find any, rename or delete them. - Match your Anaconda environment: The terminal might be using a different Python installation than Spyder. Run
which pythonin the terminal—if the path doesn't point to your Anaconda environment (e.g.,/Users/yourname/anaconda3/bin/python), activate your Anaconda base environment first withconda activate base(or your specific environment name), then run your script again. - Reinstall Pygame for Anaconda: If the above doesn't work, uninstall Pygame with
pip uninstall pygame, then install it via Conda (which is more compatible with Anaconda environments) usingconda install -c cogsci pygame.
2. Spyder: Keys Don't Control Objects (Cursor Stays in Spyder)
Spyder's IPython console can sometimes keep focus away from the Pygame window. Try these fixes:
- Adjust Spyder's graphics backend: Go to
Tools > Preferences > IPython Console > Graphicsand set the backend to "Automatic" or "Qt5". Restart Spyder after changing this. - Give the Pygame window focus: After running the script, click directly on the Pygame game window—this ensures keyboard events are sent to the window instead of Spyder's console.
- Double-check working directory: You already set Spyder's working directory to
/users/himansu, which matches your image path, so that part is good!
3. Image Loading Path Issue (Script on Desktop, Image Needs to Be in /users/himansu)
You don't have to rely on a fixed directory—make the script load images from its own folder, so you can keep the script and image together anywhere:
Modify the image loading code using os.path to dynamically get the script's directory:
# Get the folder where your script is located script_dir = os.path.dirname(os.path.abspath(__file__)) # Load the image from the same folder as the script carImg = pygame.image.load(os.path.join(script_dir, 'racecar.png'))
Now you can keep racecar.png right next to your script on the desktop, no need to move it to /users/himansu.
Quick Code Fix: Syntax Error
Also, your original code has a tiny syntax mistake on the first line—you combined two imports into one line, which will cause errors. Fix it to:
import pygame import os
Here's your corrected, full script with all the fixes applied:
import pygame import os pygame.init() display_width = 800 display_height = 600 black = (0, 0, 0) white = (255, 255, 255) red = (255, 0, 0) gameDisplay = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption('A bit Racey') clock = pygame.time.Clock() # Load image from the same directory as the script script_dir = os.path.dirname(os.path.abspath(__file__)) carImg = pygame.image.load(os.path.join(script_dir, 'racecar.png')) def car(x, y): gameDisplay.blit(carImg, (x, y)) def game_loop(): x = display_width * 0.45 y = display_height * 0.8 x_change = 0 print(x_change) gameExit = False while not gameExit: # Event handling loop for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = -5 elif event.key == pygame.K_RIGHT: x_change = 5 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: x_change = 0 x += x_change gameDisplay.fill(white) car(x, y) pygame.display.update() clock.tick(60) game_loop() pygame.quit() quit()
内容的提问来源于stack exchange,提问作者Himansu Odedra

