|
| 1 | +from turtle import * # Import necessary modules: turtle and csv. |
| 2 | +import turtle |
| 3 | +import csv |
| 4 | + |
| 5 | +ws = turtle.Screen() # Set up the turtle screen. |
| 6 | +width = 1000 |
| 7 | +height = 600 |
| 8 | +ws.setup(width, height) |
| 9 | +ws.tracer(1) |
| 10 | + |
| 11 | +gravity = turtle.textinput("Gravity", "Enter gravity (1-5 works best)") # Set gravity using user input. |
| 12 | +velocity = turtle.textinput("Velocity", f"Gravity: {gravity} \nEnter Velocity (y, 1-20 works best)") # Set y-velocity using user input. |
| 13 | +xvelocity = turtle.textinput("Velocity", f"Gravity: {gravity}\nY Velocity: {velocity} \nEnter Velocity (x, 1-20 works best)") # Set x-velocity using user input. |
| 14 | + |
| 15 | +original_velocity = velocity # Store the original velocity value |
| 16 | +original_xvelocity = xvelocity # Store the original xvelocity value |
| 17 | + |
| 18 | +ball = turtle.Turtle() # Set properties of the ball object. |
| 19 | +ball.color("green") |
| 20 | +ball.shape("circle") |
| 21 | +ball.penup() |
| 22 | +energy_loss = 0.8 # Set energy loss value. |
| 23 | +x_energy_loss = 0.95 # Set x-energy loss value. |
| 24 | +cut_off = 0.001 # Set cutoff value. |
| 25 | +cor = [] # Create an empty list to store x and y coordinates. |
| 26 | + |
| 27 | +myFile = open("ballpositions.csv", 'w', newline="") # Open a CSV file to write ball positions. |
| 28 | +writer = csv.writer(myFile) |
| 29 | +writer.writerow(['X', 'Y', "Gravity", "yv", "xv"]) # Write headers to the CSV file. |
| 30 | + |
| 31 | + |
| 32 | +x = 0 # Initialize x variable. |
| 33 | +y = 0 # Initialize y variable. |
| 34 | + |
| 35 | +while True: # Start an infinite loop. |
| 36 | + velocity = float(velocity) - float(gravity) # Update velocity based on gravity. |
| 37 | + |
| 38 | + x += float(xvelocity) # Update x position based on x-velocity. |
| 39 | + y += float(velocity) # Update y position based on velocity. |
| 40 | + |
| 41 | + ball.setx(x) # Move the ball to the updated x position. |
| 42 | + ball.sety(y) # Move the ball to the updated y position. |
| 43 | + |
| 44 | + cor.append(ball.pos()) # Append the current position to the x-coordinate list. |
| 45 | + |
| 46 | + if ball.ycor() < -height / 2 - 20: # Check if the ball is below the screen. |
| 47 | + velocity = int(-velocity + float(gravity)) * energy_loss # Update velocity with energy loss and gravity. |
| 48 | + |
| 49 | + if abs(velocity) < cut_off: # Check if velocity is below cutoff. |
| 50 | + break # Exit the loop if velocity is below cutoff. |
| 51 | + elif abs(velocity) < 25: # Check if velocity is below a certain threshold. |
| 52 | + xvelocity = int(xvelocity) * x_energy_loss # Update x-velocity with energy loss. |
| 53 | + |
| 54 | + if ball.xcor() > width / 2 or ball.xcor() < -width / 2: # Check if the ball is outside the screen horizontally. |
| 55 | + xvelocity = -int(xvelocity) # Reverse the x-velocity if it hits the boundary. |
| 56 | + |
| 57 | + myFile = open("ballpositions.csv", 'a', newline="") # Open the CSV file in append mode. |
| 58 | + writer = csv.writer(myFile) |
| 59 | + #writer.writerow([cor[-1],"", gravity, velocity, xvelocity]) # Write the last recorded position to the CSV file. |
| 60 | + #writer.writerow(cor[-1]) |
| 61 | + xc, yc = ball.pos() |
| 62 | + writer.writerow([xc,yc,gravity, original_velocity, original_xvelocity]) |
| 63 | + print(ball.pos()) # Print the current position of the ball. |
| 64 | + |
| 65 | +turtle.done() # Exit the turtle graphics environment. |
0 commit comments