【问题标题】:GRAV variable not working?GRAV 变量不起作用?
【发布时间】:2018-05-02 20:09:52
【问题描述】:

此程序无法正常工作,我已尝试更改我认为错误的任何内容。我正在尝试简单地制作一个重力起作用的游戏,但不知道我是否需要更改所有内容,或者只需将两个引号换成两个撇号

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 还很陌生。任何帮助都会很棒!

【问题讨论】:

  • 出了什么问题?你有错误吗?它是否以您不期望的方式执行?如果是这样,您希望它如何工作?这里的人们乐于提供帮助,但您必须向我们提供足够的信息才能找出问题所在。
  • GRAV =+ 1 和 GRAV =-2 分别将变量设置为常量值 1 和 -2。你可能想要+=,-= 这里。
  • “不工作”是什么意思?程序会崩溃吗?重力是不是太强了?太弱?还有什么?
  • 程序运行了,但是我昵称为“dude”的精灵没有下降,也没有上升,也没有左右移动。抱歉信息不足,我对这个网站很陌生

标签: python python-3.x tkinter


【解决方案1】:

我不认为这是单行修复,因为您的代码存在多个与 tkinter 相关的问题,而不是您的重力问题:

  • 什么是self.rect.colliderect(),PyGame 工件?

  • c.bind_all('&lt;key&gt;', move_dude) 应该使用&lt;Key&gt;

  • move_dude() 似乎是避免声明的内部函数 Q_PRESSED全球。

  • 没有呼叫mainloop()。

我对你的代码进行了最小程度的修改,以展示我相信你试图实现的运动:

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()

【讨论】:

  • 未来参考的问题:c.find_overlapping(*c.bbox(floor)) 中的返回花花公子是否返回真/假?我可能会再次尝试 codeacademy,因为我不了解 python 的万亿分之一,而且不包括所有插件(pygame、time 等)。谢谢
  • 返回 c.find_overlapping(c.bbox(floor)) 中的老兄
  • 是的,@Rauligio,关键操作是in,它返回一个布尔值——然后该布尔结果作为函数的值返回。
猜你喜欢
  • 1970-01-01
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 2018-10-20
  • 2018-12-28
  • 2018-04-22
相关资源
最近更新 更多