Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPong.py
More file actions
Latest commit
169 lines (127 loc) · 5.66 KB
/
Copy pathPong.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
importthreading
importtime
importAnimations
importDirection
importGame
importapi
classPong(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))
defget_menu_frame(self):
returnself._menu_frame
defhas_menu_animation(self):
returnTrue
defstart_game(self):
pass
defplay_animation(self):
self.an=Animations.TickerAnimation("pong")
self.an.start()
defclose_animation(self):
self.an.stop()
defstopped_animation(self):
returnself.an.stopped()
defis_animation_alive(self):
returnself.an.is_alive()
defdone(self):
forxinrange(0, 8):
api.led_on([0, x, 0], [7, x, 0], [0, x, 7], [7, x, 7])
api.display(api.leds)
time.sleep(1)
defrun(self):
counter=0
whilenotself.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)
ifcounter==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
ifself.b_loc[0] -self.b_radius<0orself.b_loc[0] +self.b_radius>1:
self.ball_vel_x*=-1
changed=True
ifself.b_loc[1] -self.b_radius<0orself.b_loc[1] +self.b_radius>1:
self.ball_vel_y*=-1
changed=True
ifself.b_loc[2] -self.b_radius<0orself.b_loc[2] +self.b_radius>1:
self.ball_vel_z*=-1
changed=True
ifself.b_loc[1] -self.b_radius<1/self.cube_sizeandnotchanged:
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
ifself.b_loc[1] -self.b_radius<0:
ifnot ((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
ifself.player_actionisnotNone:
ifself.player_action==int(Direction.Direction.UP):
api.cuboid_off(self.p_loc, self.p_size, 1, self.p_size)
ifself.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)
ifself.player_action==int(Direction.Direction.DOWN):
api.cuboid_off(self.p_loc, self.p_size, 1, self.p_size)
ifself.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)
ifself.player_action==int(Direction.Direction.RIGHT):
api.cuboid_off(self.p_loc, self.p_size, 1, self.p_size)
ifself.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)
ifself.player_action==int(Direction.Direction.LEFT):
api.cuboid_off(self.p_loc, self.p_size, 1, self.p_size)
ifself.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)