繁体   English   中英

当我单击箭头键时,create_image 没有水平移动

[英]create_image is not moving horizontally when im clicking arrow key

我正在尝试创建一个抓蛋游戏,当我单击箭头键时图像没有移动我尝试使用此代码但它不起作用

catcher_width = 100
catcher_height = 100
catcher_startx = 800 / 2 - catcher_width / 2
catcher_starty = 500 - catcher_height - 20
catcher_startx2 = catcher_startx + catcher_width
catcher_starty2 = catcher_starty + catcher_height

basket = PhotoImage(file='C:\\Users\Mike\Downloads\\basket.png') 
catcher = c.create_image(catcher_startx, catcher_starty,  image=basket)

def move_left(event):
    (x1, y1, x2, y2) = c.coords(catcher)
    if x1 > 0:
        c.move(catcher, -20, 0)

def move_right(event):
    (x1, y1, x2, y2) = c.coords(catcher)
    if x2 < 800:
        c.move(catcher, 20, 0)

c.bind("<Left>", move_left)
c.bind("<Right>", move_right)

c.focus_set()
root.mainloop()

您的代码会抛出错误,这些错误会准确地告诉您问题出在哪里。 c.coords(catcher)仅返回一个 x,y 对而不是两个对。 当我用c.bbox(catcher)替换该代码时,当我按下箭头键时图像会移动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM