这个程序不起作用,我试着改变任何我认为错误的东西。我正在尝试简单地制作一个重力起作用的游戏,不知道是否需要更改所有内容,或者只需将两个引号改为两个撇号
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, outline='limegreen',
fill='limegreen')
dude_id2 = c.create_rectangle(45, 30, 60, 100, outline='limegreen',
fill='limegreen')
dude_id3 = c.create_oval(0, 0, 60, 60, outline='limegreen',
fill='limegreen')
def is_collided_with(dude, floor):
return self.rect.colliderect(dude.rect)
MID_X = WIDTH / 2
MID_Y = HEIGHT / 2
c.move(dude_id, MID_X, MID_Y)
c.move(dude_id2, MID_X, MID_Y)
c.move(dude_id3, MID_X, MID_Y)
floor_id = c.create_rectangle(0, 485, 800, 500, fill='black')
while Q_PRESSED != False:
if not dude.is_collided_with(floor):
GRAV =+ 1
c.move(dude_id, 0, GRAV)
c.move(dude_id2, 0, GRAV)
c.move(dude_id3, 0, GRAV)
if dude.is_collided_with(floor):
GRAV =-2
# to move
def move_dude(event):
if event.keysym == 'q':
Q_PRESSED = True
if event.keysym == 'Left':
c.move(dude_id, -10, GRAV)
c.move(dude_id2, -10, GRAV)
c.move(dude_id3, -10, GRAV)
if event.keysym == 'Right':
c.move(dude_id, 10, GRAV)
c.move(dude_id2, 10, GRAV)
c.move(dude_id3, 10, GRAV)
c.bind_all('<key>', move_dude)我在一本书上得到了一些帮助,用于装订键的东西,但它是为孩子或初学者制作的,因为我对python非常陌生。任何帮助都是最好的!
发布于 2018-05-03 05:54:35
我不相信这是一行修复,因为你的代码中有多个问题与tkinter有关,而不一定是你的重力问题:
<Key>
move_dude()似乎是一个内部函数,用来避免声明artifact?
c.bind_all('<key>', move_dude) global.
mainloop().,
self.rect.colliderect(),PyGame Q_PRESSED应该使用它我对您的代码进行了最小限度的修改,以演示我认为您正在尝试实现的运动:
from tkinter import *
HEIGHT = 500
WIDTH = 800
GRAVITY = 0
def is_collided_with(dude, floor):
return dude in c.find_overlapping(*c.bbox(floor))
def move_dude(event):
global GRAVITY
if is_collided_with(dude_id, floor_id):
GRAVITY = 0
else:
GRAVITY += 1
if event.keysym == 'q':
exit()
if event.keysym == 'Left':
c.move(dude_id, -10, GRAVITY)
elif event.keysym == 'Right':
c.move(dude_id, 10, GRAVITY)
# 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, outline='green', fill='green')
c.move(dude_id, WIDTH / 2, HEIGHT / 2)
floor_id = c.create_rectangle(0, 485, 800, 500, fill='black')
c.bind_all('<Key>', move_dude)
window.mainloop()https://stackoverflow.com/questions/50142776
复制相似问题