This repository has been archived by the owner on Aug 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.ts
220 lines (197 loc) · 5.49 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Tune these
const params = {
fps: 5
}
const actions = {
// Initialize the current game state
initialize: function (): State {
return {
puckPos: new Vector({
x: 2, y: 0
}),
platformPos: new Vector({
x: 2, y: 4
}),
puckVel: new Vector({
x: Math.floor(Math.random() * 3 - 1),
y: 1
}),
platformVel: new Vector({
x: 0, y: 0
}),
running: true,
moved: false
}
},
// Move the platform left
moveLeft: function (state: State): State {
if (state.moved) {
return state
}
return {
puckPos: state.puckPos,
platformPos: state.platformPos,
puckVel: state.puckVel,
running: state.running,
platformVel: state.platformVel.add(new Vector({
x: -1, y: 0
})),
moved: true
}
},
// Move the platform right
moveRight: function (state: State): State {
if (state.moved) {
return state
}
return {
puckPos: state.puckPos,
platformPos: state.platformPos,
puckVel: state.puckVel,
running: state.running,
platformVel: state.platformVel.add(new Vector({
x: 1, y: 0
})),
moved: true
}
},
// Advances the game state by 1 step
tick: function (state: State): State {
if (!state.running) {
return state
}
let platformPos = state.platformPos
let platformVel = state.platformVel
let puckPos = state.puckPos
let puckVel = state.puckVel
let running = state.running
let moved = state.moved
// Update the position of the platform
platformPos = state.platformPos.add(state.platformVel)
if (platformPos.x > 3 || platformPos.x < 1) {
// Prevent the platform from exceeding the boundaries of the screen
platformPos = state.platformPos
}
// Reset the velocity of the platform
platformVel = new Vector({
x: 0,
y: 0
})
// Update the position of the puck
puckPos = puckPos.add(puckVel)
// Prevent the puck from exceeding the horizontal boundaries of the screen
if (puckPos.x < 0) {
puckPos = puckPos.add(new Vector({
x: 2,
y: 0
}))
puckVel = new Vector({
x: 1,
y: puckVel.y
})
} else if (puckPos.x > 4) {
puckPos = puckPos.add(new Vector({
x: -2,
y: 0
}))
puckVel = new Vector({
x: -1,
y: puckVel.y
})
}
// Prevent the puck from exceeding the vertical boundaries of the screen
if (puckPos.y < 0) {
puckPos = puckPos.add(new Vector({
x: 0,
y: 2
}))
puckVel = new Vector({
x: puckVel.x,
y: 1
})
} else if (puckPos.y == platformPos.y) {
if (puckPos.x >= platformPos.x - 1
&& puckPos.x <= platformPos.x + 1) {
puckVel = new Vector({
x: Math.floor(Math.random() * 3 - 1),
y: -1
})
puckPos = new Vector({
x: state.puckPos.x + puckVel.x,
y: platformPos.y - 2
})
}
} else if (puckPos.y > platformPos.y) {
// The player failed to catch the puck. Game over!
running = false
}
moved = false
return {
puckPos: puckPos,
platformPos: platformPos,
puckVel: puckVel,
platformVel: platformVel,
running: running,
moved: moved
}
}
}
let s: State = actions.initialize()
basic.forever(function () {
// Advance the game state
s = actions.tick(s)
// Is the game over?
if (!s.running) {
basic.showIcon(IconNames.Sad)
return
}
// Clear the screen
for (let x = 0; x < 5; x++) {
for (let y = 0; y < 5; y++) {
led.unplot(x, y)
}
}
// Draw the platform on the screen
led.plot(s.platformPos.x, s.platformPos.y)
led.plot(s.platformPos.x - 1, s.platformPos.y)
led.plot(s.platformPos.x + 1, s.platformPos.y)
// Draw the puck on the screen
led.plot(s.puckPos.x, s.puckPos.y)
// Delay the game loop to meet the desired frame rate
basic.pause(Math.floor(1000 / params.fps))
})
// Handle left button presses
input.onButtonPressed(Button.A, function () {
s = actions.moveLeft(s)
})
// Handle right button presses
input.onButtonPressed(Button.B, function () {
s = actions.moveRight(s)
})
// Interface definitions for various classes used in the game
interface State {
puckPos: Vector
platformPos: Vector
puckVel: Vector
platformVel: Vector
running: boolean
moved: boolean
}
interface Position {
x: number
y: number
}
class Vector {
x: number
y: number
constructor(pos: Position) {
this.x = pos.x
this.y = pos.y
}
add(v: Vector): Vector {
return new Vector({
x: this.x + v.x,
y: this.y + v.y
})
}
}