Craig Evans
/
ELEC2645_Pong2021
Pong game for ELEC1620 board.
lib/Paddle.cpp@3:5746c6833d73, 2021-03-11 (annotated)
- Committer:
- eencae
- Date:
- Thu Mar 11 14:54:25 2021 +0000
- Revision:
- 3:5746c6833d73
- Parent:
- 2:482d74ef09c8
Added on LEDs and 7-seg for lives, bouncing off paddle and walls.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 1:d63a63f0d397 | 1 | #include "Paddle.h" |
eencae | 1:d63a63f0d397 | 2 | |
eencae | 1:d63a63f0d397 | 3 | // nothing doing in the constructor and destructor |
eencae | 1:d63a63f0d397 | 4 | Paddle::Paddle() { } |
eencae | 1:d63a63f0d397 | 5 | |
eencae | 1:d63a63f0d397 | 6 | void Paddle::init(int x,int height,int width) { |
eencae | 1:d63a63f0d397 | 7 | printf("Paddle: Init\n"); |
eencae | 1:d63a63f0d397 | 8 | _x = x; // x value on screen is fixed |
eencae | 1:d63a63f0d397 | 9 | _y = HEIGHT/2 - height/2; // y depends on height of screen and height of paddle |
eencae | 1:d63a63f0d397 | 10 | _height = height; |
eencae | 1:d63a63f0d397 | 11 | _width = width; |
eencae | 1:d63a63f0d397 | 12 | _speed = 1; // default speed |
eencae | 1:d63a63f0d397 | 13 | _score = 0; // start score from zero |
eencae | 1:d63a63f0d397 | 14 | } |
eencae | 1:d63a63f0d397 | 15 | |
eencae | 1:d63a63f0d397 | 16 | void Paddle::draw(N5110 &lcd) { |
eencae | 1:d63a63f0d397 | 17 | printf("Paddle: Draw\n"); |
eencae | 1:d63a63f0d397 | 18 | lcd.drawRect(_x,_y,_width,_height,FILL_BLACK); |
eencae | 1:d63a63f0d397 | 19 | } |
eencae | 1:d63a63f0d397 | 20 | |
eencae | 1:d63a63f0d397 | 21 | void Paddle::update(UserInput input) { |
eencae | 1:d63a63f0d397 | 22 | printf("Paddle: Update\n"); |
eencae | 3:5746c6833d73 | 23 | _speed = 2; |
eencae | 1:d63a63f0d397 | 24 | // update y value depending on direction of movement |
eencae | 1:d63a63f0d397 | 25 | // North is decrement as origin is at the top-left so decreasing moves up |
eencae | 1:d63a63f0d397 | 26 | if (input.d == N) { _y-=_speed; } |
eencae | 1:d63a63f0d397 | 27 | else if (input.d == S) { _y+=_speed; } |
eencae | 1:d63a63f0d397 | 28 | |
eencae | 1:d63a63f0d397 | 29 | // check the y origin to ensure that the paddle doesn't go off screen |
eencae | 1:d63a63f0d397 | 30 | if (_y < 1) { _y = 1; } |
eencae | 1:d63a63f0d397 | 31 | if (_y > HEIGHT - _height - 1) { _y = HEIGHT - _height - 1; } |
eencae | 1:d63a63f0d397 | 32 | } |
eencae | 1:d63a63f0d397 | 33 | |
eencae | 1:d63a63f0d397 | 34 | void Paddle::add_score() { _score++; } |
eencae | 1:d63a63f0d397 | 35 | |
eencae | 1:d63a63f0d397 | 36 | int Paddle::get_score() { return _score; } |
eencae | 1:d63a63f0d397 | 37 | |
eencae | 2:482d74ef09c8 | 38 | Position2D Paddle::get_pos() { return {_x,_y}; } |
eencae | 2:482d74ef09c8 | 39 | |
eencae | 2:482d74ef09c8 | 40 | int Paddle::get_height() { return _height; } |
eencae | 2:482d74ef09c8 | 41 | |
eencae | 2:482d74ef09c8 | 42 | int Paddle::get_width() { return _width; } |