-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPong.py
More file actions
169 lines (127 loc) · 5.66 KB
/
Pong.py
File metadata and controls
169 lines (127 loc) · 5.66 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
import threading
import time
import Animations
import Direction
import Game
import api
class Pong(Game.CubeGame, threading.Thread):
_name = 'Pong'
_version = 'v0.1'
_menu_frame = [0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0]
def __init__(self, cube_size, frame_size):
Game.CubeGame.__init__(self, cube_size, frame_size, self._name)
threading.Thread.__init__(self)
self.an = None
self.b_loc = [0.250, 0.5, 0.250]
self.b_size = 1
self.b_radius = (self.b_size / self.cube_size) / 2
self.ball_vel_x = 0.125
self.ball_vel_y = 0.125
self.ball_vel_z = 0.125
self.failed = False
self.player_action = None
self.p_loc = [0.5, 0, 0.5]
self.p_size = 2
self.p_radius = (self.p_size / self.cube_size) / 2
self.mov_val = (1 / (self.cube_size - 1))
def get_menu_frame(self):
return self._menu_frame
def has_menu_animation(self):
return True
def start_game(self):
pass
def play_animation(self):
self.an = Animations.TickerAnimation("pong")
self.an.start()
def close_animation(self):
self.an.stop()
def stopped_animation(self):
return self.an.stopped()
def is_animation_alive(self):
return self.an.is_alive()
def done(self):
for x in range(0, 8):
api.led_on([0, x, 0], [7, x, 0], [0, x, 7], [7, x, 7])
api.display(api.leds)
time.sleep(1)
def run(self):
counter = 0
while not self.failed:
# Turn off last position
# api.led_off(self.ball_loc)
# Ball moving
"""
self.ball_loc[0] += self.ball_vel_x
self.ball_loc[1] += self.ball_vel_y
self.ball_loc[2] += self.ball_vel_z
"""
api.cuboid_off(self.b_loc, self.b_size, self.b_size, self.b_size)
if counter == 7:
self.b_loc[0] += self.ball_vel_x
self.b_loc[1] += self.ball_vel_y
self.b_loc[2] += self.ball_vel_z
changed = False
if self.b_loc[0] - self.b_radius < 0 or self.b_loc[0] + self.b_radius > 1:
self.ball_vel_x *= -1
changed = True
if self.b_loc[1] - self.b_radius < 0 or self.b_loc[1] + self.b_radius > 1:
self.ball_vel_y *= -1
changed = True
if self.b_loc[2] - self.b_radius < 0 or self.b_loc[2] + self.b_radius > 1:
self.ball_vel_z *= -1
changed = True
if self.b_loc[1] - self.b_radius < 1 / self.cube_size and not changed:
if ((self.p_loc[0] + self.p_radius > self.b_loc[0] > self.p_loc[0] - self.p_radius) and (
self.p_loc[2] + self.p_radius > self.b_loc[2] > self.p_loc[2] - self.p_radius)):
self.ball_vel_y *= -1
if self.b_loc[1] - self.b_radius < 0:
if not ((self.p_loc[0] + self.p_radius > self.b_loc[0] > self.p_loc[0] - self.p_radius) and (
self.p_loc[2] + self.p_radius > self.b_loc[2] > self.p_loc[2] - self.p_radius)):
self.failed = True
self.done()
# Player moving
self.player_action = Direction.direction_p_1.value
if self.player_action is not None:
if self.player_action == int(Direction.Direction.UP):
api.cuboid_off(self.p_loc, self.p_size, 1, self.p_size)
if self.p_loc[0] + self.mov_val <= 1:
self.p_loc[0] += self.mov_val
else:
self.p_loc[0] = 0.99
api.cuboid_on(self.p_loc, self.p_size, 1, self.p_size)
if self.player_action == int(Direction.Direction.DOWN):
api.cuboid_off(self.p_loc, self.p_size, 1, self.p_size)
if self.p_loc[0] - self.mov_val >= 0:
self.p_loc[0] -= self.mov_val
else:
self.p_loc[0] = 0.01
api.cuboid_on(self.p_loc, self.p_size, 1, self.p_size)
if self.player_action == int(Direction.Direction.RIGHT):
api.cuboid_off(self.p_loc, self.p_size, 1, self.p_size)
if self.p_loc[2] + self.mov_val <= 1:
self.p_loc[2] += self.mov_val
else:
self.p_loc[2] = 0.99
api.cuboid_on(self.p_loc, self.p_size, 1, self.p_size)
if self.player_action == int(Direction.Direction.LEFT):
api.cuboid_off(self.p_loc, self.p_size, 1, self.p_size)
if self.p_loc[2] - self.mov_val >= 0:
self.p_loc[2] -= self.mov_val
else:
self.p_loc[2] = 0.01
api.cuboid_on(self.p_loc, self.p_size, 1, self.p_size)
Direction.direction_p_1.value = 0
# Turn on new position
# api.led_on(self.ball_loc)
api.cuboid_on(self.b_loc, self.b_size, self.b_size, self.b_size)
api.cuboid_on(self.p_loc, self.p_size, 1, self.p_size)
api.display(api.leds)
counter = (counter + 1) % 8
time.sleep(0.1)