-1

Okay so I'd like to make a circle move in an elliptical pattern with a button in tkinter. I previously made it move back and forth 10px at a time but I have no idea how to make it go in an ellipse

My back and forth code looks like this:

from Tkinter import *

def ball(gd, hb):
    global x1, y1
    x1, y1 = x1+gd, y1+hb
    can1.coords(oval1,x1, y1, x1+30, y1+30)

def move():
    global direction
    if x1 + 30 == 250:
        direction = -1
    elif x1 == 0:
        direction = 1
    ball(direction*10, 0)

x1 , y1, direction = 0, 125, 1

root = Tk()

can1 = Canvas(root,height = 250, width =250, bg = 'black')
oval1= can1.create_oval(x1,y1,x1+30,y1+30, width=2, fill='orange')
can1.pack()
Button(root, text ='Move Ball', command = move).pack()

root.mainloop()

Any ideas would help me, I just need to be pointed in the right direction

1 Answers1

0

Option 1

Okay so if you have meant that you just want to have an elliptical shape all the time you can use x1,y1,x1+60,y1+30 instead of x1+30,y1+30.


Option 2

If you have meant to reshape the ball like the ball bounces you have to think about some factors.

  • When should it happen. => Direction change
  • What do I have to modify => based on that: how a oval shape is defined you can easily say that if the ball touches the max width ( in your case x = 250) that you have to stretch out the opposite coordinates (oval1,x1, y1, x1+60, y1+30), with y1 ignored because you are just moving on the x-axis.
  • How do I stretch => See my Image

    It also shows that you may use the sinus to achieve what you want. Like adding him on top of the desired coordinates. So everywhere the "Point" should move faster adding the sinus on top might does the trick.

    enter image description here

    That was my idea. Just a quick one. Maybe tomorrow I will find a better one. If you have questions just comment.

  • Sens4
    • 605
    • 1
    • 11
    • 29