Pong game for ELEC1620 board.

Committer:
eencae
Date:
Wed Mar 10 16:37:52 2021 +0000
Revision:
2:482d74ef09c8
Parent:
1:d63a63f0d397
Child:
3:5746c6833d73
Added re-bound off back wall and paddle collisions.

Who changed what in which revision?

UserRevisionLine numberNew 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 1:d63a63f0d397 23 _speed = int(input.mag*_height); // speed of movement changes depending on
eencae 1:d63a63f0d397 24 // how far the joystick is pushed (0 to 1) and the height of the paddle
eencae 1:d63a63f0d397 25
eencae 1:d63a63f0d397 26 // update y value depending on direction of movement
eencae 1:d63a63f0d397 27 // North is decrement as origin is at the top-left so decreasing moves up
eencae 1:d63a63f0d397 28 if (input.d == N) { _y-=_speed; }
eencae 1:d63a63f0d397 29 else if (input.d == S) { _y+=_speed; }
eencae 1:d63a63f0d397 30
eencae 1:d63a63f0d397 31 // check the y origin to ensure that the paddle doesn't go off screen
eencae 1:d63a63f0d397 32 if (_y < 1) { _y = 1; }
eencae 1:d63a63f0d397 33 if (_y > HEIGHT - _height - 1) { _y = HEIGHT - _height - 1; }
eencae 1:d63a63f0d397 34 }
eencae 1:d63a63f0d397 35
eencae 1:d63a63f0d397 36 void Paddle::add_score() { _score++; }
eencae 1:d63a63f0d397 37
eencae 1:d63a63f0d397 38 int Paddle::get_score() { return _score; }
eencae 1:d63a63f0d397 39
eencae 2:482d74ef09c8 40 Position2D Paddle::get_pos() { return {_x,_y}; }
eencae 2:482d74ef09c8 41
eencae 2:482d74ef09c8 42 int Paddle::get_height() { return _height; }
eencae 2:482d74ef09c8 43
eencae 2:482d74ef09c8 44 int Paddle::get_width() { return _width; }