-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
144 lines (127 loc) · 3.93 KB
/
game.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
class Game {
constructor() {
this.delayTime = 75;
this.gameWidth = 25;
this.gameHeight = 25;
this.direction = "d";
const self = this;
this.loopRuns = true;
this.alive = true;
this.graphics = new Graphics({
cellWidth: 10,
cellHeight: 10,
spcBetweenSquares: 2,
backgroundColor: "green",
squareColor: "pink",
snakeColor: "red",
headColor: "#A62A2A",
foodColor: "black",
boardWidth: this.gameWidth,
boardHeight: this.gameHeight,
});
this.createPlayers(this.gameWidth, this.gameHeight);
}
createPlayers(width, height) {
const names = ["Mrello"];
this.players = names.map(name => {
return new Player(name);
});
this.players.forEach((player) => {
player.board = new Board(width, height);
});
}
placeSnake(x, y, player) {
let target = player.board.fetchSquare(x, y);
target.setSnakeHere();
player.setHeadLocation(target);
}
growSnake(player, currHead) {
if (player.head.foodHere == true) {
player.head.foodHere = false;
this.placeFood();
player.size++;
}
if (player.tail.length == player.size) {
let missingno = player.tail.splice(0, 1);
missingno[0].setNeutral();
}
player.tail.push(currHead);
}
moveSnake(player, direction) {
let origin = player.head;
this.growSnake(player, origin);
let target = null;
if (direction == "d") {
// if (origin.x + 1 >= player.board.width) {
// origin.x -= player.board.width;
// }
target = player.board.fetchSquare(origin.x + 1, origin.y);
}
else if (direction == "w") {
// if (origin.y == 0) {
// origin.y += player.board.height;
// }
target = player.board.fetchSquare(origin.x, origin.y - 1);
}
else if (direction == "a") {
target = player.board.fetchSquare(origin.x - 1, origin.y);
}
else if (direction == "s") {
target = player.board.fetchSquare(origin.x, origin.y + 1);
}
if (target.snakeHere == true) {
this.alive = false;
}
target.setSnakeHere();
player.setHeadLocation(target);
}
placeFood() {
let foodPlace = this.players[0].pickRandSquare();
foodPlace.setFoodHere();
}
draw() {
for (const player of this.players) {
this.graphics.drawBoard(player);
}
}
start () {
let firstX = Math.floor(this.gameWidth / 2);
let firstY = Math.floor(this.gameHeight / 2);
this.placeSnake(firstX, firstY, this.players[0]);
this.placeFood();
this.draw();
var eventsKey = {
w: "KeyW",
s: "KeyS",
d: "KeyD",
a: "KeyA",
p: "KeyP",
space: "space"
};
document.addEventListener("keydown", () => {
if (event.code == eventsKey.s && this.direction != "w") {
this.direction = "s";
}
if (event.code == eventsKey.w && this.direction != "s") {
this.direction = "w";
}
if (event.code == eventsKey.d && this.direction != "a") {
this.direction = "d";
}
if (event.code == eventsKey.a && this.direction != "d") {
this.direction = "a";
}
});
if (this.alive) {
window.setInterval(this.update.bind(this), this.delayTime);
}
}
update() {
if (this.alive) {
this.moveSnake(this.players[0], this.direction);
this.draw();
}
}
}
var game = new Game();
game.start();