-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.c
208 lines (173 loc) · 3.74 KB
/
pong.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
#include <gb/gb.h>
#include <stdio.h>
#include "ball.c"
#include "paddle.c"
//boolean implementation
typedef int bool;
#define true 1
#define false 0
//functions
void checkControls();
void loadSprites();
void startBall();
void updateSprite(char pad, signed char cuanto);
void moveBall();
bool collisionCheck(unsigned char x1, unsigned char y1, unsigned char w1, unsigned char h1, unsigned char x2, unsigned char y2, unsigned char w2, unsigned char h2);
//paddle 1 position on Y axis
unsigned char yPad1=70;
//paddle 2 position on Y axis
unsigned char yPad2=70;
unsigned char countPad=0;
//ball
unsigned char xBall=80;
unsigned char yBall=72;
signed char xVel;
signed char yVel;
bool newBall;
int count=0;//Delays the ball without halt the whole system
int i;
//Extra
UINT8 score1=0;
UINT8 score2=0;
//pointer to memory address for random number generation
unsigned char *ptr_div_reg = 0xFF04;
unsigned char randy = *(ptr_div_reg);
void main(){
loadSprites();
restart:
startBall();
while(newBall==false){
checkControls();
}
goto restart;
}
void loadSprites(){
SPRITES_8x8;
//Starting at zero, loads paddle array to memory twice and loads the ball as well
set_sprite_data(0,2,paddle);
set_sprite_data(2,4,paddle);
set_sprite_data(4,4,ball);
//Setting that sprite we just loaded to tile mem
set_sprite_tile(0,0);
set_sprite_tile(1,1);
set_sprite_tile(2,2);
set_sprite_tile(3,3);
//this one is the ball
set_sprite_tile(4,4);
//sets pad1(right paddle)
move_sprite(0, 150, yPad1);
move_sprite(1, 150, yPad1+8);
//sets pad2(left paddle)
move_sprite(2, 20, yPad2);
move_sprite(3, 20, yPad2+8);
//sets ball
move_sprite(4, xBall, yBall);
DISPLAY_ON;
SHOW_SPRITES;
}
bool collisionCheck(unsigned char x1, unsigned char y1, unsigned char w1, unsigned char h1, unsigned char x2, unsigned char y2, unsigned char w2, unsigned char h2){
if ((x1 < (x2+w2)) && ((x1+w1) > x2) && (y1 < (h2+y2)) && ((y1+h1) > y2)) {
return true;
} else {
return false;
}
}
void updateSprite(char pad, signed char cuanto){
if(pad==1){
yPad1+=cuanto;
move_sprite(0, 150, yPad1);
move_sprite(1, 150, yPad1+8);
}
if(pad==2){
yPad2+=cuanto;
move_sprite(2, 20, yPad2);
move_sprite(3, 20, yPad2+8);
}
return;
}
void moveBall(){
if(yBall==144||yBall==0){
yVel=yVel*-1;
}
if(xBall==0){
score1++;
newBall=true;
}
if(xBall==160){
score2++;
newBall=true;
}
if(collisionCheck(150, yPad1, 8,16, xBall, yBall, 8,8)==true || collisionCheck(20, yPad2, 8,16, xBall, yBall, 8,8)==true){
xVel=xVel*-1;
if(collisionCheck(150, yPad1, 8,2, xBall, yBall, 8,8)==true|| collisionCheck(20, yPad2, 8,2, xBall, yBall, 8,8)==true){
if(yVel==0){
yVel=1;
}else{
yVel=yVel*-1;
}
}
}
count++;
if(countPad==40){
up:
countPad++;
if(countPad!=80){
goto up;
}
xBall=xBall+xVel;
yBall=yBall+yVel;
move_sprite(4, xBall, yBall);
count=0;
}
}
void startBall(){
xBall=80;
yBall=72;
move_sprite(4, 80, 72);
printf("PAUSE");
pause:
if(joypad()==J_START){
if(score1>=score2){
xVel=1;
yVel=0;
}else {
xVel=-1;
yVel=0;
}
newBall=false;
}else{
goto pause;
}
}
void checkControls(){
countPad++;
if(countPad==40){
if(joypad()==J_UP ){
if(yPad1<=16){
}else{
updateSprite(1, -1);
}
}
if(joypad()==J_DOWN){
if(yPad1>=144){
}else{
updateSprite(1, 1);
}
}
if(joypad()==J_A){
if(yPad2>=144){
}else{
updateSprite(2, 1);
}
}
if(joypad()==J_B){
if(yPad2<=16){
}else{
updateSprite(2, -1);
}
}
moveBall();
countPad=0;
}
return;
}