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

GRAV变量无法正常工作:Tkinter简易重力游戏代码故障求助

Hey there! Let's work through your gravity game issue step by step. You mentioned your Tkinter-based game won't run properly, even after tweaking parts you thought were problematic, and you're unsure if you need a full rewrite or just quote adjustments.

First, let's look at the code snippet you shared (note it's truncated, which is one immediate issue):

from tkinter import * 
HEIGHT = 500 
WIDTH = 800 
GRAV = 0 
Q_PRESSED = False 

# to make the window 
window = Tk() 
window.title("jumpy jump") 
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='white') 
c.pack() 

# to create the sprite 
dude_id = c.create_rectangle(0, 30, 15, 100...

Let's break down the key problems here:

  • Truncated code: The create_rectangle call is incomplete—you haven't closed the parentheses or finished the arguments (like a fill color). This will throw a syntax error right off the bat.
  • Gravity set to 0: You defined GRAV = 0, which means no downward force will ever be applied to your sprite. To get a falling effect, set this to a positive value like 0.5 or 1.
  • Missing game loop & logic: Tkinter needs a way to continuously update the sprite's position and handle input. You have a Q_PRESSED flag but no code to detect key presses or update the sprite's location over time.
  • Quotes aren't the issue: In Python, single and double quotes work interchangeably as long as they're properly paired. Your current quote usage (like bg='white') is totally fine—this isn't what's breaking your game.

Here's a fixed, working version of your game to reference:

from tkinter import *

HEIGHT = 500
WIDTH = 800
GRAV = 0.5  # Give gravity a functional value
Q_PRESSED = False

def update_game():
    global Q_PRESSED
    # Exit if Q is pressed
    if Q_PRESSED:
        window.quit()
    
    # Get current sprite coordinates
    x1, y1, x2, y2 = c.coords(dude_id)
    # Apply gravity to move the sprite down
    new_y1 = y1 + GRAV
    new_y2 = y2 + GRAV
    
    # Stop sprite from falling off the bottom of the screen
    if new_y2 > HEIGHT:
        new_y1 = HEIGHT - (y2 - y1)
        new_y2 = HEIGHT
    
    # Update the sprite's position
    c.coords(dude_id, x1, new_y1, x2, new_y2)
    
    # Repeat this update every 10ms to create animation
    window.after(10, update_game)

def on_key_press(event):
    global Q_PRESSED
    if event.keysym.lower() == 'q':
        Q_PRESSED = True

# Set up the window
window = Tk()
window.title("jumpy jump")
c = Canvas(window, width=WIDTH, height=HEIGHT, bg='white')
c.pack()

# Create the sprite (complete the create_rectangle call)
dude_id = c.create_rectangle(0, 30, 15, 100, fill='darkgreen')

# Bind key press events to our handler
window.bind('<KeyPress>', on_key_press)

# Start the game update loop
update_game()

# Launch the main Tkinter loop
window.mainloop()

What I fixed:

  1. Completed the create_rectangle call with a fill color and closed parentheses
  2. Set GRAV to 0.5 so gravity actually affects the sprite
  3. Added an update_game function that handles gravity, boundary collision, and continuous position updates using window.after()
  4. Added a key press handler to detect when Q is pressed and trigger a quit
  5. Connected all the pieces to create a functional game loop

This should get your game running properly—no full rewrite needed, just filling in the missing logic and fixing those small syntax issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:32:05