-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
178 lines (162 loc) · 5.26 KB
/
Copy pathScript.js
File metadata and controls
178 lines (162 loc) · 5.26 KB
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
174
175
176
177
178
document.addEventListener("DOMContentLoaded",main,false);
const MAX_WIDTH = 500;
const MAX_HEIGHT = 500;
const PIXEL_DIM = 20;
const GAMETICK = 70;
let id_game = null;
let campo;
let pos;
let direzione;
let maxPunteggio;
let remPunteggio;
let giro,tempo;
let elementi;
const colori = ["white","green","yellow","black"];
const dimensioni = [PIXEL_DIM,14,PIXEL_DIM,PIXEL_DIM];
const dispoElementi = [ [3,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,3,0,0,3,3,3,3,3,3,0,0,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,0,0,0,0,3,3,3,0,0,0,0,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,0,3,0,0,0,0,0,0,0,3,0,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,0,3,0,0,0,0,0,0,0,3,0,0,0,0,3,3,3,3],
[3,3,3,3,0,0,3,0,3,0,3,3,3,3,0,0,0,3,0,0,0,3,3,3,3],
[3,3,3,3,0,3,3,0,0,3,3,0,0,3,0,0,0,0,3,0,0,3,3,3,3],
[3,3,3,3,0,3,0,0,3,0,0,0,0,3,0,3,0,0,3,0,0,3,3,3,3],
[3,3,3,3,0,3,0,0,0,0,3,0,0,3,0,0,0,3,0,3,3,3,3,3,3],
[3,3,3,3,0,3,3,3,3,3,3,3,3,3,3,0,0,3,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,0,0,0,3,0,0,0,3,0,3,0,0,3,0,3,3,3,3],
[3,3,3,3,0,0,3,3,3,3,3,3,0,3,3,0,0,0,0,3,0,3,3,3,3],
[3,3,3,3,0,0,3,0,0,0,0,0,0,0,0,3,3,0,0,3,0,3,3,3,3],
[3,3,3,3,0,0,3,0,0,0,3,0,3,0,3,0,0,0,0,3,0,3,3,3,3],
[3,3,3,3,0,3,3,0,0,0,0,0,0,3,0,0,0,0,0,3,0,3,3,3,3],
[3,3,3,3,0,3,0,0,0,0,0,3,0,0,0,3,3,3,0,3,0,3,3,3,3],
[3,3,3,3,0,0,3,0,3,3,3,0,0,3,3,3,0,3,3,3,0,3,3,3,3],
[3,3,3,3,0,0,0,3,0,0,0,0,3,3,0,0,0,3,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,3,0,0,3,0,0,0,3,0,0,3,3,3,3,3,3,3,3],
[3,3,3,3,0,0,0,0,3,3,3,3,0,0,0,0,0,0,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,0,0,0,0,3,0,0,3,0,3,0,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,0,0,0,3,0,0,0,3,3,0,0,0,0,0,3,3,3,3],
[3,3,3,3,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,3,3,3,3],
[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
];
elementi = dispoElementi;
function getObj(id){
return document.getElementById(id);
}
function main(){
getObj("start").addEventListener("click",game);
document.addEventListener("keydown",(a) => {
if(id_game == null) return
let b = a.code;
console.log(b)
switch(b){
case "KeyR":
stopGame();
break;
case "KeyW":
case "ArrowUp":
direzione = [0,-1];
break;
case "KeyS":
case "ArrowDown":
direzione = [0,1];
break
case "KeyA":
case "ArrowLeft":
direzione = [-1,0];
break;
case "KeyD":
case "ArrowRight":
direzione = [1,0];
break;
}
})
campo = getObj("campoGioco").getContext("2d");
}
function game(){
if(id_game != null) return
reset()
id_game = setInterval(() => {
calcolaPos();
calcolatempo(),
checkFood();
clearCampo();
disegnaCampo();
disegnaQuadrato(campo,pos[0],pos[1],colori[2]);
setTempo();
checkPunti();
},GAMETICK);
}
function calcolaPos(){
let Npos = [pos[0] + direzione[0] , pos[1] + direzione[1]];
if(elementi[Npos[1]][Npos[0]] == 3) return
pos = Npos;
}
function checkPunti(){
if(remPunteggio==0){
stopGame();
}
}
function calcolatempo(){
giro++;
let ms = giro * GAMETICK;
let s = ms > 1000 ? (ms/1000).toFixed(0):0;
let m = s > 60 ? (s/60).toFixed(0):0;
tempo = `${m} : ${s%60} : ${ms%1000}`;
}
function disegnaCampo(){
for(let y = 0;y < elementi.length;y++){
for(let x = 0;x < elementi[y].length;x++){
let elemento = elementi[y][x];
let colore = colori[elemento];
let dimensione = dimensioni[elemento];
disegnaQuadrato(campo,x,y,colore,dimensione);
}
}
}
function trasforma(){
for(let i = 0; i < elementi.length;i++){
for(let j = 0; j < elementi[i].length;j++){
if(elementi[i][j] == 0){
elementi[i][j] = 1;
remPunteggio++;
}
}
}
}
function checkFood(){
if(elementi[pos[1]][pos[0]] == 1){
elementi[pos[1]][pos[0]] = 0;
maxPunteggio++;
remPunteggio--;
}
}
function stopGame(){
clearInterval(id_game);
clearCampo();
reset();
}
function setTempo(){
let ogg = getObj("Tmp");
ogg.innerText = `Tempo: ${tempo}`;
}
function reset(){
direzione = [1,0];
pos = [10,9];
id_game = null;
remPunteggio = 0;
trasforma();
maxPunteggio = 0;
giro=0;
elementi = dispoElementi;
}
function clearCampo(){
campo.fillStyle = "white";
campo.fillRect(0,0,MAX_WIDTH,MAX_HEIGHT);
}
function disegnaQuadrato(c,x,y,colore,dim = PIXEL_DIM,center = true){
c.fillStyle = colore;
x = center && dim != PIXEL_DIM && dim%2==0 ? x*20 + 10 - (dim/2) : x*20;
y = center && dim != PIXEL_DIM && dim%2==0 ? y*20 + 10 - (dim/2) : y*20;
c.fillRect(x,y,dim,dim);
}