-
Notifications
You must be signed in to change notification settings - Fork 0
/
huntTheDot.js
99 lines (91 loc) · 2.28 KB
/
huntTheDot.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
let curY = 0
let curX = 0
let y = 0
let x = 0
let counter = 0
let gameWon = false
let gameStarted = false
input.onButtonPressed(Button.B, () => {
gameStarted = false
gameWon = false
counter = 0
basic.showLeds(`
. . # . .
. # . . .
# # # # #
. # . . .
. . # . .
`)
})
input.onButtonPressed(Button.A, () => {
//update game state
gameStarted = true
gameWon = false
//animate to say we are hiding a dot
for (let i = 0; i < 2; i++) {
basic.showIcon(IconNames.SmallDiamond)
basic.pause(250)
basic.showIcon(IconNames.Diamond)
basic.pause(250)
}
//where is the dot hiding
x = Math.random(5)
y = Math.random(5)
//start in the middle
curX = 2
curY = 2
led.plot(curX, curY)
//while we haven't found the dot use tilt to move the dot
while (!(gameWon)) {
if (input.acceleration(Dimension.X) < 0 && curX > 0) {
curX = curX - 1
}
if (input.acceleration(Dimension.X) > 0 && curX < 4) {
curX = curX + 1
}
if (input.acceleration(Dimension.Y) < 0 && curY > 0) {
curY = curY - 1
}
if (input.acceleration(Dimension.Y) > 0 && curY < 4) {
curY = curY + 1
}
//clear the screen and show where the current guess is
basic.clearScreen()
led.plot(curX, curY)
//if the dot is found animate it's position
if (curX == x && curY == y) {
gameWon = true
counter = counter + 1
basic.clearScreen()
for (let i = 0; i < 4; i++) {
led.plot(curX, curY)
basic.pause(250)
basic.clearScreen()
basic.pause(250)
}
basic.showIcon(IconNames.Yes)
basic.pause(500)
basic.showNumber(counter)
basic.pause(500)
}
basic.pause(250)
}
//update the game state and show how to restart
gameStarted == false
basic.showLeds(`
. . # . .
. # . . .
# # # # #
. # . . .
. . # . .
`)
})
while (!(gameStarted)) {
basic.showLeds(`
. . # . .
. # . . .
# # # # #
. # . . .
. . # . .
`)
}