-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
355 lines (268 loc) · 7.3 KB
/
main.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// "PONG"
// by @danhans42
// http://psx0.wordpress.com/
// http://www.github.com/danhans42/
//
// My first ever PlayStation game Yay! Cant take all the credit though...
//
// This was written using the helloworld_and_flappycredits Pad/GPU example
// as a base, and using the info from the following links :-
//
// https://github.com/JonathanDotCel/helloworld_and_flappycredits
// https://drdoane.com/thinking-through-a-basic-pong-game-in-processing/
// https://github.com/grumpycoders/pcsx-redux/
//
// For more PSX related chat join us on the #psxdev discord
//
// ------ Original Helloworld_and_flappycredits Licence Text Below --------
//
// https://github.com/JonathanDotCel
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// Standard kinda fare though, no explicit or implied warranty.
// Any issues arising from the use of this software are your own.
//
// https://github.com/JonathanDotCel
//
// To do List Features
//
// [X] Add Second Player on same pad
// [ ] Alternate ball serve direction?
// [X] Scoring
// [X] Paddle binding to court
// [ ] Sound - Gotta be old school Spectrumesque Beeps - or shitter
// [ ] Instructions/button mapping on main screen
// [ ] Arkanoid styleee breakout mode?
//
#ifndef MAINC
#define MAINC
#include <stdarg.h>
#include <stdbool.h>
#include "main.h"
#include "littlelibc.h"
#include "utility.h"
#include "drawing.h"
#include "pads.h"
#include "gpu.h"
//Screens
void MainScreen();
void GameScreen();
void GameOver();
// Graphic Elements
void DrawGameArea();
void DrawLogo();
void DrawBall();
// Stuff
void DoStuff();
bool Collision();
int p1_score, p2_score, p1_bat;
short blockX = SCREEN_WIDTH / 2;
short blockY = SCREEN_HEIGHT / 2;
short BallX=256;
short BallY=120;
short LimitX=40;
short LimitY=34;
short WidthX=464;
short HeightY=200;
short dX = 2;
short dY = 2;
short p1_PaddleY=100;
short p1_PaddleX=46;
short p2_PaddleY=100;
short p2_PaddleX=458;
short PaddleWidth = 40;
int main(){
// Sets shit up
ExitCritical();
InitBuffer();
InitGPU();
InitPads();
// Main Screen
MainScreen();
}
void DoStuff(){
if ( Released( PADRdown ) ){
// go to start of game
}
if ( Released( PADselect ) ){
// restart via bios
goto *(ulong*)0xBFC00000;
}
}
void DrawBall() {
DrawTile (BallX,BallY,8,6,0xFFFFFF);
}
void DrawGameArea() {
// Colours can be changed in main.h
DrawTile(38,30,436,180,CLR_BORDER); // Draw Border
DrawTile(40,32,432,176,CLR_COURT); // Draw Court
}
void DrawLogo() {
//Draws the word PONG - 1979 Styleee - Yeeeaaaah Baby!!
DrawTile(98,54,16,60,CLR_LOGO); // Letter P
DrawTile(146,54,16,36,CLR_LOGO);
DrawTile(114,54,48,12,CLR_LOGO);
DrawTile(114,78,48,12,CLR_LOGO);
DrawTile(178,54,16,36,CLR_LOGO); // Letter O
DrawTile(178,54,50,12,CLR_LOGO);
DrawTile(178,78,50,12,CLR_LOGO);
DrawTile(228,54,16,36,CLR_LOGO);
DrawTile(260,54,16,36,CLR_LOGO); // Letter N
DrawTile(260,54,50,12,CLR_LOGO);
DrawTile(310,54,16,36,CLR_LOGO);
DrawTile(390,54,16,60,CLR_LOGO); // Letter G
DrawTile(342,54,50,12,CLR_LOGO);
DrawTile(342,54,16,36,CLR_LOGO);
DrawTile(342,78,50,12,CLR_LOGO);
DrawTile(342,102,50,12,CLR_LOGO);
}
void MainScreen() {
while ( 1 ){
MonitorPads();
ClearScreenText();
DrawBG();
Blah("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
Blah(" Press Start To Play\n\n\n\n\n");
Blah(" github.com/danhans42/psx_pong \n");
DrawGameArea();
DrawLogo();
DrawGitQR();
if ( Released( PADstart ) ){
GameScreen();
}
if ( Released( PADselect ) ){
// restart via bios
goto *(ulong*)0xBFC00000;
}
Draw();
}
}
void GameScreen() {
// Put the ball in the middle of the court
p1_score = 0;
p2_score = 0;
while (1) {
ClearScreenText();
MonitorPads();
DrawBG();
Blah ("\n\n\n\n\n %i %i \n",p1_score,p2_score);
DrawGameArea();
// Draw Court Divider
DrawTile(256,32,2,11,CLR_BORDER);
DrawTile(256,61,2,11,CLR_BORDER);
DrawTile(256,88,2,11,CLR_BORDER);
DrawTile(256,115,2,11,CLR_BORDER);
DrawTile(256,142,2,11,CLR_BORDER);
DrawTile(256,169,2,11,CLR_BORDER);
DrawTile(256,197,2,11,CLR_BORDER);
DrawBall();
// Checks if the paddles are off the top the court area, if not clip them back in
if (p1_PaddleY < LimitY+2 ) p1_PaddleY = LimitY;
if (p2_PaddleY < LimitY+2 ) p2_PaddleY = LimitY;
// Checks if the paddles are off the bottom the court area, if not clip them back in
// (same thing essentially, but we need to take paddleX size into consideration)
if (p1_PaddleY+PaddleWidth > HeightY ) p1_PaddleY = (HeightY+6)-PaddleWidth;
if (p2_PaddleY+PaddleWidth > HeightY ) p2_PaddleY = (HeightY+6)-PaddleWidth;
// Handles if the ball runs off the court on P1 side - Point for Player 2
if (BallX < p1_PaddleX) {
p2_score=p2_score+1;
BallX=256;
BallY=120;
}
// Handles if the ball runs off the court on P2 side - Point for Player 1
if (BallX > p2_PaddleX) {
p1_score=p1_score+1;
BallX=256;
BallY=120;
}
// Handles paddle collision
if (Collision()) {
dX = -dX; // if dX == 2, it becomes -2; if dX is -2, it becomes 2
}
// Handles ball bouncing off the court sides - should be self explanatory
if (BallX > WidthX) {
dX = -dX;
}
if (BallX < LimitX) {
dX = -dX;
}
if (BallY > HeightY) {
dY = -dY;
}
if (BallY < LimitY) {
dY = -dY;
}
// Increment the movement
BallX = BallX + dX;
BallY = BallY + dY;
// Draw P1 Paddle
DrawTile(p1_PaddleX,p1_PaddleY,8,PaddleWidth,0xffffff);
// Draw P2 Paddle
DrawTile(p2_PaddleX,p2_PaddleY,8,PaddleWidth,0xffffff);
// Draw the screen
Draw();
//Process Input - also clips the paddle to the court area
// move P1 Paddle Down
if ( Released( PADLdown ) ){
p1_PaddleY = p1_PaddleY+6;
}
// move P1 Paddle Up
if (Released( PADLup ) ){
p1_PaddleY = p1_PaddleY-6;
}
// move P2 Paddle Up
if (Released( PADRup ) ){
p2_PaddleY = p2_PaddleY-6;
}
// move P2 Paddle Down
if ( Released( PADRdown ) ){
p2_PaddleY = p2_PaddleY+6;
}
// Check scores to see if a player has won - first to 8
if (p1_score >7 | p2_score >7 ) GameOver();
}
}
void GameOver() {
while(1) {
ClearScreenText();
DrawGameArea();
MonitorPads();
Blah ("\n\n\n\n\n\n GAME OVER\n\n\n\n\n\n\n");
if (p1_score > p2_score) {
Blah(" Player 1 WINS");
}
if (p2_score > p1_score) {
Blah(" Player 2 WINS");
}
Blah ("\n\n\n\n\n\n\n\n Press Start to Play Again\n\n");
Blah (" or O for Main Menu \n");
Draw();
if ( Released( PADstart ) ){
GameScreen();
}
if ( Released( PADRright ) ){
MainScreen();
}
}
}
bool Collision() {
bool returnValue = false;
// player 1
if ((BallX >= p1_PaddleX+8) && (BallX <= p1_PaddleX +8)) {
if ((BallY >= p1_PaddleY) && (BallY <= p1_PaddleY + PaddleWidth)) {
returnValue = true;
}
}
// player 2
if ((BallX >= p2_PaddleX-6) && (BallX <= p2_PaddleX-6 )) {
if ((BallY >= p2_PaddleY) && (BallY <= p2_PaddleY + PaddleWidth)) {
returnValue = true;
}
}
return returnValue;
}
#endif