-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
163 lines (163 loc) · 12.9 KB
/
App.js
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
"use strict";
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
var len = document.getElementById('len');
var step = 25; // 方块大小
var originX = canvas.width % step / 2;
var originY = canvas.height % step / 2;
var endX = canvas.width - originX;
var endY = canvas.height - originY;
var App = /** @class */ (function () {
function App() {
this.map = new Map(step);
this.snake = new Snake();
this.food = new Food(this.getPoint());
}
App.prototype.run = function () {
this.init();
this.update();
this.draw();
};
App.prototype.init = function () {
};
App.prototype.update = function () {
var _this = this;
setInterval(function () {
_this.snakeRun();
}, 200);
// 电脑方向键控制
onkeydown = function (e) {
var oldVector = _this.snake.vector;
switch (e.key) {
case 'ArrowUp':
_this.snake.vector = [0, -step];
break;
case 'ArrowDown':
_this.snake.vector = [0, step];
break;
case 'ArrowLeft':
_this.snake.vector = [-step, 0];
break;
case 'ArrowRight':
_this.snake.vector = [step, 0];
break;
default:
return;
}
// 长按加速
if (oldVector[0] === _this.snake.vector[0] && oldVector[1] === _this.snake.vector[1]) {
_this.snakeRun();
}
};
};
App.prototype.snakeRun = function () {
this.snake.move();
// 食物碰撞
if (this.snake.body[0][0] === this.food.x && this.snake.body[0][1] === this.food.y) {
this.snake.eat();
len.innerText = (this.snake.body.length - 1).toString();
console.log("\uD83D\uDC0D.len = " + this.snake.body.length);
var point = this.getPoint();
this.food.x = point[0];
this.food.y = point[1];
}
};
App.prototype.draw = function () {
var _this = this;
requestAnimationFrame(function () { return _this.draw(); });
this.map.draw();
this.snake.draw();
this.food.draw();
};
App.prototype.getPoint = function () {
var stepCountX = (endX - originX) / step - 1;
var stepCountY = (endY - originY) / step - 1;
var x;
var y;
var key;
do {
x = Math.round(Math.random() * stepCountX) * step + originX;
y = Math.round(Math.random() * stepCountY) * step + originY;
key = this.snake.body.some(function (item) {
if (x === item[0] && y === item[1])
return true;
});
} while (key);
return [x, y];
};
return App;
}());
var Map = /** @class */ (function () {
function Map(size) {
this.size = size;
}
Map.prototype.draw = function () {
context.fillStyle = 'black';
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = 'gray';
for (var i = originY; i <= canvas.height; i += this.size) {
context.beginPath();
context.moveTo(0, i);
context.lineTo(canvas.width, i);
context.stroke();
}
for (var i = originX; i <= canvas.width; i += this.size) {
context.beginPath();
context.moveTo(i, 0);
context.lineTo(i, canvas.height);
context.stroke();
}
};
return Map;
}());
var Snake = /** @class */ (function () {
function Snake() {
this.body = [[originX, originY]];
this.vector = [step, 0];
}
Snake.prototype.eat = function () {
var tail = this.body[this.body.length - 1];
this.body.push(tail);
this.move();
};
Snake.prototype.move = function () {
var newHead = [this.body[0][0] + this.vector[0], this.body[0][1] + this.vector[1]];
// 碰壁处理
if (newHead[0] > endX - step)
newHead[0] = originX;
if (newHead[0] < originX)
newHead[0] = endX - step;
if (newHead[1] > endY - step)
newHead[1] = originY;
if (newHead[1] < originY)
newHead[1] = endY - step;
this.body.unshift(newHead);
this.body.pop();
};
Snake.prototype.draw = function () {
context.fillStyle = '#888888';
this.body.forEach(function (item, index) {
if (index !== 0) {
context.fillStyle = '#CCCCCC';
}
context.fillRect(item[0], item[1], step, step);
});
};
return Snake;
}());
var Food = /** @class */ (function () {
function Food(xy) {
this.x = xy[0];
this.y = xy[1];
}
Food.prototype.draw = function () {
context.fillStyle = 'green';
context.fillRect(this.x, this.y, step, step);
};
return Food;
}());
var app = new App();
app.run();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQXBwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiQXBwLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxJQUFNLE1BQU0sR0FBeUMsUUFBUSxDQUFDLGNBQWMsQ0FBQyxRQUFRLENBQUMsQ0FBQztBQUN2RixJQUFNLE9BQU8sR0FBNkIsTUFBTSxDQUFDLFVBQVUsQ0FBQyxJQUFJLENBQUUsQ0FBQztBQUNuRSxNQUFNLENBQUMsTUFBTSxHQUFHLE1BQU0sQ0FBQyxXQUFXLENBQUM7QUFDbkMsTUFBTSxDQUFDLEtBQUssR0FBRyxNQUFNLENBQUMsVUFBVSxDQUFDO0FBRWpDLElBQU0sR0FBRyxHQUE2QixRQUFRLENBQUMsY0FBYyxDQUFDLEtBQUssQ0FBQyxDQUFDO0FBRXJFLElBQUksSUFBSSxHQUFXLEVBQUUsQ0FBQyxDQUFDLE9BQU87QUFDOUIsSUFBSSxPQUFPLEdBQVcsTUFBTSxDQUFDLEtBQUssR0FBRyxJQUFJLEdBQUcsQ0FBQyxDQUFDO0FBQzlDLElBQUksT0FBTyxHQUFXLE1BQU0sQ0FBQyxNQUFNLEdBQUcsSUFBSSxHQUFHLENBQUMsQ0FBQztBQUMvQyxJQUFJLElBQUksR0FBVyxNQUFNLENBQUMsS0FBSyxHQUFHLE9BQU8sQ0FBQztBQUMxQyxJQUFJLElBQUksR0FBVyxNQUFNLENBQUMsTUFBTSxHQUFHLE9BQU8sQ0FBQztBQUUzQztJQUFBO1FBQ1ksUUFBRyxHQUFRLElBQUksR0FBRyxDQUFDLElBQUksQ0FBQyxDQUFDO1FBQ3pCLFVBQUssR0FBVSxJQUFJLEtBQUssRUFBRSxDQUFDO1FBQzNCLFNBQU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