-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.ts
193 lines (192 loc) · 5.26 KB
/
pong.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Blocks that start pong game
*/
//% color="#ff5030" weight=15 icon="\uf1ec" block="Pong"
//% groups=['Pong']
namespace pong {
let canStart = false
let connected = false
let gameOver = false
let selectedGroup=0
let selected = 0
let planeX = 0
let direct;
const yDir = {
Up: 0,
Down: 1
}
const searchIcon = () =>
basic.showLeds(`
..##.
.#..#
.#..#
..##.
.#...
`)
let icon = () =>
basic.showLeds(`
##...
.....
.#...
.....
..##.
`)
/**
* Starts pong with menu
*/
//% block
export function StartGameWithMenu(): void {
icon();
input.onButtonPressed(Button.B, () => {
SelectGroup()
})
}
function SelectGroup(): void {
basic.showNumber(selectedGroup)
input.onButtonPressed(Button.A, () => {
selectedGroup++
if (selectedGroup > 9) selectedGroup = 0
basic.showNumber(selectedGroup)
})
input.onButtonPressed(Button.B, () => {
StartGame(selectedGroup)
})
}
/**
* Starts game without menu
* @param group radio group, where connects; eg.69
*/
//% block
//% group.min=0 group.max= 256
export function StartGame(group: number): void {
canStart = false
connected = false
gameOver = false
basic.clearScreen()
radio.setGroup(group)
searchIcon()
radio.sendString("connect")
radio.onReceivedString(function (name: string) {
if (name == "connect") {
connected = true
radio.sendString("connected");
}
if (name == "connected") {
connected = true
canStart = true
}
if (connected) {
basic.clearScreen()
basic.showNumber(3)
basic.pause(500)
basic.showNumber(2)
basic.pause(500)
basic.showNumber(1)
basic.pause(500)
basic.clearScreen()
if (canStart) radio.sendNumber(randint(0, 4))
led.plot(planeX, 4)
led.plot(planeX + 1, 4)
radio.onReceivedString((lol) => {
if (lol == "gameOver") {
gameOver = true
basic.showIcon(IconNames.Happy);
basic.pause(2000)
StartGameWithMenu()
}
})
}
});
radio.onReceivedNumber(function (num) {
if (connected) {
if (randint(0, 1) == 0) {
direct = Direction.Right;
} else {
direct = Direction.Left;
}
Game(num, direct);
}
})
input.onButtonPressed(Button.A, function () {
if (planeX > 0 && !(gameOver) && connected) {
led.unplot(planeX, 4)
led.unplot(planeX + 1, 4)
planeX += -1
led.plot(planeX, 4)
led.plot(planeX + 1, 4)
}
})
input.onButtonPressed(Button.B, function () {
if (planeX < 3 && !(gameOver) && connected) {
led.unplot(planeX, 4)
led.unplot(planeX + 1, 4)
planeX += 1
led.plot(planeX, 4)
led.plot(planeX + 1, 4)
}
})
radio.onReceivedValue(function (name, num) {
if (name == "0") {
direct = Direction.Left;
} else {
direct = Direction.Right;
}
Game(num, direct);
})
}
function Game(x: number, dir: Direction): void {
let y = 0;
gameOver = false;
let anotherScreen = false;
let ydir = yDir.Down;
x = 4 - x;
led.plot(x, 0);
basic.pause(300);
while (!gameOver && !anotherScreen) {
if (!(y == 4 && (x == planeX || x == planeX + 1)))
led.unplot(x, y);
if (dir == Direction.Right) {
x++;
if (x > 4) {
x = 3;
dir = Direction.Left;
}
}
else {
x--;
if (x < 0) {
x = 1;
dir = Direction.Right;
}
}
if (ydir == yDir.Down) {
y++;
if (y >= 4) {
if (x == planeX || x == planeX + 1) {
ydir = yDir.Up;
dir = randint(0, 1);
}
if (y > 4) gameOver = true;
}
}
else {
y--;
if (y < 0) {
anotherScreen = true;
radio.sendValue(dir.toString(), x)
}
}
led.plot(x, y);
if (y > 4) {
gameOver = true;
radio.sendString("gameOver")
}
basic.pause(300);
}
if (gameOver) {
basic.showIcon(IconNames.Sad)
basic.pause(2000)
StartGameWithMenu()
}
}
}