-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
123 lines (107 loc) · 2.97 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
const balance = document.querySelector('#balance');
let money = 0;
// make the user have money instead just so it is more organized.
const user = {
money: 0,
multiplier: 1,
// refreshes the score
// call this function everytime the score changes
update: function () {
balance.textContent = `$${this.money}`;
},
scored: function () {
this.money += this.multiplier;
this.update();
},
reset: function () {
this.money = 0;
this.update();
},
};
// function randomize() {
// let randomIndex = Math.floor(Math.random() * grid.squares.length);
// let randomSquare = grid.squares[randomIndex];
// randomSquare.color = 'rgba(255,255,255,1)';
// grid.render();
// }
// let intervalId = setInterval(() => {
// grid.clear();
// randomize();
// }, 1000);
// let randomSquare;
function appear() {
return new Promise((resolve, reject) => {
setTimeout(() => {
clearCanvas();
let randomIndex = Math.floor(Math.random() * grid.squares.length);
randomSquare = grid.squares[randomIndex];
randomSquare.color = 'rgba(255,255,255,1)';
grid.render();
if (randomSquare.color === 'rgba(255,255,255,1)') {
resolve(randomSquare); // return the square to use it in the promise chain.
} else {
reject('There was an error.');
}
}, 1000);
});
}
function disappear(square) {
return new Promise((resolve, reject) => {
setTimeout(() => {
clearCanvas();
square.color = 'rgba(0,0,0,1)';
grid.render();
if (square.color === 'rgba(0,0,0,1)') {
resolve('it turned black');
} else {
reject('error');
}
}, 1000);
});
}
let gameStart = false;
async function playGame() {
if (!gameStart) {
gameStart = true;
while (gameStart) {
await appear()
.then(disappear) // the square gets passed in as a parameter for the disappear function.
.catch((err) => console.log(err));
}
// for (let i = 0; i < 1000; i++) {
// await appear()
// .then(disappear) // the square gets passed in as a parameter for the disappear function.
// .catch((err) => console.log(err));
// }
}
}
// playGame();
// Random Squares appearing function Gamemode:
/* function random() {
let filteredGrid = [];
let intervalId = setInterval(() => {
if (!filteredGrid.length) {
filteredGrid = grid.squares.filter((square) => {
if (square.color !== 'rgba(255,255,255,1)') {
return true;
} else {
return false;
}
});
} else if (filteredGrid.length) {
filteredGrid = filteredGrid.filter((square) => {
if (square.color !== 'rgba(255,255,255,1)') {
return true;
} else {
return false;
}
});
}
if (filteredGrid.length) {
let randomIndex = Math.floor(Math.random() * filteredGrid.length);
filteredGrid[randomIndex].color = 'rgba(255,255,255,1)';
filteredGrid[randomIndex].draw();
}
}, 1000);
}
random(); */