-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
71 lines (66 loc) · 1.75 KB
/
script.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
class Game {
constructor () {
this.score = 0
}
addChip(value, x, y) {
const main = document.querySelector('main')
let chip = new Chip(value, x, y)
chip.mount(main)
}
update() {
const h1 = document.querySelector('h1')
h1.textContent = 'Score: '+this.score
}
resetGame() {
const chips = document.querySelectorAll('div')
chips.forEach(element => {
element.remove()
});
this.score = 0
this.update()
for (let i=0 ; i<10 ; i++) {
const value = Math.ceil(Math.random()*3)
const x = Math.random()*(innerWidth-100)
const y = Math.random()*(innerHeight-100)
this.addChip(value, x, y)
}
}
}
class Chip {
constructor (value, x, y) {
this.value = value
this.x = x
this.y = y
}
render() {
const chip = document.createElement('div')
if (this.value === 1) {
chip.className = 'chip chip--1'
}
else if (this.value === 2) {
chip.className = 'chip chip--2'
}
else if (this.value === 3) {
chip.className = 'chip chip--3'
}
chip.style.top = this.y+'px'
chip.style.left = this.x+'px'
chip.textContent = this.value
chip.addEventListener('click', () => {
game.score += this.value
game.update()
chip.remove()
})
return chip
}
mount(parent) {
this.element = this.render()
parent.appendChild(this.element)
}
}
const game = new Game()
game.resetGame()
const buttonReset = document.querySelector('.reset')
buttonReset.addEventListener('click', () => {
game.resetGame()
})