Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #1

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions bouncingBall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from turtle import * # Import necessary modules: turtle and csv.
import turtle
import csv

ws = turtle.Screen() # Set up the turtle screen.
width = 1000
height = 600
ws.setup(width, height)
ws.tracer(1)

gravity = turtle.textinput("Gravity", "Enter gravity (1-5 works best)") # Set gravity using user input.
velocity = turtle.textinput("Velocity", f"Gravity: {gravity} \nEnter Velocity (y, 1-20 works best)") # Set y-velocity using user input.
xvelocity = turtle.textinput("Velocity", f"Gravity: {gravity}\nY Velocity: {velocity} \nEnter Velocity (x, 1-20 works best)") # Set x-velocity using user input.

original_velocity = velocity # Store the original velocity value
original_xvelocity = xvelocity # Store the original xvelocity value

ball = turtle.Turtle() # Set properties of the ball object.
ball.color("green")
ball.shape("circle")
ball.penup()
energy_loss = 0.8 # Set energy loss value.
x_energy_loss = 0.95 # Set x-energy loss value.
cut_off = 0.001 # Set cutoff value.
cor = [] # Create an empty list to store x and y coordinates.

myFile = open("ballpositions.csv", 'w', newline="") # Open a CSV file to write ball positions.
writer = csv.writer(myFile)
writer.writerow(['X', 'Y', "Gravity", "yv", "xv"]) # Write headers to the CSV file.


x = 0 # Initialize x variable.
y = 0 # Initialize y variable.

while True: # Start an infinite loop.
velocity = float(velocity) - float(gravity) # Update velocity based on gravity.

x += float(xvelocity) # Update x position based on x-velocity.
y += float(velocity) # Update y position based on velocity.

ball.setx(x) # Move the ball to the updated x position.
ball.sety(y) # Move the ball to the updated y position.

cor.append(ball.pos()) # Append the current position to the x-coordinate list.

if ball.ycor() < -height / 2 - 20: # Check if the ball is below the screen.
velocity = int(-velocity + float(gravity)) * energy_loss # Update velocity with energy loss and gravity.

if abs(velocity) < cut_off: # Check if velocity is below cutoff.
break # Exit the loop if velocity is below cutoff.
elif abs(velocity) < 25: # Check if velocity is below a certain threshold.
xvelocity = int(xvelocity) * x_energy_loss # Update x-velocity with energy loss.

if ball.xcor() > width / 2 or ball.xcor() < -width / 2: # Check if the ball is outside the screen horizontally.
xvelocity = -int(xvelocity) # Reverse the x-velocity if it hits the boundary.

myFile = open("ballpositions.csv", 'a', newline="") # Open the CSV file in append mode.
writer = csv.writer(myFile)
#writer.writerow([cor[-1],"", gravity, velocity, xvelocity]) # Write the last recorded position to the CSV file.
#writer.writerow(cor[-1])
xc, yc = ball.pos()
writer.writerow([xc,yc,gravity, original_velocity, original_xvelocity])
print(ball.pos()) # Print the current position of the ball.

turtle.done() # Exit the turtle graphics environment.