-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpongBall.c
116 lines (102 loc) · 2.84 KB
/
pongBall.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
/** @file pongBall.c
@author G Lamont and L Brewster
@date 6 October 2017
@brief Pong ball module. Initiates and moves a ball object
*/
#include "system.h"
#include "pacer.h"
#include "pio.h"
#include "ledMat.h"
#include "pongPaddle.h"
#define MAX_COL 4
#define MIN_COL 0
#define MAX_ROW 6
#define MIN_ROW 0
#define BALL_SHIFT 2
#define ON 1
#define TRUE 1
#define FALSE 0
#define NUM_OF_LIVES 3
#define BALL_SPEED 200
/** Structure to define a pong ball
* contains balls current position
* and current direction */
typedef struct ball_struct
{
int8_t curr_row;
int8_t curr_col;
bool left;
bool down;
int8_t player_1_lives;
int8_t player_2_lives;
int16_t speed;
} ball_struct_t;
/** Initializes the ball
* places the ball at pos (1,1) */
ball_struct_t init_ball(void)
{
ball_struct_t ball = {MIN_ROW,MIN_COL,TRUE,TRUE, NUM_OF_LIVES, NUM_OF_LIVES, BALL_SPEED};
ledmat_pixel_set (ball.curr_col, ball.curr_row, ON);
return ball;
}
/** Checks if the ball has collided with the wall */
bool wall_collision(paddle_struct_t paddle, ball_struct_t ball)
{
if(paddle.curr_row_1 == ball.curr_row) {
if(ball.curr_col != paddle.curr_col_1 && ball.curr_col != paddle.curr_col_2) {
return TRUE;
}
else {
return FALSE;
}
}
return FALSE;
}
/** Moves the given ball diagonally across the board
* @param ball: given balls new position */
ball_struct_t move_ball (ball_struct_t ball, paddle_struct_t paddle_1, paddle_struct_t paddle_2)
{
if (ball.left) {
ball.curr_row++;
} else {
ball.curr_row--;
}
if (ball.down) {
ball.curr_col++;
} else {
ball.curr_col--;
}
/** On collision with wall, the affected player loses one life
* and the ball speed increases*/
if(ball.curr_row == 0 && wall_collision(paddle_1, ball)) {
ball.player_1_lives --;
ball.speed -= 20;
} else if (ball.curr_row == 6 && wall_collision(paddle_2, ball)) {
ball.player_2_lives--;
ball.speed -= 20;
} else if ((ball.curr_row == 0 && !wall_collision(paddle_1, ball)) || (ball.curr_row == 6 && !wall_collision(paddle_2, ball))) {
if (ball.left) {
ball.curr_row -= BALL_SHIFT;
} else {
ball.curr_row += BALL_SHIFT;
}
ball.left = !(ball.left);
}
if (ball.curr_row > MAX_ROW || ball.curr_row < MIN_ROW) {
if (ball.left) {
ball.curr_row -= BALL_SHIFT;
} else {
ball.curr_row += BALL_SHIFT;
}
ball.left = !(ball.left);
}
if (ball.curr_col > MAX_COL || ball.curr_col < MIN_COL) {
if (ball.down) {
ball.curr_col -= BALL_SHIFT;
} else {
ball.curr_col += BALL_SHIFT;
}
ball.down = !(ball.down);
}
return ball;
}