-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.c
270 lines (219 loc) · 5.82 KB
/
Game.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
//Main c file
#include "Game.h"
#include <stdbool.h>
//display
void sync()
{
while(REG_DISPLAY_VCOUNT >= 160);
while(REG_DISPLAY_VCOUNT < 160);
}
//choose color
uint16 makeColor(uint8 r, uint8 g, uint8 b)
{
return (r & 0x1f) | ((g & 0x1f) << 5) | ((b & 0x1f) << 10);
}
uint32 clamp(int value, int min, int max)
{
return (value < min ? min : (value > max ? max : value));
}
//draw rectangle
void drawRect(struct Rect rect, uint16 color)
{
for(int y = 0; y < rect.h; y++)
{
for(int x = 0; x < rect.w; x++)
{
SCREENBUFFER[(rect.y + y) * SCREEN_WIDTH + rect.x + x] = color;
}
}
}
//player 1 7seg display
void init7seg1()
{
a.w = 16;
a.h = 4;
f.w = 4;
f.h = 16;
b = c = e = f;
d = g = a;
a.x = d.x = e.x = f.x = g.x = SCREEN_WIDTH/4;
b.x = c.x = a.x + a.w;
a.y = b.y = f.y = 0;
c.y = e.y = g.y = b.y + b.h - a.h;
d.y = g.y + b.h - a.h;
}
void clear7seg1()
{
drawRect(a, makeColor(0,0,0));
drawRect(b, makeColor(0,0,0));
drawRect(c, makeColor(0,0,0));
drawRect(d, makeColor(0,0,0));
drawRect(e, makeColor(0,0,0));
drawRect(f, makeColor(0,0,0));
drawRect(g, makeColor(0,0,0));
}
void draw7seg1(uint8 num)
{
clear7seg1();
bool w, x, y, z;
w = (num >= 8 ? ((num-=8) ? 1 : 1) : 0);
x = (num >= 4 ? ((num-=4) ? 1 : 1) : 0);
y = (num >= 2 ? ((num-=2) ? 1 : 1) : 0);
z = (num >= 1 ? ((num-=1) ? 1 : 1) : 0);
if(w || y || (!x && !z) || (x && z))
drawRect(a, makeColor(0x1f, 0x1f, 0x1f));
if((y && z) || (!y && !z) || !x)
drawRect(b, makeColor(0x1f, 0x1f, 0x1f));
if(w || x || !y || z)
drawRect(c, makeColor(0x1f, 0x1f, 0x1f));
if(w || (y && !z) || (!x && !z) || (!x && y) || (x && !y && z))
drawRect(d, makeColor(0x1f, 0x1f, 0x1f));
if((y && !z) || (!x && !z))
drawRect(e, makeColor(0x1f, 0x1f, 0x1f));
if(w || (x && !y) || (x && !z) || (!y && !z))
drawRect(f, makeColor(0x1f, 0x1f, 0x1f));
if(w || (!y && x) || (y && !z) || (y && !x))
drawRect(g, makeColor(0x1f, 0x1f, 0x1f));
}
//Player 2 7seg display
void init7seg2()
{
h.w = 16;
h.h = 4;
m.w = 4;
m.h = 16;
i = j = l = m;
k = n = h;
h.x = k.x = l.x = m.x = n.x = (SCREEN_WIDTH/4) + (SCREEN_WIDTH/2);
i.x = j.x = h.x + h.w;
h.y = i.y = m.y = 0;
j.y = l.y = n.y = i.y + i.h - h.h;
k.y = n.y + i.h - h.h;
}
void clear7seg2()
{
drawRect(h, makeColor(0,0,0));
drawRect(i, makeColor(0,0,0));
drawRect(j, makeColor(0,0,0));
drawRect(k, makeColor(0,0,0));
drawRect(l, makeColor(0,0,0));
drawRect(m, makeColor(0,0,0));
drawRect(n, makeColor(0,0,0));
}
void draw7seg2(uint8 num)
{
clear7seg2();
bool w, x, y, z;
w = (num >= 8 ? ((num-=8) ? 1 : 1) : 0);
x = (num >= 4 ? ((num-=4) ? 1 : 1) : 0);
y = (num >= 2 ? ((num-=2) ? 1 : 1) : 0);
z = (num >= 1 ? ((num-=1) ? 1 : 1) : 0);
if(w || y || (!x && !z) || (x && z))
drawRect(h, makeColor(0x1f, 0x1f, 0x1f));
if((y && z) || (!y && !z) || !x)
drawRect(i, makeColor(0x1f, 0x1f, 0x1f));
if(w || x || !y || z)
drawRect(j, makeColor(0x1f, 0x1f, 0x1f));
if(w || (y && !z) || (!x && !z) || (!x && y) || (x && !y && z))
drawRect(k, makeColor(0x1f, 0x1f, 0x1f));
if((y && !z) || (!x && !z))
drawRect(l, makeColor(0x1f, 0x1f, 0x1f));
if(w || (x && !y) || (x && !z) || (!y && !z))
drawRect(m, makeColor(0x1f, 0x1f, 0x1f));
if(w || (!y && x) || (y && !z) || (y && !x))
drawRect(n, makeColor(0x1f, 0x1f, 0x1f));
}
int main()
{
REG_DISPLAY = VIDEOMODE | BGMODE;
int top = 0;
int left = 0;
int bottom = SCREEN_HEIGHT;
int right = SCREEN_WIDTH;
int ballTop = 130;
int ballLeft = 80;
int speedRight = 1;
int speedDown = 1;
int score1 = 0;
int score2 = 0;
player1.x = left;
player1.y = top;
player1.w = 8;
player1.h = 32;
prevPlayer1 = player1;
player2.x = right - 8;
player2.y = bottom - 32;
player2.w = 8;
player2.h = 32;
prevPlayer2 = player2;
ball.x = ballLeft;
ball.y = ballTop;
ball.w = 8;
ball.h = 8;
init7seg1();
draw7seg1(score1);
init7seg2();
draw7seg2(score1);
prevBall = ball;
while(1)
{
sync();
drawRect(prevPlayer1, makeColor(0,0,0));
drawRect(prevPlayer2, makeColor(0,0,0));
drawRect(prevBall, makeColor(0,0,0));
ballLeft += speedRight;
ballTop += speedDown;
if(!((REG_KEY_INPUT) & DOWN)) // player 1 down button
top++;
if(!((REG_KEY_INPUT) & UP)) // player 1 up button
top--;
if(!((REG_KEY_INPUT) & A)) // player 2 up button (on keyboard Q)
bottom = bottom + 2;
if(!((REG_KEY_INPUT) & B)) // player 2 down button (on keyboard W)
bottom = bottom - 2;;
top = clamp(top, 0, SCREEN_HEIGHT - player1.h); // stop rectangle going out of the board
bottom = clamp(bottom, 0, SCREEN_HEIGHT - player2.h); // stop rectangle going out of the board
ballLeft = clamp(ballLeft, 0, SCREEN_WIDTH-ball.w); // define ball left side
ballTop = clamp(ballTop, 0, SCREEN_HEIGHT-ball.h); // define ball top side
if(ballLeft == SCREEN_WIDTH-ball.w) // move ball right
speedRight = -speedRight;
if(ballTop == 0 || ballTop == SCREEN_HEIGHT - ball.h) // move ball up and down
speedDown = -speedDown;
if(ballLeft == player1.x + player1.w &&
(ballTop >= top - ball.h &&
ballTop <= top + player1.h)) // when ball touches the Left rectangle
{
speedRight = -speedRight;
}
if(ballLeft + 8 == player2.x &&
(ballTop >= bottom - ball.h &&
ballTop <= bottom + player2.h)) // when ball touches the Right rectangle
{
speedRight = -speedRight;
}
if(ballLeft == 0) //when ball touches the left side
{
score1++;
ballLeft = 130;
ballTop = 80;
}
if(ballLeft + 8 == SCREEN_WIDTH) //when ball touches the right side
{
score2++;
ballLeft = 130;
ballTop = 80;
}
player1.y = top;
prevPlayer1 = player1;
player2.y = bottom;
prevPlayer2 = player2;
ball.x = ballLeft;
ball.y = ballTop;
prevBall = ball;
drawRect(player1, makeColor(0x1f, 0, 0));
drawRect(player2, makeColor(0, 0, 0x1f));
drawRect(ball, makeColor(0, 0x1f, 0));
draw7seg1(score1);
draw7seg2(score2);
}
}