-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontrols.js
More file actions
83 lines (79 loc) · 1.94 KB
/
controls.js
File metadata and controls
83 lines (79 loc) · 1.94 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
window.addEventListener("keydown",
(event)=>{
if(event.key=== "ArrowRight"){
if(checkifPossible(currentBlock,0,1)){
leftOff = (leftOff+1)%11
setTempBlock()
}
}
else if(event.key=== "ArrowDown"){
if(checkifPossible(currentBlock,1,0)){
topOff = (topOff+1)%20
score++;
setTempBlock()
}
}
else if(event.key=== "ArrowLeft"){
if(checkifPossible(currentBlock,0,-1)){
leftOff = (leftOff-1)%11
setTempBlock()
}
}
else if(event.key === "ArrowUp"){
topOff = topOff
cb = currentBlock.map(s=>[...s])
rotateBlock(cb)
if(checkifPossible(cb,0,0)){
currentBlock = cb
setTempBlock()
}
}
else if(event.key ==="c"){
setHold()
setTempBlock()
Holdpressed = true
}
else if(event.key ===" "){
gameOver = !gameOver
setTempBlock()
playPauseMusic();
}
else if(event.key==="Shift"){
topOff = tempTopOff
}
else if(event.key ==="Enter"){
gameStart = true;
}
})
const checkifPossible=(cb,xoff,yoff)=>{
for(let i=0;i<4;i++){
for(let j=0;j<4;j++){
if(cb[i][j] && staticGrid[i+topOff+xoff][j+leftOff+yoff]){
return false
}
}
}
return true
}
function rotateBlock(a) {
if(currentBlockType!=="o"){
var n=a.length;
if(currentBlockType!=="i")
n--;
for (var i=0; i<n/2; i++) {
for (var j=i; j<n-i-1; j++) {
var tmp=a[i][j];
a[i][j]=a[n-j-1][i];
a[n-j-1][i]=a[n-i-1][n-j-1];
a[n-i-1][n-j-1]=a[j][n-i-1];
a[j][n-i-1]=tmp;
}
}
if(leftOff===0){
leftOff++;
}
if(leftOff===9){
leftOff--;
}
}
};