-
Notifications
You must be signed in to change notification settings - Fork 23
/
pong.py
328 lines (213 loc) · 7.9 KB
/
pong.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"""
examples/pong.py
Copyright (c) 2022, Nir Aides - [email protected]
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
"""
* pong sound is by [freesound](https://freesound.org/people/NoiseCollector/sounds/4359/).
* Commodore 64 font is by [KreativeKorp](https://www.kreativekorp.com/software/fonts/c64.shtml).
"""
import math
import time
import sys
import os
import PIL.Image
import numpy as np
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import jupylet.color
from jupylet.app import App
from jupylet.state import State
from jupylet.label import Label
from jupylet.sprite import Sprite
from jupylet.audio.sample import Sample
import moderngl_window.timers.clock as _clock
app = App()
background = '#3e32a2'
foreground = '#7c71da'
a0 = np.ones((32, 32)) * 255
a1 = np.ones((128, 16)) * 255
a2 = np.ones((app.height * 9 // 10, app.width * 9 // 10, 3)) * 255
ball = Sprite(a0, y=app.height/2, x=app.width/2)
padl = Sprite(a1, y=app.height/2, x=48)
padr = Sprite(a1, y=app.height/2, x=app.width-48)
field = Sprite(a2, y=app.height/2, x=app.width/2, color=background)
pong_sound = Sample('sounds/pong-blip.wav', amp=0.2).load()
scorel = Label(
'0', font_size=42, color=foreground,
x=64, y=app.height/2,
anchor_y='center', anchor_x='left',
font_path='fonts/PetMe64.ttf'
)
scorer = Label(
'0', font_size=42, color=foreground,
x=app.width-64, y=app.height/2,
anchor_y='center', anchor_x='right',
font_path='fonts/PetMe64.ttf'
)
@app.event
def render(ct, dt):
app.window.clear(color=foreground)
field.draw()
scorel.draw()
scorer.draw()
ball.draw()
padl.draw()
padr.draw()
state = State(
sl = 0,
sr = 0,
bvx = 192,
bvy = 192,
vyl = 0,
pyl = app.height/2,
vyr = 0,
pyr = app.height/2,
left = False,
right = False,
key_a = False,
key_d = False,
)
@app.event
def key_event(key, action, modifiers):
keys = app.window.keys
if action == keys.ACTION_PRESS:
if key == keys.LEFT:
state.left = True
if key == keys.RIGHT:
state.right = True
if key == keys.A:
state.key_a = True
if key == keys.D:
state.key_d = True
if action == keys.ACTION_RELEASE:
if key == keys.LEFT:
state.left = False
if key == keys.RIGHT:
state.right = False
if key == keys.A:
state.key_a = False
if key == keys.D:
state.key_d = False
@app.run_me_every(1/120)
def update_pads(ct, dt):
if state.right:
state.pyr = min(app.height, state.pyr + dt * 512)
if state.left:
state.pyr = max(0, state.pyr - dt * 512)
if state.key_a:
state.pyl = min(app.height, state.pyl + dt * 512)
if state.key_d:
state.pyl = max(0, state.pyl - dt * 512)
ayl = 200 * (state.pyl - padl.y)
ayr = 200 * (state.pyr - padr.y)
state.vyl = state.vyl * 0.9 + (ayl * dt)
state.vyr = state.vyr * 0.9 + (ayr * dt)
padl.y += state.vyl * dt
padr.y += state.vyr * dt
padr.clip_position(app.width, app.height)
padl.clip_position(app.width, app.height)
@app.run_me_every(1/60)
def update_ball(ct, dt):
bs0 = state.bvx ** 2 + state.bvy ** 2
ball.angle += 200 * dt
ball.x += state.bvx * dt
ball.y += state.bvy * dt
if ball.top >= app.height:
pong_sound.play(pan=2*max(.25, min(.75, ball.x / app.width))-1)
ball.y -= ball.top - app.height
state.bvy = -state.bvy
if ball.bottom <= 0:
pong_sound.play(pan=2*max(.25, min(.75, ball.x / app.width))-1)
ball.y -= ball.bottom
state.bvy = -state.bvy
if ball.right >= app.width:
pong_sound.play(pan=2*max(.25, min(.75, ball.x / app.width))-1)
ball.x -= ball.right - app.width
state.bvx = -192
state.bvy = 192 * np.sign(state.bvy)
bs0 = 0
state.sl += 1
scorel.text = str(state.sl)
if ball.left <= 0:
pong_sound.play(pan=2*max(.25, min(.75, ball.x / app.width))-1)
ball.x -= ball.left
state.bvx = 192
state.bvy = 192 * np.sign(state.bvy)
bs0 = 0
state.sr += 1
scorer.text = str(state.sr)
if state.bvx > 0 and ball.top >= padr.bottom and padr.top >= ball.bottom:
if 0 < ball.right - padr.left < 10:
pong_sound.play(pan=2*max(.25, min(.75, ball.x / app.width))-1)
ball.x -= ball.right - padr.left
state.bvx = -state.bvx
state.bvy += state.vyr / 2
if state.bvx < 0 and ball.top >= padl.bottom and padl.top >= ball.bottom:
if 0 < padl.right - ball.left < 10:
pong_sound.play(pan=2*max(.25, min(.75, ball.x / app.width))-1)
ball.x += ball.left - padl.right
state.bvx = -state.bvx
state.bvy += state.vyl / 2
bs1 = state.bvx ** 2 + state.bvy ** 2
if bs1 < 0.9 * bs0:
state.bvx = (bs0 - state.bvy ** 2) ** 0.5 * np.sign(state.bvx)
ball.wrap_position(app.width, app.height)
@app.run_me()
def highlights(ct, dt):
sl0 = state.sl
sr0 = state.sr
slc = np.array(scorel.color)
src = np.array(scorer.color)
while True:
ct, dt = yield 1/24
r0 = 0.9 ** (120 * dt)
scorel.color = np.array(scorel.color) * r0 + (1 - r0) * slc
scorer.color = np.array(scorer.color) * r0 + (1 - r0) * src
if sl0 != state.sl:
sl0 = state.sl
scorel.color = 'white'
if sr0 != state.sr:
sr0 = state.sr
scorer.color = 'white'
def step(player0=[0, 0, 0, 0, 0], player1=[0, 0, 0, 0, 0], n=1):
state.key_a, state.key_d = player0[:2]
state.left, state.right = player1[:2]
sl0 = state.sl
sr0 = state.sr
if app.mode == 'hidden':
app.step(n)
reward = (state.sl - sl0) - (state.sr - sr0)
return observe(reward)
def observe(reward=0):
return {
'screen0': app.observe(),
'player0': {'score': state.sl, 'reward': reward},
'player1': {'score': state.sr, 'reward': -reward},
}
START = 'pong-start.state'
def reset():
load(START)
return observe()
def load(path):
app.load_state(path, state, ball, padl, padr, scorel, scorer)
return observe()
def save(path=None):
app.save_state('pong', path, state, ball, padl, padr, scorel, scorer)
if __name__ == '__main__':
app.run()