0

I am writing a program for an exercise that simulates a projectile launch while tracking down every x and y position per second.

While the program technically works, and it does track down every x and y position, the text that tracks down the x and y position flickers for each step.

I understand the problem is that I am using the .clear() function (which, as its name implies, clears the text generated by the turtle). However, I believe that the program only functions properly with it.

Here is my code:

"""Program to track specific location of turtle (for every step)"""

from turtle import *
from math import *

def cball_graphics():

    leonardo = Turtle()
    leonardo.color("dark blue")
    leonardo.shape("turtle")
    leonardo.speed(1)

    return leonardo

def show_position():

    pos = Turtle()
    pos.color("white")
    pos.goto(30, -50)
    pos.color("red")

    return pos

class cannon_ball:

    def __init__(self, angle, vel, height, time):

        self.x_pos = 0
        self.y_pos = height
        theta = pi * angle / 180
        self.x_vel = vel * cos(theta)
        self.y_vel = vel * sin(theta)
        self.time = time

    def update_time(self):

        self.x_pos += self.time * self.x_vel
        y_vel1 = self.y_vel - 9.8 * self.time
        self.y_pos += self.time * (self.y_vel + y_vel1) / 2
        self.y_vel = y_vel1

    def get_x(self):

        return self.x_pos

    def get_y(self):

        return self.y_pos

def variables():

    angle = 55
    vel = 10
    height = 100
    time = .01

    return cannon_ball(angle, vel, height, time)

def main():

    leonardo = cball_graphics()

    """pos is a variable that writes the position on the screen using x and y pos"""
    pos = show_position()
    pos.hideturtle()
    projectile = variables()

    while projectile.y_pos >= 0:

        pos.write(f"{'%0.0f' % projectile.x_pos}, {'%0.0f' % projectile.y_pos}")

        projectile.update_time()
        leonardo.goto(projectile.x_pos, projectile.y_pos)

        pos.clear()

main()