-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaddle.c
82 lines (70 loc) · 2.18 KB
/
paddle.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
/**
Paddle module source code
@team 128
@author Ambrose Ledbrook - 79172462
@author Josh Jarvis - 28803714
@date 06-oct-2018
@brief This module provides the functionality of the game paddle.
*/
#include "paddle.h"
#include "system.h"
#include "tinygl.h"
#include "navswitch.h"
/**
This function is used to initialize the state of the paddle.
It sets the initial postion of the paddle
*/
void paddle_init(void) {
// Setting the initial postions of the paddle
tinygl_point_t left = tinygl_point(BOTTOM_COLUMN, PADDLE_START_LEFT);
tinygl_point_t right = tinygl_point(BOTTOM_COLUMN, PADDLE_START_RIGHT);
// Intialising the paddle points
paddle.left = left;
paddle.right = right;
// Displaying the paddle
paddle_show();
}
/**
This function shows the paddle in its current postion.
*/
void paddle_show(void) {
// Drawing the paddle
tinygl_draw_line(paddle.left, paddle.right, SHOW_PADDLE);
}
/**
This function moves the paddle on a NAVSWITCH_NORTH or
NAVSWITCH_SOUTH navswitch_push_event_p.
*/
void paddle_move(void) {
// Removing current paddle
tinygl_draw_line(paddle.left, paddle.right, HIDE_PADDLE);
// Updating the paddle postion if the navswitch is press
if (paddle.right.y != MAX_RIGHT && navswitch_push_event_p(NAVSWITCH_NORTH)) {
paddle.left.y -= MOVEMENT_INCREMENT;
paddle.right.y -= MOVEMENT_INCREMENT;
} else if (paddle.left.y != MAX_LEFT && navswitch_push_event_p(NAVSWITCH_SOUTH)) {
paddle.left.y += MOVEMENT_INCREMENT;
paddle.right.y += MOVEMENT_INCREMENT;
}
// Showing the paddle in its new current postion
paddle_show();
}
/**
Checking if the passed ball postion is within the paddle.
@param A tinygl_point_t which is the game balls postion
@return 1 if the ball is within the paddle, 0 otherwise
*/
uint8_t check_ball(tinygl_point_t pos) {
if (pos.y >= paddle.right.y && pos.y <= paddle.left.y) {
return 1;
} else {
return 0;
}
}
/**
Getting the center y coordinate of the paddle
@return The center y coordinate of the paddle
*/
uint8_t get_paddle_center(void) {
return (paddle.left.y + paddle.right.y) / PADDLE_WIDTH;
}