-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (52 loc) · 1.86 KB
/
Copy pathapp.py
File metadata and controls
62 lines (52 loc) · 1.86 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
from flask import Flask, render_template, request
from flask_sockets import Sockets
import json
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
app = Flask(__name__)
sockets = Sockets(app)
# Game state variables
current_direction = None
players = []
food = [100, 100] # Initial food position
@app.route('/')
def index():
return render_template('index.html')
@app.route('/favicon.ico')
def favicon():
return '', 204
@sockets.route('/game')
def game_socket(ws):
global current_direction, players
while not ws.closed:
message = ws.receive()
if message is not None:
data = json.loads(message)
player_id = ws # Assuming socket object uniquely identifies player
for player in players:
if player['id'] == player_id:
player['direction'] = data.get('direction')
break
current_direction = update_game_state(players)
ws.send(json.dumps({'direction': current_direction, 'players': players, 'food': food}))
def update_game_state(players):
for player in players:
direction = player.get('direction')
if direction:
move_player(player, direction)
check_collisions()
def move_player(player, direction):
x, y = player['position']
dx, dy = direction
player['position'] = [x + dx * BLOCK_SIZE, y + dy * BLOCK_SIZE]
def check_collisions():
# Implement collision detection logic here
pass
@app.before_request
def verify_websocket_handshake():
if request.path == '/game' and request.headers.get('Upgrade', '').lower() != 'websocket':
return 'Invalid WebSocket handshake request.', 400
if __name__ == '__main__':
server = pywsgi.WSGIServer(('localhost', 3000), app, handler_class=WebSocketHandler)
print("Server running on port 3000")
server.serve_forever()