-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
216 lines (159 loc) · 5.51 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
#include <gb/gb.h>
#include <gb/drawing.h>
#include <stdio.h>
#include <stdlib.h>
#include <rand.h>
#include "sprites.c"
const INT16 width = 160;
const INT16 height = 140;
const INT8 xoff = 10;
const INT8 yoff = 15;
INT16 Ballxpos = 88;
INT16 Ballypos = 78;
INT16 PrevBallx =0;
INT16 PrevBally =0;
INT8 Ballxvel = 0;
INT8 Ballyvel = 0;
INT16 PaddleAypos = 75;
INT8 PaddleAyvel = 0;
INT16 PaddleBypos = 75;
INT8 PaddleByvel = 0;
INT8 DistinceFromWall = 10;
BOOLEAN running = 0;
void updateSpritePositions(){
move_sprite(_ball, Ballxpos, Ballypos);
move_sprite(_paddleATop, DistinceFromWall, PaddleAypos - 8);
move_sprite(_paddleAMiddle, DistinceFromWall, PaddleAypos + 0);
move_sprite(_paddleABottom, DistinceFromWall, PaddleAypos + 8);
move_sprite(_paddleBTop, width-DistinceFromWall, PaddleBypos - 8);
move_sprite(_paddleBMiddle, width-DistinceFromWall, PaddleBypos + 0);
move_sprite(_paddleBBottom, width-DistinceFromWall, PaddleBypos + 8);
}
void initSprites(){
set_sprite_data(_ball, _ballTileCount, Ball);
set_sprite_tile(_ball, _ballTileIndex);
set_sprite_data(_paddleATop, _paddleTileCount, Paddle);
set_sprite_tile(_paddleATop, _paddleTileIndex);
set_sprite_data(_paddleAMiddle, _paddleTileCount, Paddle);
set_sprite_tile(_paddleAMiddle, _paddleTileIndex);
set_sprite_data(_paddleABottom, _paddleTileCount, Paddle);
set_sprite_tile(_paddleABottom, _paddleTileIndex);
set_sprite_data(_paddleBTop, _paddleTileCount, Paddle);
set_sprite_tile(_paddleBTop, _paddleTileIndex);
set_sprite_data(_paddleBMiddle, _paddleTileCount, Paddle);
set_sprite_tile(_paddleBMiddle, _paddleTileIndex);
set_sprite_data(_paddleBBottom, _paddleTileCount, Paddle);
set_sprite_tile(_paddleBBottom, _paddleTileIndex);
SHOW_SPRITES;
}
void score(){
//Ballxvel = -Ballxvel;
running = 0;
srand((unsigned) (Ballxpos * Ballxvel + Ballypos * Ballxvel));
// PrevBallx = Ballxpos * Ballxvel;
// PrevBally = Ballypos * Ballxvel;
Ballxvel = 0;
Ballyvel = 0;
Ballxpos = 88;
Ballypos = 78;
PaddleAypos = 75;
PaddleAyvel = 0;
PaddleBypos = 75;
PaddleByvel = 0;
}
void AIMove(){
PaddleBypos = Ballypos;
PaddleByvel = ((Ballypos-8) - (PaddleBypos-12));
if(PaddleByvel > 5)
PaddleByvel = 5;
}
BOOLEAN UpPressed = 0;
BOOLEAN DownPressed = 0;
void PlayerMove(){
if (joypad() & J_UP)
UpPressed = 1;
else
UpPressed = 0;
if (joypad() & J_DOWN)
DownPressed = 1;
else
DownPressed = 0;
if(UpPressed)
PaddleAyvel = -5;
else if(DownPressed)
PaddleAyvel = 5;
else
PaddleAyvel = 0;
}
// void clearBack(){
// // Set up background display
// set_bkg_data(0, 1, &color); // Load a color into the background palette
// // Clear the screen
// memset((UINT8*)0x9800, 0, 0x1800); // Clear the VRAM
// gotoxy(10, height-10);
// printf("Hello, Game Boy!");
// }
void main(){
init();
initSprites();
rand(0x100);
while(1){
PlayerMove();
// clearBack();
if(running == 1){
AIMove();
if(Ballxpos > width || Ballxpos-8 < 0)
score();
if(Ballypos-8 > height || Ballypos-16 < 0)
Ballyvel = -Ballyvel;
if(Ballxvel > 0 && Ballxpos >= width-DistinceFromWall-4 && Ballypos-13 <= PaddleBypos && Ballypos-13 >= PaddleBypos-24){
Ballxvel = -Ballxvel;
Ballyvel += Ballypos - PaddleBypos;
}
else if(Ballxvel < 0 && Ballxpos-8 <= DistinceFromWall-4 && Ballypos-13 <= PaddleAypos && Ballypos-13 >= PaddleAypos-24){
Ballxvel = -Ballxvel;
Ballyvel += Ballypos - PaddleBypos;
}
if(Ballyvel == 0)
Ballyvel++;
if(Ballyvel > 5)
Ballyvel = 5;
else if(Ballyvel < -5)
Ballyvel = -5;
if(Ballxvel > 5)
Ballxvel = 5;
else if(Ballxvel < -5)
Ballxvel = -5;
if(PaddleAyvel > 0 && PaddleAypos > height)
PaddleAypos = height;
else if(PaddleAyvel < 0 && PaddleAypos-8 < yoff)
PaddleAypos = yoff + 8;
if( PaddleByvel > 0 && PaddleBypos > height)
PaddleBypos = height;
else if( PaddleByvel < 0 && PaddleBypos-8 < yoff)
PaddleBypos = yoff + 8;
// line(DistinceFromWall-4, PaddleAypos-24, width-DistinceFromWall-4, PaddleBypos-24);//top
// line(DistinceFromWall-4, PaddleAypos, width-DistinceFromWall-4, PaddleBypos);//bottom
// circle(Ballxpos, Ballypos-13, 1, 1);
// circle(Ballxpos-8, Ballypos-8 , 1, 1);//top left corner of ball
// circle(Ballxpos-8, Ballypos-16, 1, 1);//bottom left corner of ball
// circle(Ballxpos , Ballypos-8 , 1, 1);//top right corner of ball
// circle(Ballxpos , Ballypos-16, 1, 1);//bottom right corner of ball
Ballxpos += Ballxvel;
Ballypos += Ballyvel;
PaddleAypos += PaddleAyvel;
PaddleBypos += PaddleByvel;
}
else if(PaddleAyvel != 0){
running = 1;
while(Ballxvel == 0){
Ballxvel = rand() % 10 -5;
}
while(Ballyvel == 0){
Ballyvel = rand() % 10 -5;
}
}
updateSpritePositions();
delay(20);
}
}