Pygame打地鼠游戏:实现地鼠在指定坐标随机出现求助
Hey there! Let's fix up your whack-a-mole game so those moles pop up randomly at your preset coordinates, plus tidy up some other quirks in your code. Here's the revised version with explanations below:
import pygame import random pygame.init() display_width = 600 display_height = 480 gameDisplay = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption('Tape Taupe') clock = pygame.time.Clock() # Load all images once (optimized!) fond = pygame.image.load("background.jpg").convert() BAMimg = pygame.image.load('Marteau.png') gameIcon = pygame.image.load('Taupe.png').convert_alpha() pygame.display.set_icon(gameIcon) mole_img = pygame.image.load("Taupe.png").convert_alpha() hole_img = pygame.image.load("troutaup.png").convert_alpha() # Preset mole hole coordinates (all your original positions) mole_holes = [ (160, 55), (320, 55), (480, 55), (160, 200), (320, 200), (480, 200), (160, 350), (320, 350), (480, 350) ] current_mole_pos = None # Track where the current mole is MOLE_SPAWN_EVENT = pygame.USEREVENT + 1 pygame.time.set_timer(MOLE_SPAWN_EVENT, 1500) # Spawn mole every 1.5 seconds def draw_hammer(x, y): gameDisplay.blit(BAMimg, (x, y)) def game_loop(): global current_mole_pos # Hammer starting position hammer_x = (display_width * 0.45) hammer_y = (display_height * 0.8) x_change = 0 y_change = 0 gameExit = False while not gameExit: for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True # Handle hammer movement if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = -5 elif event.key == pygame.K_RIGHT: x_change = 5 elif event.key == pygame.K_UP: y_change = -5 elif event.key == pygame.K_DOWN: y_change = 5 if event.type == pygame.KEYUP: if event.key in (pygame.K_LEFT, pygame.K_RIGHT): x_change = 0 elif event.key in (pygame.K_UP, pygame.K_DOWN): y_change = 0 # Handle mole spawning if event.type == MOLE_SPAWN_EVENT: # Pick a random hole from our preset list current_mole_pos = random.choice(mole_holes) # Update hammer position hammer_x += x_change hammer_y += y_change # Keep hammer within screen bounds (optional but nice!) hammer_x = max(0, min(hammer_x, display_width - BAMimg.get_width())) hammer_y = max(0, min(hammer_y, display_height - BAMimg.get_height())) # Draw everything in the correct order gameDisplay.blit(fond, (0, 0)) # Draw all holes first for hole_pos in mole_holes: gameDisplay.blit(hole_img, hole_pos) # Draw the mole if it's spawned if current_mole_pos: gameDisplay.blit(mole_img, current_mole_pos) # Draw hammer on top draw_hammer(hammer_x, hammer_y) pygame.display.update() clock.tick(60) pygame.quit() quit() game_loop()
Key Fixes & Additions
- Centralized Mole Hole Coordinates: I created a
mole_holeslist to store all your preset (x,y) positions. This makes it trivial to pick a random hole usingrandom.choice()whenever we want a mole to spawn. - Optimized Image Loading: No more loading the same hole/mole image multiple times—we load each asset once and reuse it, which saves memory and improves performance.
- Automatic Mole Spawning: Added a custom Pygame event (
MOLE_SPAWN_EVENT) that triggers every 1.5 seconds. When this event fires, we pick a random hole from our list and set that as the current mole position. - Fixed Drawing Order: Your original code drew the hammer before the background, which made it disappear. Now we draw background → holes → mole → hammer, so all elements display correctly.
- Cleaned Up Movement Logic: Combined duplicate key event checks into cleaner
elifblocks, and added bounds checking so the hammer can't go off-screen. - Removed Unused Code: Got rid of the redundant
Taupe1-Taupe6variables (if you want to add mole animations later, we can expand this to use a list of animation frames).
If you want to add whack detection (checking if the hammer hits the mole), you could add a mouse click event handler that checks if the click position overlaps with current_mole_pos—let me know if you want help with that too!
内容的提问来源于stack exchange,提问作者Christo Hardmn
相关产品推荐
相关产品推荐

