-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpong_app.py
73 lines (62 loc) · 2.14 KB
/
pong_app.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
from flask import Flask, render_template_string, request, jsonify
import pong_game
import segMonster, segMonsterSimulator
import threading, time, subprocess
app = Flask(__name__)
with open("pong_index.html") as file:
template = file.read()
@app.route('/', methods=['GET', 'POST'])
def index():
state = app.config['STATE']
if request.method == 'POST':
# Get the slider value and radio selection from the form
if request.form['radio'] == 'L':
state.set("pos_1", int(request.form['slider']))
else:
state.set("pos_2", int(request.form['slider']))
state.set("last_update", time.time())
return jsonify(success=True)
return render_template_string(template)
def webserver(state):
app.config['STATE'] = state
app.run(host='0.0.0.0', use_reloader=False, debug=True)
def run_game():
state.set("pos_2", 0)
state.set("pos_1", 0)
state.set("last_update", time.time())
g = pong_game.Game()
segMonster.initSock("10.24.200.22", 7536)
while True:
proc = subprocess.Popen("test") # for a "Screensaver"
time.sleep(1)
FPS = 5
counter = 5
while time.time() - 1*60 < state.get("last_update"):
proc.kill()
time.sleep(1/FPS)
g.pos_1 = state.get("pos_1")
g.pos_2 = state.get("pos_2")
if g.update():
FPS = 5
rawdata = segMonster.convertToDispLayout(g.state_to_monster())
segMonster.sendData(rawdata) # send to display
segMonsterSimulator.sendData(rawdata) # send to display simulation
counter += 1
if counter == FPS:
FPS += 1
counter = 0
class SharedState():
def __init__(self):
self.lock = threading.Lock()
self.state = dict()
def get(self, key):
with self.lock:
return self.state.get(key)
def set(self, key, value):
with self.lock:
self.state[key] = value
if __name__ == '__main__':
state = SharedState()
web_thread = threading.Thread(target=webserver, args=(state,))
web_thread.start()
run_game()