You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Turtle的谷歌恐龙游戏计分板每秒加100分更新问题求助

Fixing Continuous Score Updates for Your Turtle Google Dino Game

Hey Shay, no stress—let’s get that scoreboard working properly for your final project! Since you can’t share your full code, I’ll break down the most reliable ways to make your score tick up by 100 points every second without breaking your game flow.

Key Concepts to Fix Your Scoreboard

Here’s what you need to focus on:

  • Use a Dedicated Score Turtle: Have a separate Turtle object just for displaying the score—this keeps your drawing logic clean.
  • Avoid Blocking Sleep: Forget time.sleep(1)—it’ll freeze your entire game. Instead, use Turtle’s built-in ontimer() method for non-blocking updates.
  • Clear and Rewrite: Always clear the old score text before writing the new one, or you’ll end up with overlapping numbers.

Example Code Snippets

Let’s put this into practice:

1. Set Up Your Score Turtle First

Add this early in your code to create the score display:

import turtle

# Initialize score variable and display turtle
score = 0
score_display = turtle.Turtle()
score_display.hideturtle()
score_display.penup()
score_display.goto(200, 250)  # Position it in the top-right corner
score_display.write(f"Score: {score}", align="center", font=("Courier", 22, "bold"))

2. Create the Score Update Function

This function will increment the score, refresh the display, and schedule itself to run again in 1 second:

def update_score():
    global score
    # Add 100 points
    score += 100
    # Clear the old score text
    score_display.clear()
    # Write the new score
    score_display.write(f"Score: {score}", align="center", font=("Courier", 22, "bold"))
    # Schedule this function to run again in 1000ms (1 second)
    score_display.getscreen().ontimer(update_score, 1000)

3. Start the Score Loop

Right before your main game loop (like turtle.mainloop() or your custom game loop), call the update function once to kick things off:

# Start the score update cycle
update_score()

# Launch your game
turtle.mainloop()

Common Pitfalls to Avoid

  • Global Variable Scope: If you’re using a class-based structure, make sure score is accessible to the update function (either as an instance variable or passed correctly).
  • Overlapping Text: Always call clear() on the score turtle before writing the new score—this prevents messy overlapping text.
  • Blocking the Game: time.sleep() will stop all other game actions (like dinosaur movement or collision checks) while it waits. ontimer() runs in the background, so your game stays responsive.

Give these steps a shot—if you run into issues with specific parts of your existing code (even small snippets), that’ll help refine the fix further. You’ve got this, and good luck with your final project!

内容的提问来源于stack exchange,提问作者shay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 17:12:38