Im trying to create a egg catching game and the image is not moving when i clicked the arrow keys i tried using this code and it's not working
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()
CodePudding user response:
Your code throws errors which tell you exactly what the problem is. c.coords(catcher)
returns only a single x,y pair rather than two pairs. When I replace that code with c.bbox(catcher)
then the image moves when I press the arrow keys.