-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoving.py
106 lines (81 loc) · 2.42 KB
/
moving.py
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
import math
class MovingObject:
xvel = 0
yvel = 0
def __init__(self, x, y, width, height):
self.coords = (x, y, x + width, y + height)
def startUp(self):
self.yvel = -self.velocity
def startDown(self):
self.yvel = self.velocity
def stop(self):
self.xvel = 0
self.yvel = 0
def getCoords(self):
return self.coords
def move(self, backwards=False):
x1, y1, x2, y2 = self.coords
xvel, yvel = self.xvel, self.yvel
if backwards:
xvel = -xvel
yvel = -yvel
newCoords = (x1 + xvel,
y1 + yvel,
x2 + xvel,
y2 + yvel)
self.coords = newCoords
def bounce(self, side):
self.move(True)
class Paddle(MovingObject):
velocity = 2
def __init__(self, originX):
width = 5
height = 40
originY = 50
MovingObject.__init__(self,
originX,
originY,
width,
height)
class PaddleL(Paddle):
def __init__(self):
Paddle.__init__(self, 10)
class PaddleR(Paddle):
def __init__(self):
Paddle.__init__(self, 90)
class Ball(MovingObject):
velocity = math.sqrt(2)
angle = math.pi / 5
def __init__(self):
width = 5
height = 5
originX = 40
originY = 10
MovingObject.__init__(self,
originX,
originY,
width,
height)
def calcVelocity(self):
adj = math.cos(self.angle) * self.velocity
opp = math.sin(self.angle) * self.velocity
self.xvel = adj
self.yvel = opp
def move(self, backwards=False):
self.calcVelocity()
MovingObject.move(self, backwards)
def stop(self):
self.velocity = 0
def normaliseAngle(self):
while self.angle > math.pi * 2:
self.angle -= math.pi * 2
while self.angle < -math.pi * 2:
self.angle += math.pi * 2
def bounce(self, side):
print(side)
self.move(True)
if side == 'left' or side == 'right':
self.angle = math.pi - self.angle
else:
self.angle = -self.angle
self.normaliseAngle()