A
A
Alexey Romanov2020-04-23 08:55:11
Python
Alexey Romanov, 2020-04-23 08:55:11

How can I make the circle move further after pressing the space bar?

from tkinter import*
def DrawCircle(x,y,r):
    h.create_oval(x-r,y-r,x+r,y+r,fill='black')

def right(event):
    h.move(1,5,0)
def left(event):
    h.move(1,-5,0)
def up(event):
    h.move(1,0,-5)
def down(event):
    h.move(1,0,5)
def space(event):
    h.delete('all')
    h['bg']='purple'
    h.create_oval(x-r,y-r,x+r,y+r,fill='black')
    
root=Tk()

root.bind('<Right>',right)
root.bind('<Left>',left)
root.bind('<Up>',up)
root.bind('<Down>',down)
root.bind('<space>',space)

h=Canvas(root,width=500,height=500,bg='purple')
x=250 
y=250
r=50
DrawCircle(x,y,r)
h.pack()
root.mainloop()


I can move the ball but after a space it stops responding to pressing the arrows. Help me please. on the arrows, it should move 5 pixels in the indicated direction. when you press the space bar, the ball should return to the starting point.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DUDE, 2020-04-24
@stympel

If my eyes do not fail me at 4 o'clock in the morning, then when I press the spacebar, another circle is created and it no longer has anything to do with events. Here you need to do this:

  1. Create one single circle object (write to a variable for example)
  2. Create variables with window center coordinates
  3. When you press the spacebar, change the circle coordinate values ​​to the window center coordinate values

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question