-
-
Save marky1991/91270c8fa0d4e2d2afbe 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 | |
#Not proud of this | |
global ball1_hiding_time | |
global ball2_hiding_time | |
# 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 one is hiding, don't bother trying to check for a collision | |
if ball1_hiding_time == 0 and ball2_hiding_time == 0: | |
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() | |
b2.goto (random.random()*((winRight-radius) - (winLeft+radius)) + (winLeft+radius), random.random()*((winDown-radius) - (winUp+radius)) + winUp+radius) | |
#Change to whatever, but make sure it's a multiple of 5 | |
ball2_hiding_time = 2000 | |
else: | |
b1.hideturtle() | |
b1.goto (random.random()*((winRight-radius) - (winLeft+radius)) + (winLeft+radius), random.random()*((winDown-radius) - (winUp+radius)) + winUp+radius) | |
ball1_hiding_time = 2000 | |
# First ball | |
if x1+ radius >= winRight or x1 - radius <= winLeft: | |
b1xspeed *= -1 | |
if y1+ radius >= winDown or y1 - radius <= winUp: | |
b1yspeed *= -1 | |
if ball1_hiding_time <= 0: | |
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 | |
if ball2_hiding_time <= 0: | |
b2.goto(x2+b2xspeed, y2-b2yspeed) | |
if ball1_hiding_time > 0: | |
ball1_hiding_time = ball1_hiding_time - 5 | |
if ball2_hiding_time > 0: | |
ball2_hiding_time = ball2_hiding_time - 5 | |
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 | |
ball1_hiding_time = ball2_hiding_time = 0 | |
# 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