-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
163 lines (153 loc) · 4.32 KB
/
hello.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
from flask import Flask
app = Flask(__name__)
snake_html = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Snake</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/p5.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="score">0</div>
<!-- <script src="sketch.js"></script>
<script src="snake.js"></script> -->
<script>
function Food(x, y) {
this.x = x;
this.y = y;
this.show = () => {
fill(100, 250, 100);
rect(this.x * scl, this.y * scl, scl, scl);
};
}
function Snake(x, y) {
// this.x = x;
// this.y = y;
this.xdir = 1;
this.ydir = 0;
// this.tails = [];
this.X = [];
this.X.push(x);
this.Y = [];
this.Y.push(y);
this.food;
// console.log(this.X, this.Y);
this.show = () => {
fill(200, 50, 150);
rect(this.X[0] * scl, this.Y[0] * scl, scl, scl);
fill(250, 100, 200);
for (let i = 1; i < this.X.length; i++) {
rect(this.X[i] * scl, this.Y[i] * scl, scl, scl);
}
// rect(this.x * scl, this.y * scl, scl, scl);
};
this.move = () => {
for (let i = this.X.length - 1; i > 0; i--) {
this.X[i] = this.X[i - 1];
this.Y[i] = this.Y[i - 1];
}
this.X[0] = (this.X[0] + this.xdir + 30) % 30;
this.Y[0] = (this.Y[0] + this.ydir + 30) % 30;
};
this.updir = (x, y) => {
this.xdir = x;
this.ydir = y;
};
this.death = () => {
// let dis;
for (let i = 1; i < this.X.length; i++) {
let dis = dist(this.X[0], this.Y[0], this.X[i], this.Y[i]);
console.log(dis, 'distance');
if (dis < 1) {
let text = 'Your score is ';
text += score;
alert(text);
let len = this.X.length;
for (let j = 1; j < len; j++) {
this.X.pop();
this.Y.pop();
}
score = 0;
document.getElementById('score').innerText = score;
return;
}
}
};
this.eat = () => {
if (this.X[0] == this.food.x && this.Y[0] == this.food.y) {
this.X.push(this.food.x);
this.Y.push(this.food.y);
this.generate();
score++;
document.getElementById('score').innerText = score;
}
};
this.generate = () => {
let x = Math.floor(Math.random() * 30),
y = Math.floor(Math.random() * 30);
console.log(x, y);
if (x == this.x && y == this.y) {
this.generate();
return;
}
for (let i = 0; i < this.X.length; i++) {
if (this.X[i] == x && this.Y[i] == y) {
this.generate();
return;
}
}
this.food = new Food(x, y);
};
}
let scl = 20,
h = scl * 30,
w = scl * 30,
cnt = 0,
score = document.getElementById('score').innerText;
let snake;
function setup() {
createCanvas(h, w);
fill(50);
snake = new Snake(8, 8);
snake.generate();
}
function draw() {
background(100);
frameRate(12);
snake.show();
snake.food.show();
frameRate(12 + score / 2);
// if (cnt == 5) {
snake.move();
snake.death();
snake.eat();
// cnt %= 5;
// }
// cnt++;
// rect(8 * scl, 8 * scl, scl, scl);
// snake.death();
}
function keyPressed() {
if (snake.xdir != 1 && keyCode == LEFT_ARROW) {
snake.updir(-1, 0);
}
if (snake.xdir != -1 && keyCode == RIGHT_ARROW) {
snake.updir(1, 0);
}
if (snake.ydir != -1 && keyCode == DOWN_ARROW) {
snake.updir(0, 1);
}
if (snake.ydir != 1 && keyCode == UP_ARROW) {
snake.updir(0, -1);
}
}
</script>
</body>
</html>
'''
@app.route('/snake')
def hello_world():
return snake_html