forked from wvillanueva/Pang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pang.ino
328 lines (283 loc) · 7.16 KB
/
Pang.ino
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
PANG
Copyright (C) 2015 Wenceslao Villanueva Jr.
All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
*/
#include "globals.h"
#include "bitmaps.h"
#include "paddle.h"
#include "ball.h"
#include "sound.h"
// GameState
// 0 = Game Not Started
// 1 = Main Menu
// 2 = In Game
// 3 = Game Over
int GameState = 0;
Paddle Paddle_1;
Paddle* Paddle_1_ptr;
Paddle Paddle_2;
Paddle* Paddle_2_ptr;
Ball GameBall;
Ball* GameBall_ptr;
int Player1Score;
int Player2Score;
int Winner;
void setup()
{
Paddle_1_ptr = NULL;
Paddle_2_ptr = NULL;
GameBall_ptr = NULL;
arduboy.begin();
// audio setup
tunes.initChannel(PIN_SPEAKER_1);
#ifndef AB_DEVKIT
// if not a DevKit
tunes.initChannel(PIN_SPEAKER_2);
#else
// if it's a DevKit
tunes.initChannel(PIN_SPEAKER_1); // use the same pin for both channels
tunes.toneMutesScore(true); // mute the score when a tone is sounding
#endif
milliPerFrame = (1.0f / targetFPS);
lastUpdateTime = millis();
debug = 0.0f;
TitleXPos = screenWidth / 2 - ( 62 / 2 );
// If Debugging, activate the Serial Monitor
//Serial.begin( 9600 );
}
void loop()
{
// Cap FPS at targetFPS
if ( millis() < lastUpdateTime + milliPerFrame ) return;
lastUpdateTime = millis();
arduboy.clear();
// Execute the loop depending on Game State
switch ( GameState )
{
// Boot
case 0:
Bootup();
break;
// Menu
case 1:
MenuLoop();
break;
// Game
case 2:
GameLoop();
break;
// Game Over
case 3:
DrawGameOver();
GameBall.Hide();
break;
default:
break;
}
debug = GameBall.serveDirection;
// arduboy.drawLine( 0, 0, 0, 128, 1);
// arduboy.setTextSize(1);
// arduboy.setCursor( 20, 20 );
// arduboy.print(debug);
arduboy.display();
}
void Bootup()
{
tunes.tone( 987, 160 );
delay(160);
tunes.tone( 1318, 350 );
delay(2000);
GameState = 1;
tunes.playScore( titleScore );
}
// The simple front end main menu
void MenuLoop()
{
arduboy.drawSlowXYBitmap( TitleXPos, 20, title, 62, 20, 1 );
if ( arduboy.buttonsState() )
{
GameState = 2;
ResetGame();
GameBall.Show();
GameBall.Reset();
TimeToServe = 2;
}
}
// The game loop!
void GameLoop()
{
// Player One on the cross pad
if ( arduboy.pressed( UP_BUTTON ) )
{
Paddle_1.Move( paddleMoveSpeed );
}
else if ( arduboy.pressed( DOWN_BUTTON ) )
{
Paddle_1.Move( -paddleMoveSpeed );
}
// Player Two on the A and B Buttons
if ( arduboy.pressed( B_BUTTON ) )
{
Paddle_2.Move( paddleMoveSpeed );
}
else if ( arduboy.pressed( A_BUTTON ) )
{
Paddle_2.Move( -paddleMoveSpeed );
}
// If the ball is in play, update it
if ( GameBall.ballInPlay )
{
CheckCollision();
GameBall.Simulate();
}
// Otherwise we're waiting to serve
else if ( TimeToServe > 0 )
{
TimeToServe -= milliPerFrame;
}
else if ( TimeToServe <= 0 )
{
GameBall.Serve();
}
// The half court line
arduboy.drawLine( 63, 0, 63, 63, 1);
// Draw all the things!!
Paddle_1.Draw();
Paddle_2.Draw();
GameBall.Draw();
DrawScores();
}
// Using built in print to put up the scores.
void DrawScores()
{
arduboy.setTextSize( 1 );
arduboy.setCursor( screenWidth / 2 - 15, 5 );
arduboy.print( Player1Score );
arduboy.setCursor( screenWidth / 2 + 10, 5 );
arduboy.print( Player2Score );
}
// Is the game over? If so record it and return true/false
bool CheckGameOver()
{
if ( Player1Score >= PlayToScore )
{
Winner = 1;
GameOver();
return true;
}
else if ( Player2Score >= PlayToScore )
{
Winner = 2;
GameOver();
return true;
}
return false;
}
// Game Over! Set the time out for the Game Over screen and update game state
void GameOver()
{
TimeOnGameOverScreen = TimeOnGameOverScreenDefault;
GameState = 3; // Game Over
}
// Draw the Game Over Screen
void DrawGameOver()
{
arduboy.setTextSize( 1 );
arduboy.setCursor( screenWidth / 2 - 40, screenHeight / 2 );
sprintf(text, "Player %u Wins!", Winner);
arduboy.print( text );
TimeOnGameOverScreen -= milliPerFrame;
if ( TimeOnGameOverScreen <= 0 )
{
GameState = 1;
}
}
// Reset the game for the next play
void ResetGame()
{
Player1Score = 0;
Player2Score = 0;
int paddleStartY = (screenHeight / 2);
if ( Paddle_1_ptr == NULL )
{
Paddle_1 = Paddle( PaddleXPositions - 1, paddleStartY, PaddleWidth, PaddleHeight, 1 );
Paddle_1_ptr = &Paddle_1;
}
if ( Paddle_2_ptr == NULL )
{
Paddle_2 = Paddle( screenWidth - PaddleXPositions - 1, paddleStartY, PaddleWidth, PaddleHeight, 2 );
Paddle_2_ptr = &Paddle_2;
}
if ( GameBall_ptr == NULL )
{
GameBall = Ball( screenWidth / 2, screenHeight / 2, 4 );
GameBall_ptr = &GameBall;
}
}
// Perhaps not the most elegant collision system but it basically checks bounds in the X and then bounds in the Y and
// sees if the graphics are going to collide. This took the most time to write and I personally want
// to relearn methods of collision on hardware that doesn't run PhysX haha!
void CheckCollision()
{
int paddle1XDelta = GameBall.posX - Paddle_1.posX;
int paddle1YDelta = GameBall.posY - Paddle_1.posY;
int paddle2XDelta = GameBall.posX - Paddle_2.posX;
int paddle2YDelta = GameBall.posY - Paddle_2.posY;
int collisionYDeltaMax_P1 = (Paddle_1.sizeY / 2) + 1 + (GameBall.ballSize / 2);
int collisionYDeltaMax_P2 = (Paddle_2.sizeY / 2) + 1 + (GameBall.ballSize / 2);
// Player 2 scored
if ( GameBall.posX < ( -2 * GameBall.ballSize ) )
{
// Score
Player2Score++;
GameBall.Hide();
if ( !CheckGameOver() )
{
GameBall.Show();
GameBall.Reset();
TimeToServe = TimeToServerDefault;
tunes.playScore( scoreScore );
}
else
tunes.playScore( winScore );
}
// Player 1 Scored
else if ( GameBall.posX > ( screenWidth + ( 2 * GameBall.ballSize ) ) )
{
// Score
Player1Score++;
GameBall.Hide();
if ( !CheckGameOver() )
{
GameBall.Show();
GameBall.Reset();
TimeToServe = TimeToServerDefault;
tunes.playScore( scoreScore );
}
else
tunes.playScore( winScore );
}
else if ( paddle1XDelta == ( GameBall.ballSize / 2 ) && abs(paddle1YDelta) < collisionYDeltaMax_P1 )
{
// Paddle 1 Hit
float yVelocity = MapRange( paddle1YDelta * 1.0f, Paddle_1.sizeY / -2.0f, Paddle_1.sizeY / 2.0f, -ballReboundRange, ballReboundRange );
GameBall.BounceBall( -1.05, yVelocity, 2 );
}
else if ( paddle2XDelta == ( GameBall.ballSize / -2 + 1) && abs(paddle2YDelta) < collisionYDeltaMax_P2 )
{
// Paddle 2 Hit
float yVelocity = MapRange( paddle2YDelta, Paddle_2.sizeY / -2.0f, Paddle_2.sizeY / 2.0f, -ballReboundRange, ballReboundRange );
debug = yVelocity;
GameBall.BounceBall( -1.05, yVelocity, 2 );
}
}
// Didn't see a map range function so I whipped one up really quick
float MapRange( float x, float in_min, float in_max, float out_min, float out_max )
{
return ( x - in_min ) * ( out_max - out_min ) / ( in_max - in_min ) + out_min;
}