使用graphics.py开发游戏:如何实现计时与窗口点击操作并行?
Fixing Your Click Game with Zelle's Graphics.py
Got it, let's get your game working the way you want! The main problems with your current code are that time.sleep() freezes the entire window (so you can't click during the timer) and you're only showing the click count prompt after the timer runs out. Here's a solution that sticks strictly to using Zelle's graphics.py (plus standard time and random modules—no extra external libraries needed):
The Fixed Code
from graphics import GraphWin, Point, Text from random import randrange import time def main(): win = GraphWin('Click Challenge', 300, 300) win.setBackground("green") # Start screen: wait for user to click to begin start_msg = Text(Point(150, 30), "Click to start the game") start_msg.draw(win) win.getMouse() start_msg.undraw() # Game configuration total_game_time = 60 # 60-second timer as per your rule start_timestamp = time.time() target_clicks = randrange(10, 50) # Adjust this range to make the game easier/harder clicks_made = 0 # Create UI elements once (we'll update their text instead of redrawing) timer_display = Text(Point(150, 30), f"Time Left: {total_game_time}s") timer_display.draw(win) click_prompt = Text(Point(150, 70), f"Need {target_clicks - clicks_made} more clicks!") click_prompt.draw(win) # Main game loop (non-blocking, so clicks and timer work together) game_active = True while game_active: # Calculate remaining time and update the timer elapsed_time = time.time() - start_timestamp remaining_time = max(total_game_time - int(elapsed_time), 0) timer_display.setText(f"Time Left: {remaining_time}s") # Update the click counter prompt click_prompt.setText(f"Need {target_clicks - clicks_made} more clicks!") # Check for mouse clicks (doesn't block the loop) click_location = win.checkMouse() if click_location is not None: clicks_made += 1 # Check win/lose conditions if clicks_made >= target_clicks: # Player won: clean up UI and show message timer_display.undraw() click_prompt.undraw() win_msg = Text(Point(150, 150), "YOU WIN! 🎉") win_msg.draw(win) game_active = False elif remaining_time <= 0: # Player lost: clean up UI and show message timer_display.undraw() click_prompt.undraw() lose_msg = Text(Point(150, 150), "YOU LOST! 😢") lose_msg.draw(win) game_active = False # Small delay to prevent the loop from running too fast (keeps performance smooth) time.sleep(0.01) # Wait for a final click to close the window win.getMouse() win.close() if __name__ == "__main__": main()
Key Changes That Fixed Your Issues
- No more blocking
time.sleep()loops: Instead of freezing the window withsleep(), we track real elapsed time usingtime.time(). This lets the window respond to clicks while the timer counts down. - Reusable UI elements: Instead of undrawing and redrawing text every second, we create the timer and click prompt once, then just update their text with
setText(). This is cleaner and more efficient. - Combined click and timing logic: The main loop handles both updating the timer and checking for clicks in every iteration, so both happen simultaneously—no more waiting for the timer to end before clicking.
- Adjustable difficulty: I toned down the target clicks to 10-50 (200 clicks in 60 seconds is brutal!), but you can change
randrange(10, 50)back torandrange(200)if you want the original challenge. - Immediate win/lose feedback: The game stops as soon as you hit the click target or run out of time, showing the result right away.
内容的提问来源于stack exchange,提问作者omar7439
相关产品推荐
相关产品推荐

