-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
408 lines (312 loc) · 7.8 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
* Include Libraries & Models & Declare Global Variables
*/
#include <pic32mx.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include "canvas.h"
#include "shieldDisplay.h"
#include "basicFunctions.h"
#include "ballMath.h"
#include "menu.h"
#include "main.h"
#include "config.h"
#include "model/ball.c"
#include "model/map.c"
#include "model/paddle.c"
/**
* Global values
* Global values that holds data of the players and the balls position.
* Aswell as the score of the player.
*/
int paddleX1; //The initial positions of the padels. and the ball
int paddleY1;
int paddleX2;
int paddleY2;
double ballX;
double ballY;
double ballAngle = 0; //Made with RADS
int playerOneScore;
int playerTwoScore;
int difficulty = 0; //The difficulty will range from 0-2, where 0 is easiest.
bool pvpMode; // true means it is pvp
/**
* Prints the current score of the players
* on the screen
*
* @param {int} playerOneScore The current score of player one
* @param {int} playerTwoScore The current score of player two
*
* @author Åhlin, Pontus
*/
void printPlayerScore(int playerOneScore, int playerTwoScore){
int ascii1 = playerOneScore + 48; //+ 48 because we want the ascii value in decimal (48 = 0)
int ascii2 = playerTwoScore + 48; //+ 48 because somehow when
char * p1 = (char*)&ascii1; //Here we need a char pointer to go in to the canvaswrite functions,
char * p2 = (char*)&ascii2; //so we cast ascii as a char pointer, that points to the adress
//the players score.
canvasWrite(p1, 48, 2, false, false); //Player one score
canvasWrite(p2, 73, 2, false, false); //Player two score
}
/**
* Checks which button is pressed and if it is pressed
* trigger a certain action, in this case either make the
* player go down or up
*
* @author Åhlin, Pontus
*/
void paintArena() {
canvasInsertModel(paddleX1, paddleY1, 2, 8, model_paddle, false); //The left side padel
canvasInsertModel(paddleX2, paddleY2, 2, 8, model_paddle, false); //The right side padel
canvasInsertModel(0, 0, 128, 32, model_map, true); //The map
canvasInsertModel(ballX, ballY, 2, 2, model_ball, true); //The ball
printPlayerScore(playerOneScore, playerTwoScore);
}
/**
* This function determines if the paddle will move up or down.
* The function also makes bounderies for the padels.
* @param {int} pos Current position of padel
* @param {int] direction The direction where the padel is going
*
* @author Åhlin, Pontus
*/
bool upOrDown(int pos, int direction) {
int newPos = pos + direction;
if(newPos < 24 & newPos > 0){
return true;
}
return false;
}
/**
* This is a very easy Ai that
* moves the way of the ball
*
* @param {int} offset Where the ai paddle should hit the ball in the Y-axis
*
* @param {int} reaction When the ai reacts to the ball in the X-axis
*
* @author Åhlin, Pontus
*/
void playingAi(int offset, int reaction){
if (ballX > reaction){
if (paddleY2 == 23){ //Makes the paddle not jump through space and time
paddleY2 = 23;
}
else if ((paddleY2 + offset) < ballY) {
paddleY2++;
}
if (paddleY2 == 1){
paddleY2 = 1;
}
else if (ballY < (paddleY2 + offset)){
paddleY2--;
}
}
}
/**
* This function is where the
* difficulty is chosen
*
* @param {int} difficulty This
*
* @author Åhlin, Pontus
*/
void playAi(int difficulty) {
switch(difficulty){
case(0):
playingAi(4, 120);
break;
case(1):
playingAi(4,96);
break;
case(2):
playingAi(4,64);
break;
}
}
/**
* Game over
* Once player one or player two gets five
* points the game is over and it will take you
* to the high-score screen
*
* @author Åhlin, Pontus
*/
void gameOver(){
if(playerOneScore == 5 || playerTwoScore == 5){
difficulty = 1; //Init difficulty to medium.
setInGame(false);
setMenuScreenCode(41); //Jump to set high score
}
}
/**
* Serve
*
* Deciding who should serve depending
* on who has most points
*
* @param {int} playerOneScore Score of player one
* @param {int} playerTwoScore Score of player two
*/
void serve(){
if(playerOneScore > playerTwoScore) ballAngle = 0;
else ballAngle = PI;
}
/**
* This function is called from main, continuously, that checks
* whether a button is pressed or not.
*
* @param {int} buttonData Takes in the
*
* @author Åhlin, Pontus
*/
void gameButtonTriggered(int buttonData) {
if (buttonData == 15) {
setInGame(false);
setMenuScreenCode(43);
return;
}
if(buttonData & 0x8 && upOrDown(paddleY1, 1)) {
paddleY1++;
}
if(buttonData & 0x4 && upOrDown(paddleY1, -1)) {
paddleY1--;
}
if (pvpMode) {
PORTE = 0x1;
if( buttonData & 0x2 && upOrDown(paddleY2, 1)) {
paddleY2++;
PORTE = 0x2;
}
if (buttonData & 0x1 && upOrDown(paddleY2, -1)) {
paddleY2--;
PORTE = 0x4;
}
}
}
/**
* Functions that are called when
* the game is initilized
*
* @author Åhlin, Pontus
*/
void playingGame() {
canvasClear(); //Clear the menu
moveBall(&ballX, &ballY, ballAngle); //Moves the ball
paintArena(); //Paint the arena
checkEdgeHit(ballY, &ballAngle); //Check arena hit
checkLeftPaddleHit(ballX, ballY, &ballAngle, paddleX1, paddleY1); //Check player 1 paddle hit
checkRightPaddleHit(ballX, ballY, &ballAngle, paddleX2, paddleY2); //Check player 2 paddle hit
checkPlayerScore(&playerOneScore, &playerTwoScore, ballX);
gameOver(); //Checks game state
}
/**
* Resets the arena
*
* Resets the ball and paddles
* to thier starting positions
*
* @author Åhlin, Pontus
*/
void resetArena() {
ballY = 16;
ballX = 64;
paddleX1 = 0; //The initial positions of the padels. and the ball
paddleY1 = 15;
paddleX2 = 126;
paddleY2 = 15;
serve(); //Deciding
}
/**
* initializes the paddles, arena
* and player score when
* starting the game
*
* @author Åhlin, Pontus
*/
void initArena() {
resetArena();
playerOneScore = 0;
playerTwoScore = 0;
}
/**
* The function that main calls when we want to load in
* the game state.
*
* @author Åhlin, Pontus
*/
void renderGame(){
playingGame();
const uint8_t* canvas_data = canvasGetData(); //Get the data from the canvas
sendDisplayData(canvas_data); //Sending that data to the OLED display
}
/**
* Get Player One Score
*
* @return {int} score
*
* @author Åhlin, Pontus
*/
int getPlayerOneScore(void) {
return playerOneScore;
}
/**
* Get Player Two Score
*
* @return {int} score
*
* @author Åhlin, Pontus
*/
int getPlayerTwoScore(void) {
return playerTwoScore;
}
/**
* Get Difficulty Level
*
* @return {int} difficulty level
*
* @author Åhlin, Pontus
*/
int getDifficultySetting(void) {
return difficulty;
}
/**
* Toggle Difficulty Settings
*
* @author Fridh, William
*/
void toggleDifficultySetting() {
difficulty++;
if (difficulty==3) difficulty = 0;
}
/**
* Turns either the pvp mode on or off
*
* @param {bool} trueOrFalse
*
* @author Åhlin, Pontus
*/
void pvpModeOnOff(bool trueOrFalse) {
pvpMode = trueOrFalse;
}
/**
* Get Winner
*
* Check who won the match
*
* @return {int} 1 = player one, 2 = player two, 3 = ai
*
* @author Fridh, William
*/
int getWinner(void) {
if (getPlayerOneScore() > getPlayerTwoScore()) { // Player one wins vs
return 1;
} else { // Player two wins
if (pvpMode) { // PVP
return 2;
} else {
return 3;
}
}
}