/
main.c
173 lines (150 loc) · 4.26 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
#include <pspkernel.h>
#include <oslib/oslib.h>
#define MAXBALLS 10
PSP_MODULE_INFO("MULTI PONG", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(12*1024);
int i = 0;
int NumOfBalls = 1;
int hits = 0;
//structure for a ball
struct ball {
int x;
int y;
int vx;
int vy;
} lob[MAXBALLS];
//standard inits
int initOSLib(){
oslInit(0);
oslInitGfx(OSL_PF_8888, 1);
oslInitAudio();
oslSetQuitOnLoadFailure(1);
oslSetKeyAutorepeatInit(40);
oslSetKeyAutorepeatInterval(10);
return 0;
}
int startSong(){
OSL_SOUND *music;
//song.bgm is required in the memory stick in the same directory as the EBOOT.PBP
music = oslLoadSoundFileBGM("song.bgm", OSL_FMT_STREAM);
oslAssert(music);
oslPlaySound(music, 0);
return 0;
}
//code to generate a new ball, index = parent
int newBall(int index) {
lob[NumOfBalls].x = lob[index].x - 5; //make sure it doesn't generate its own ball
lob[NumOfBalls].y = lob[index].y;
lob[NumOfBalls].vx = ((rand() % 4) + 2) * -1; //random velocity to keep things interesting
lob[NumOfBalls].vy = -1 * lob[index].vy; //peels off of parent ball
return ++NumOfBalls;
}
int readInput(int py){
oslReadKeys();
if (osl_pad.released.start)
oslQuit();
if (py >= 5 && osl_pad.held.up)
py -= 5;
if (py <= 227 && osl_pad.held.down)
py += 5;
if (osl_pad.pressed.cross && NumOfBalls < MAXBALLS) newBall(NumOfBalls - 1);
return py;
}
//game over stuff
int gameOver(int score) {
char buffer [32];
int skip = 0;
for (i = 0; i < 360; i++) {
if (!skip) {
oslStartDrawing();
oslClearScreen(RGB(0,0,0));
oslDrawString(200,150, "Game Over");
snprintf(buffer, 32, "Final Score: %d", score);
oslDrawString(170, 130, buffer);
oslEndDrawing();
}
oslEndFrame();
skip = oslSyncFrame();
}
oslQuit();
return score;
}
int moveBalls(int py, int score){
for (i = 0; i < NumOfBalls; i++) {
lob[i].x += lob[i].vx;
lob[i].y += lob[i].vy;
if (lob[i].x > 460) { // paddle side
if (lob[i].y > py - 5 && lob[i].y < py + 45) { //making sure it hit the paddle
lob[i].vx *= -1; //reverse it
if (NumOfBalls < MAXBALLS && hits >= (NumOfBalls * NumOfBalls)) {
newBall(i); //possibly add new ball
hits = 0;
} else {
hits++; //or just keep going
}
} else { //or else game over
gameOver(score);
}
}
if (lob[i].x < 0) { //bounce off other wall
lob[i].vx *= -1;
if (lob[i].vx > 1) { //slow it down (otherwise it was too difficult)
lob[i].vx--;
lob[i].x = 5;
}
}
if (lob[i].y > 260 || lob[i].y < 0) //bounce off ceiling and floor
lob[i].vy *= -1;
if (lob[i].vx > 1) { //slow it down
lob[i].vx--;
}
}
return 0;
}
int drawScreen(int skip, int py, int score){ //draw functi
char buffer [32];
if (!skip){
oslStartDrawing();
oslClearScreen(RGB(0,0,0));
oslDrawFillRect(470, py, 480, py + 40, RGB(54,204,204));
for (i = 0; i < NumOfBalls; i++) {
oslDrawRect(lob[i].x, lob[i].y, lob[i].x + 10, lob[i].y + 10, RGB(255,0,0));
}
snprintf(buffer, 32, "Score: %d", score);
oslDrawString(5, 10, buffer);
oslEndDrawing();
}
oslEndFrame();
skip = oslSyncFrame();
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(){
int skip = 0;
int py = 0; //player y-coordinate
int score = 0;
initOSLib();
oslIntraFontInit(INTRAFONT_CACHE_MED);
//Load font:
OSL_FONT *pgfFont = oslLoadFontFile("flash0:/font/ltn0.pgf");
oslIntraFontSetStyle(pgfFont, 1.0, RGBA(255,255,255,255), RGBA(0,0,0,0), INTRAFONT_ALIGN_LEFT);
oslSetFont(pgfFont);
startSong(); //load song
lob[0].x = 0; //initial ball
lob[0].y = rand() % 250;
lob[0].vx = 1;
lob[0].vy = -1;
while(!osl_quit){ //main game loop
py = readInput(py);
moveBalls(py, score);
drawScreen(skip, py, score);
score += NumOfBalls;
}
//Quit OSL:
oslEndGfx();
sceKernelExitGame();
return 0;
}