-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
78 lines (65 loc) · 2.06 KB
/
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
73
74
75
76
77
78
from flask import Flask, render_template, jsonify, request
import random
app = Flask(__name__)
class SnakeGame:
def __init__(self):
self.width = 20
self.height = 20
self.snake = [(10, 10)]
self.direction = 'right'
self.food = self.generate_food()
self.score = 0
self.game_over = False
def generate_food(self):
while True:
food = (random.randint(0, self.width-1),
random.randint(0, self.height-1))
if food not in self.snake:
return food
def move(self, direction):
if self.game_over:
return
head = self.snake[0]
if direction == 'right':
new_head = ((head[0] + 1) % self.width, head[1])
elif direction == 'left':
new_head = ((head[0] - 1) % self.width, head[1])
elif direction == 'up':
new_head = (head[0], (head[1] - 1) % self.height)
elif direction == 'down':
new_head = (head[0], (head[1] + 1) % self.height)
if new_head in self.snake:
self.game_over = True
return
self.snake.insert(0, new_head)
if new_head == self.food:
self.score += 1
self.food = self.generate_food()
else:
self.snake.pop()
game = SnakeGame()
@app.route('/')
def home():
return render_template('index.html')
@app.route('/move', methods=['POST'])
def move():
direction = request.json.get('direction')
game.move(direction)
return jsonify({
'snake': game.snake,
'food': game.food,
'score': game.score,
'game_over': game.game_over
})
@app.route('/reset', methods=['POST'])
def reset():
global game
game = SnakeGame()
return jsonify({
'snake': game.snake,
'food': game.food,
'score': game.score,
'game_over': game.game_over
})
if __name__ == '__main__':
app.run(debug=True)