Home > Software design >  Deformed Circle in Python Turtle Graphics Animation
Deformed Circle in Python Turtle Graphics Animation

Time:09-08

Is there any way to not have the circle be deformed during the animation produced by the code below please? It seems that at anything other than a very slow speed the animation is unusabley distorted.

import turtle

SPEED = 5  # ms between frames?
STEP = 5  # Pixels per "step"


def move():
    global x, y, xdir, ydir

    x = x   xdir
    y = y   ydir

    if x >= right_edge:
        xdir = - xdir
    if x <= left_edge:
        xdir = - xdir
    if y >= top_edge:
        ydir = - ydir
    if y <= bottom_edge:
        ydir = -ydir

    turtle.goto(x, y)
    turtle.ontimer(move, SPEED)


screen = turtle.Screen()
screen.title("Bounce")
screen.bgcolor("pink")
turtle.color("blue")
turtle.shape("circle")
# turtle.shape("triangle")
turtle.penup()

x, y = 0, 0
xdir, ydir = STEP, STEP  # Vectors anyone?
right_edge = turtle.window_width() // 2
left_edge = -turtle.window_width() // 2
top_edge = turtle.window_height() // 2
bottom_edge = -turtle.window_height() // 2

move()

turtle.done()

CodePudding user response:

I find that dot() produces a higher quality circle than either circle() or shape('circle'):

from turtle import Screen, Turtle, Vec2D

SPEED = 5  # ms between frames
STEP = 5  # Pixels per "step"

X, Y = 0, 1  # vector index symbols

position = Vec2D(0, 0)
direction = Vec2D(STEP, STEP)  # Primitive vectors for everyone

def move():
    global position, direction

    position  = direction

    if not left_edge <= position[X] <= right_edge:
        direction = Vec2D(-direction[X], direction[Y])

    if not bottom_edge <= position[Y] <= top_edge:
        direction = Vec2D(direction[X], -direction[Y])

    turtle.clear()
    turtle.goto(position)
    turtle.dot(20)

    screen.update()
    screen.ontimer(move, SPEED)

screen = Screen()
screen.title("Bounce")
screen.bgcolor('pink')
screen.tracer(False)

turtle = Turtle()
turtle.hideturtle()
turtle.color('blue')
turtle.shape('circle')
turtle.penup()

right_edge = screen.window_width() // 2
left_edge = -screen.window_width() // 2
top_edge = screen.window_height() // 2
bottom_edge = -screen.window_height() // 2

move()

screen.mainloop()
  • Related