Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- from turtle import *
- from random import randint
- from numpy import array
- from math import atan, tan, sin, cos, pi
- setup()
- bgcolor("Black")
- #Setup the crew
- turtle = Turtle()
- turtle2 = Turtle()
- turtle3 = Turtle()
- screen = Screen()
- Objects = [[[310,-310],[310,-200],[-300,-310]]] #Slope
- Objects += [[[-200,-300],[-200,300],[-190,300],[-190,-300]]] #Rectangle
- #Setup the ball
- turtle.color("blue")
- turtle.shape("circle")
- tracer(False)
- turtle.speed("fastest")
- #Setup the walls
- turtle2.color("white")
- turtle2.penup()
- turtle2.hideturtle()
- turtle3.color("Red")
- turtle3.shape("circle")
- turtle3.speed("fastest")
- turtle3.hideturtle()
- class Ball:
- #Physics stuff
- pos = [0,0]
- speed = [5,5]
- elas = 0.5
- #Move the ball to start position
- turtle.penup()
- turtle.goto(Ball.pos)
- turtle.pendown()
- #Draw the canvases walls
- def Walls():
- for i in range(len(Objects)):
- turtle2.begin_fill()
- for j in Objects[i]:
- turtle2.goto(j)
- turtle2.pendown()
- if j == Objects[i][-1]:
- turtle2.goto(Objects[i][0])
- turtle2.end_fill()
- turtle2.penup()
- bounding = True
- def intersection(a,b,c,d):
- ## print(a,b)
- ## print(c,d)
- if bounding == True:
- turtle3.showturtle()
- turtle3.penup()
- turtle3.goto(a)
- turtle3.pendown()
- turtle3.goto(b)
- turtle3.penup()
- turtle3.goto(c)
- turtle3.pendown()
- turtle3.goto(d)
- turtle3.penup()
- turtle3.penup()
- turtle3.goto(a[0],a[1])
- turtle3.pendown()
- turtle3.goto(b[0],a[1])
- turtle3.goto(b[0],b[1])
- turtle3.goto(a[0],b[1])
- turtle3.goto(a[0],a[1])
- turtle3.penup()
- #y=mx+c
- ##
- ## if min(a[0],b[0])!=a[0]:
- ## temp = a
- ## a = b
- ## b = temp
- if min(c[0],d[0])!=c[0]:
- temp = c
- c = d
- d = temp
- I = [min(a[0],b[0]),max(a[0],b[0]),min(c[0],d[0]),max(c[0],d[0])]
- J = [min(a[1],b[1]),max(a[1],b[1]),min(c[1],d[1]),max(c[1],d[1])]
- if J[0] > J[3] or J[1] < J[2] or I[0] > I[3] or I[1] < I[2]:
- return
- #If both x are 0 return
- if a[0]-b[0]==0 and c[0]-d[0] == 0:
- return
- #Return intersection where a,b's x is zero
- if a[0]-b[0]==0:
- #Find gradient
- m1 = 0
- m2 = (d[1]-c[1])/(d[0]-c[0])
- c2 = c[1]-m2*c[0]
- #Find intersect
- x = a[0]
- y = m2*x+c2
- deflection = rebound(a,b,c,d,x,y,m1,m2)
- return deflection
- #Return intersection where c,d's x is zero
- if c[0]-d[0]==0:
- m1 = (b[1]-a[1])/(b[0]-a[0])
- c1 = a[1]-m1*a[0]
- m2 = 0
- x = c[0]
- y = m1*x+c1
- deflection = rebound(a,b,c,d,x,y,m1,m2)
- return deflection
- if a[0]-b[0]!=0 and c[0]-d[0] != 0:
- #Gradients and C values
- m1 = (b[1]-a[1])/(b[0]-a[0])
- c1 = a[1]-m1*a[0]
- m2 = (d[1]-c[1])/(d[0]-c[0])
- c2 = c[1]-m2*c[0]
- #y=mx+c
- #m1x+c1=m2x+c2
- #m1x-m2x=c2-c1
- #x(m1-m2)=c2-c1
- #x = (c2-c1)/(m1-m2)
- #Calculate x,y with maths
- x = (c2-c1)/(m1-m2)
- y = m1*x+c1
- if bounding == True:
- turtle3.goto(x,y)
- #Parallel check
- if m1==m2:
- return
- ## print("m1,m2:",m1,m2)
- ## print("c1,c2:",c1,c2)
- ## print(c,d)
- ## print(x,y)
- ## print(a,b)
- if x < I[1] and x > I[0] and y < J[1] and y > J[0]:
- deflection = rebound(a,b,c,d,x,y,m1,m2)
- return deflection
- else:
- ###print(x,y)
- ###print(a,b)
- ###print(c,d)
- ###print(I1[0],I2[0])
- return
- def rebound(a,b,c,d,x,y,m1,m2):
- s = [b[0]-a[0],b[1]-a[1]]
- mag = 0.9*(abs(s[0]**2+s[1]**2)**0.5)
- reflection = 0
- if a[0]-b[0]==0:
- if s[1]<0:
- reflection = atan(m2)+pi/2
- else:
- reflection = atan(m2)-pi/2
- if c[0]-d[0]==0:
- if s[0]<0:
- reflection = -atan(m1)
- else:
- reflection = pi-atan(m1)
- if a[0]-b[0]!=0 and c[0]-d[0]!=0:
- if s[1]<0:
- if s[0]<0:
- if m2<0:
- #Negative x,y intersect downward slope 2
- reflection = pi/2+atan(m1)+atan(m2)
- else:
- #Negative x,y intersect upward slope 4
- reflection = pi+atan(m1)-atan(m2)
- else:
- if m2<0:
- #Negative y intersect downward slope 1
- reflection = (-pi/2)-atan(m2)-atan(m1)
- else:
- #Negative y intersect upward slope 3
- reflection = (pi/2)+atan(m2)+atan(m1)
- else:
- if s[0]<0:
- if m2<0:
- #Positive y intersect downward slope 6
- reflection = (pi/2)-atan(m2)-atan(m1)
- else:
- #Positive y intersect upward slope 8
- reflection = -atan(m2)+atan(m1)
- else:
- if m2<0:
- #Positive x,y intersect downward slope 5
- reflection = (-pi/2)-atan(m2)-atan(m1)
- else:
- #Positive x,y intersect upward slope 7
- reflection = (pi/2)-atan(m1)-atan(m2)
- ## if s[1]<0: #Not hitting the ceiling
- ## if m2 > 0:
- ## if s[0]>0:
- ## print(1)
- ## reflection = atan(m2)-atan(m1)
- ## else:
- ## print(2)
- ## reflection = pi-atan(m1)+atan(m2)
- ## else:
- ## if s[0]>0:
- ## print(3)
- ## reflection = (pi/2)+atan(m2)+atan(m1)
- ## else:
- ## print(4)
- ## reflection = -pi+atan(m2)
- ## else:
- ## if m2 > 0:
- ## print(3)
- ## reflection = -(pi/2)+atan(m2)-atan(m1)
- ## else:
- ## print(4)
- ## reflection = (pi/2)-atan(m1)+atan(m2)
- #turtle.goto(x,y)
- deflection = [cos(reflection)*mag,sin(reflection)*mag]
- if bounding == True:
- turtle3.goto(x,y)
- turtle3.pendown()
- turtle3.color("green")
- turtle3.goto(b)
- turtle3.penup()
- turtle3.color("red")
- turtle3.goto(x,y)
- turtle3.pendown()
- turtle3.color("yellow")
- turtle3.goto(x+deflection[0],y+deflection[1])
- turtle3.color("red")
- turtle3.penup()
- ## print("Speed vector:",s)
- ## print("Intersecting angle:",atan(m1))
- ## print("Slope of platform:",atan(m2))
- ## print("Reflection off platform:",reflection)
- ## print("Magnitude:",mag)
- ## print("Resultant vector:",deflection)
- return deflection
- #print(intersection([100,100],[0,-100],[0,0],[300,150]))
- #print(intersection([0,0],[200,-300],[0,-300],[300,-200]))
- #print(intersection([0,-100],[0,-300],Objects[0][2],Objects[0][1]))
- #print(intersection([-100,0],[200,-300],[0,-300],[300,-200]))
- #print(intersection([100,100],[50,0],[0,0],[300,150]))
- #Slopes
- print(intersection([-320, 200],[-250,100],[-400,200],[-200,100]))
- print(intersection([0, 200],[-150,100],[-200,200],[0,100]))
- print(intersection([0, 200],[150,100],[0,100],[200,200]))
- print(intersection([350, 200],[250,100],[400,200],[200,100]))
- print(intersection([-390, 0],[-250,100],[-400,100],[-200,0]))
- print(intersection([-90, 0],[-150,100],[-200,100],[0,0]))
- print(intersection([90, 0],[150,100],[0,0],[200,100]))
- print(intersection([350, 0],[250,100],[400,100],[200,0]))
- #print(intersection([0,100],[300,-300],Objects[0][2],Objects[0][1]))
- #print(intersection([0,-100],[300,-300],Objects[0][2],Objects[0][1]))
- #print(intersection([0,0],[-200,-300],Objects[0][2],Objects[0][1]))
- #print(intersection([0,-100],[-300,-50],[0,-200],[-300,100]))
- #print(intersection([200,0],[100,-200],[-100,0],[200,-200]))
- #print(intersection([0,0],[100,-200],[-100,0],[200,-200]))
- #print(intersection([-150,50],[-50,-100],[0,-100],[-200,0]))
- #Ceiling deflections
- #print(intersection([0,0],[-200,200],[-100,0],[-110,300]))
- #print(intersection([0,0],[-200,200],[-100,0],[0,300]))
- #print(intersection([200,200],[0,0],[0,300],[100,0]))
- #print(intersection([-50,0],[-200,200],[-100,0],[0,90]))
- #Wall deflections
- #print(intersection([0,0],[-200,200],[-100,0],[-100,300]))
- #print(intersection([-200,0],[200,200],[-100,0],[-100,300]))
- #print(intersection([0,200],[-200,0],[-100,0],[-100,300]))
- #print(intersection([0,0],[0,200],[-100,100],[100,150]))
- def Physics(thing):
- for i in range(len(Objects)):
- for j in range(-1,len(Objects[i])-1):
- temp = intersection(thing.pos, [thing.pos[0] + thing.speed[0]]+[thing.pos[1] + thing.speed[1]-1], Objects[i][j], Objects[i][j+1])
- if temp:
- thing.speed = temp
- thing.pos[0] = thing.pos[0] + thing.speed[0]
- thing.pos[1] = thing.pos[1] + thing.speed[1]
- #Applies downward force (gravity)
- thing.speed[1] = thing.speed[1] - 1
- ## #Bounce off the floor and ceiling
- ## if thing.pos[1] < -300:
- ## if thing.speed[1]<0 and thing.speed[1]>=-2:
- ## thing.speed[1] = 0
- ## else:
- ## print("Before:",thing.speed)
- ## thing.speed[1] = -1*((thing.speed[1]+1)*thing.elas)
- ## thing.pos[1] = -300
- ## print("After:",thing.speed)
- ##
- ## #Bounce off the walls
- ## if thing.pos[0] <= -500 or thing.pos[0] >= 500:
- ## thing.speed[0] = thing.speed[0]*-1
- return
- def drag(x,y):
- print(x,y)
- turtle3.clear()
- turtle.clear()
- Ball.speed[1] = randint(0,30)
- Ball.speed[0] = randint(-10,10)
- #Ball.speed[0] = 0
- Ball.elas = randint(0,1000)/1000
- turtle.penup()
- turtle.goto(0,0)
- turtle.pendown()
- Ball.pos = [0,0]
- return
- #Walls()
- #The main loop
- while True:
- #1/60th of a second
- sleep(0.01666)
- #Physics(Ball)
- turtle.goto(Ball.pos)
- update()
- screen.onscreenclick(drag)
- #Exit Gracefully
- hideturtle()
- done()
Add Comment
Please, Sign In to add comment