-
-
Save anonymous/ec11908615282ed21b86 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import random | |
import math | |
import time | |
def move(): | |
global b1xspeed | |
global b1yspeed | |
global b2xspeed | |
global b2yspeed | |
# Coordinates | |
x1 = b1.xcor() | |
x2 = b2.xcor() | |
y1 = b1.ycor() | |
y2 = b2.ycor() | |
# Collision of the balls | |
hitb1x = b1xspeed | |
hitb1y = b1yspeed | |
hitb2x = b2xspeed | |
hitb2y = b2speed | |
if math.sqrt( (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))<radius*2: | |
# Balls bouncing off each other | |
b1xspeed = (hitb1x + (2*radius*hitb2x))/ (2*radius) | |
b2xspeed = (hitb2x + (2*radius*hitb1x))/ (2*radius) | |
b1yspeed = (hitb1y + (2*radius*hitb2y))/ (2*radius) | |
b2yspeed = (hitb1y + (2*radius*hitb2y))/ (2*radius) | |
# Balls disappearing and reappearing | |
ball_choice = random.choice([1,2]) | |
if ball_choice == 1: | |
b2.hideturtle() | |
time.sleep(0.2) | |
b2.goto (random.random()*((winRight-radius) - (winLeft+radius)) + (winLeft+radius), random.random()*((winDown-radius) - (winUp+radius)) + winUp+radius) | |
b2.showturtle() | |
else: | |
b1.hideturtle() | |
time.sleep(0.2) | |
b1.goto (random.random()*((winRight-radius) - (winLeft+radius)) + (winLeft+radius), random.random()*((winDown-radius) - (winUp+radius)) + winUp+radius) | |
b1.showturtle() | |
# First ball | |
if x1+ radius >= winRight or x1 - radius <= winLeft: | |
b1xspeed *= -1 | |
if y1+ radius >= winDown or y1 - radius <= winUp: | |
b1yspeed *= -1 | |
b1.goto(x1+b1xspeed, y1-b1yspeed) | |
# Second ball | |
if x2+ radius >= winRight or x2 - radius <= winLeft: | |
b2xspeed *= -1 | |
if y2+ radius >= winDown or y2 - radius <= winUp: | |
b2yspeed *= -1 | |
b2.goto(x2+b2xspeed, y2-b2yspeed) | |
win.ontimer(move, 5) | |
# Initialising the graphics | |
turtle.setup(700,400) # Screen size | |
win = turtle.Screen() # Loads the screen | |
win.title("Bouncing Ball") | |
win.bgcolor("blue") # Background colour | |
win.register_shape("beachball.gif") # Loads the ball graphic | |
win.register_shape("ball.gif") | |
# Border detection | |
winRight = win.window_width()/2 | |
winLeft = -winRight | |
winDown = win.window_height()/2 | |
winUp = -winDown | |
# First ball setup | |
b1=turtle.Turtle() | |
b1.shape("ball.gif") | |
b1.penup() | |
# Second ball setup | |
b2=turtle.Turtle() | |
b2.shape("beachball.gif") | |
b2.penup() | |
# Some more dimensions | |
radius = 35 | |
# Speed of balls | |
b1xspeed = 12 | |
b1yspeed = 12 | |
b2xspeed = 8 | |
b2yspeed = 8 | |
# Angle of balls | |
bangle = math.radians(30) | |
wangle = math.radians(240) | |
b1speed = 20 | |
b1speed = math.cos(bangle) * b1speed | |
b1speed = math.sin(bangle) * b1speed | |
b2speed = 12 | |
b2speed = math.cos(wangle) * b2speed | |
b2yspeed = math.sin(wangle) * b2speed | |
# Random starting point for balls | |
b1.goto (random.random()*((winRight-radius) - (winLeft+radius)) + (winLeft+radius), random.random()*((winDown-radius) - (winUp+radius)) + winUp+radius) | |
b2.goto (random.random()*((winRight-radius) - (winLeft+radius)) + (winLeft+radius), random.random()*((winDown-radius) - (winUp+radius)) + winUp+radius) | |
# Calls the move function | |
move() | |
win.exitonclick() | |
win.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment