-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.py
More file actions
182 lines (140 loc) · 5.79 KB
/
Copy pathball.py
File metadata and controls
182 lines (140 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import turtle
import math
import time
import random
import bouncer
baller = turtle.Turtle()
class configData():
ballerStart = (0,0) # ball start coordinates
shape = "circle" # shape of the ball, for science purposes.
xSize, ySize, outLineSize = (1,1,1) # This tuple is changed into 3 variables for the size of the ball.
bounceCoefficient = 1 # the amount in which the speed changes (multiply) when the ball bounces
# do not make bounceCoefficient negative (please I beg you)
def roundStart(screensize):
movement.init_()
events.init_(screensize)
baller.penup()
baller.ht()
baller.goto(configData.ballerStart)
baller.shape(configData.shape)
baller.shapesize(configData.xSize, configData.ySize, configData.outLineSize)
baller.showturtle()
Vector1.roundStartVector()
class Vector1():
heading = 0
speed = 0
'''
This is an improvised Vector system.
This class contains the heading, speed, and direction information.
Heading and speed contain calculation methods, but are not used.
Ball movement is managed by the moveOnVelocity() method within the movement class.
Vector1 also includes the updateVector and roundStartVector methods.
updateVector() - This method takes the xVelocity and yVelocity global variables and calulates the heading.
If the heading would cause a 0 or undefined result from the math.atan() function, then it uses predetermined
values for the heading.
roundStartVector() - This method sets the starting values to xVelocity and yVelocity. By default these
'''
def updateHeading():
if xVelocity != 0:
Vector1.heading = math.degrees(math.atan(yVelocity / xVelocity))
if yVelocity < 0 and xVelocity < 0 or yVelocity > 0 and xVelocity < 0:
Vector1.heading -= 180
elif yVelocity < 0:
Vector1.heading = 270
else:
Vector1.heading = 90
def updateSpeed():
if(Vector1.speed < 0):
raise ValueError("Speed less than 0")
else:
Vector1.speed = math.isqrt(math.pow(xVelocity, 2) + math.pow(yVelocity, 2))
def roundStartVector():
global xVelocity
global yVelocity
xVelocity = random.randint(15,30)
yVelocity = random.randint(20,30)
class movement:
'''
This class manages the movement of the ball.
It contains the init_(), updatePositions(), and moveOnVelocity() methods.
init_() - This method initializes the values for xVelocity, yVelocity, xPosition, and yPosition.
xVelocity and yVelocity are set to 0, while xPosition and yPosition are set to values based on
the ballerStart tuple contained within the configData class.
updatePositions() - This method only changes the global variables xPosition and yPosition to the current
position of the ball.
moveOnVelocity() - This method calls movement.updatePositions() and events.collisionCheckAll() before
setting the variables xTarget and yTarget. These variables are calculated by adding their corresponding
positions* and velocities*.
*corresponding positons and velocities refers to their x and y variants.
'''
def init_():
global xVelocity
global yVelocity
global xPosition
global yPosition
xVelocity = 0
yVelocity = 0
xPosition, yPosition = configData.ballerStart
def updatePositions():
global xPosition
global yPosition
xPosition = baller.xcor()
yPosition = baller.ycor()
def moveOnVelocity():
movement.updatePositions()
events.collisionCheckAll()
xTarget = xPosition + xVelocity
yTarget = yPosition + yVelocity
baller.goto(xTarget, yTarget)
class events():
'''
This class contains the collision events for the Ball.
collisionCheckAll() - This method only consolidates the other collision checkers.
init_(screensize) - This initialization method calls the screen size to ensure that the ball is bouncing off of the edge
of the screen.
wallCollision() - This checks if the ball is colliding with the boundaries of the screen, causing the ball to
reverse xVelocity on collision.
reflectOnBouncer() - This method calls bouncer.eventChecker.collisionCheck() with the turtle ball as a parameter.
If the method returns True, then the yVelocity is reversed.
floorCollision() - This method is called to check if the ball collides with the bottom of the screen.
It returns True if the condition is met, and returns False when not met.
reflectOnRoof() - This method checks if the ball is colliding with the roof and the yVelocity is greater than 0.
If this condition is True, then the yVelocity of the ball is reversed.
''' # for video events
def collisionCheckAll():
events.wallCollision()
events.reflectOnBouncer()
events.reflectOnRoof()
def init_(screensize):
global ScreenLength
global ScreenWidth
ScreenWidth, ScreenLength = 550,600
if(configData.bounceCoefficient < 0):
raise ValueError("bounceCoefficient cannot be negative")
if(ScreenWidth <= 0) or (ScreenLength <= 0):
print("ScreenWidth, ScreenLength: ", ScreenWidth, ", ", ScreenLength)
raise ValueError("Negative dimensions detected")
if(abs(baller.xcor()) > ScreenWidth) or (abs(baller.ycor()) > ScreenLength):
raise Exception("Baller outside of screen boundaries")
def wallCollision():
if(abs(baller.xcor()) > ScreenWidth):
global xVelocity
xVelocity *= -1
def reflectOnBouncer():
if(bouncer.eventChecker.collisionCheck(baller)):
global yVelocity
yVelocity *= -1 * configData.bounceCoefficient
print("Collision Activated")
def floorCollision():
if(baller.ycor() < -1*int(ScreenLength/2)):
return True
else:
return False
def reflectOnRoof():
global yVelocity
if(baller.ycor() > int((ScreenLength / 2))) and (yVelocity > 0):
yVelocity *= -1 * configData.bounceCoefficient
print("roof boinger")
def collideWithBlock():
global yVelocity
yVelocity *= -1 * configData.bounceCoefficient