-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
173 lines (150 loc) · 4.52 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
164
165
166
167
168
169
170
171
172
173
import { Snake } from "./Snake.js";
import { Map } from "./Map.js";
import { Game } from "./Game.js";
var canvas = document.getElementById('thegame'),
context = canvas.getContext('2d');
// command listener
document.addEventListener("keydown",direction);
function direction(event){
let key = event.keyCode;
// check if the speed key is pressed
if (key == 16) {
if(game.gameTick >= 50){
game.gameTick-=5
}
}else{ // check the direction
if( (key == 37 || key == 65) && direct != "RIGHT"){
direct = "LEFT";
}else if( ( key == 38 || key == 87) && direct != "DOWN"){
direct = "UP";
}else if( (key == 39 || key == 68) && direct != "LEFT"){
direct = "RIGHT";
}else if( ( key == 40 || key == 83) && direct != "UP"){
direct = "DOWN";
}
game.snake.direct = direct;
}
}
document.addEventListener("keyup",function(event) {
let key = event.keyCode;
if (key == 16) {
game.gameTick = defaultTick;
console.log("cancel")
}
})
document.getElementById("start-button").addEventListener("click", function(){
if(!game.gameStarted ){
let level = document.getElementById("level-select").value
switch (level) {
case "level1":
game = new Game({ctx:context, tick:gameTick, blockSize:blockSize});
break;
case "level2":
game = new Game({
ctx:context,
tick:gameTick,
blockSize:blockSize,
map:new Map({
name:"Border",
ctx:context,
border:true,
cellHeight:blockSize,
cellWidth:blockSize
})
});
break;
case "level3":
game= new Game({
ctx:context,
tick:gameTick,
blockSize:blockSize,
map:new Map({
ctx:context,
name:"Test",
rectangles:level3rects,
border:true,
cellHeight:blockSize,
cellWidth:blockSize,
})
})
default:
break;
}
game.start();
}
})
document.getElementById("stop-button").addEventListener("click", function(){
game.stop();
document.getElementById("continue-button").style.display = "block";
this.style.display = "none";
})
document.getElementById("continue-button").addEventListener("click", function(){
game.resume();
document.getElementById("stop-button").style.display = "block";
this.style.display = "none";
})
document.getElementById("reset-button").addEventListener("click", function(){
game.reset();
document.getElementById("stop-button").style.display = "block";
document.getElementById("continue-button").style.display = "none";
})
document.getElementById("restart-button").addEventListener("click", function(e) {
document.getElementById("reset-button").click();
document.getElementById("start-button").click();
document.getElementById("stop-button").disabled = false;
})
// document.getElementById("start-button").addEventListener("click", function(){
// // game.reset();
// })
document.getElementById("block-size").addEventListener("change",function(){
let value = parseInt(this.value);
if(value > 2){
showWarnings();
}else{
hideWarnings();
}
blockSize = value*10;
game.blockSize = value;
})
document.getElementById("game-tick").addEventListener("change",function(){
let value = parseInt(this.value);
console.log(value)
gameTick = value;
game.gameTick = value;
})
document.getElementById("level-select").addEventListener("click", function(){
// still no idea what to put here
})
function showWarnings(){
document.querySelector(".warning").style.display = "block";
}
function hideWarnings(){
document.querySelector(".warning").style.display = "none";
}
var level3rects = [
{
x:3,
y:3,
w:10,
h:1
},
{
x:15,
y:3,
w:1,
h:10
}
];
var level4rects = [
];
var highScore = 0;
var blockSize = 10;
var gameTick = 100;
var defaultTick = 100;
var direct = "";
var game = new Game({
ctx:context,
tick:gameTick,
blockSize:blockSize
});
console.log(game)