N5110

Dependencies:   mbed

Committer:
aileen
Date:
Tue Apr 28 01:39:30 2020 +0000
Revision:
0:90b7f20b3a8d
N5110

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aileen 0:90b7f20b3a8d 1 #include "Paddle.h"
aileen 0:90b7f20b3a8d 2
aileen 0:90b7f20b3a8d 3 // nothing doing in the constructor and destructor
aileen 0:90b7f20b3a8d 4 Paddle::Paddle()
aileen 0:90b7f20b3a8d 5 {
aileen 0:90b7f20b3a8d 6
aileen 0:90b7f20b3a8d 7 }
aileen 0:90b7f20b3a8d 8
aileen 0:90b7f20b3a8d 9 Paddle::~Paddle()
aileen 0:90b7f20b3a8d 10 {
aileen 0:90b7f20b3a8d 11
aileen 0:90b7f20b3a8d 12 }
aileen 0:90b7f20b3a8d 13
aileen 0:90b7f20b3a8d 14 void Paddle::init(int x,int height,int width)
aileen 0:90b7f20b3a8d 15 {
aileen 0:90b7f20b3a8d 16 _x = x; // x value on screen is fixed
aileen 0:90b7f20b3a8d 17 _y = HEIGHT/2 - height/2; // y depends on height of screen and height of paddle
aileen 0:90b7f20b3a8d 18 _height = height;
aileen 0:90b7f20b3a8d 19 _width = width;
aileen 0:90b7f20b3a8d 20 _speed = 1; // default speed
aileen 0:90b7f20b3a8d 21 _score = 0; // start score from zero
aileen 0:90b7f20b3a8d 22
aileen 0:90b7f20b3a8d 23 }
aileen 0:90b7f20b3a8d 24
aileen 0:90b7f20b3a8d 25 void Paddle::draw(N5110 &lcd)
aileen 0:90b7f20b3a8d 26 {
aileen 0:90b7f20b3a8d 27 // draw paddle in screen buffer.
aileen 0:90b7f20b3a8d 28 lcd.drawRect(_x,_y,_width,_height,FILL_BLACK);
aileen 0:90b7f20b3a8d 29 }
aileen 0:90b7f20b3a8d 30
aileen 0:90b7f20b3a8d 31 void Paddle::update(Direction d,float mag)
aileen 0:90b7f20b3a8d 32 {
aileen 0:90b7f20b3a8d 33 _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future
aileen 0:90b7f20b3a8d 34
aileen 0:90b7f20b3a8d 35 // update y value depending on direction of movement
aileen 0:90b7f20b3a8d 36 // North is decrement as origin is at the top-left so decreasing moves up
aileen 0:90b7f20b3a8d 37 if (d == N) {
aileen 0:90b7f20b3a8d 38 _y-=_speed;
aileen 0:90b7f20b3a8d 39 } else if (d == S) {
aileen 0:90b7f20b3a8d 40 _y+=_speed;
aileen 0:90b7f20b3a8d 41 }
aileen 0:90b7f20b3a8d 42
aileen 0:90b7f20b3a8d 43 // check the y origin to ensure that the paddle doesn't go off screen
aileen 0:90b7f20b3a8d 44 if (_y < 1) {
aileen 0:90b7f20b3a8d 45 _y = 1;
aileen 0:90b7f20b3a8d 46 }
aileen 0:90b7f20b3a8d 47 if (_y > HEIGHT - _height - 1) {
aileen 0:90b7f20b3a8d 48 _y = HEIGHT - _height - 1;
aileen 0:90b7f20b3a8d 49 }
aileen 0:90b7f20b3a8d 50 }
aileen 0:90b7f20b3a8d 51
aileen 0:90b7f20b3a8d 52 void Paddle::add_score()
aileen 0:90b7f20b3a8d 53 {
aileen 0:90b7f20b3a8d 54 _score++;
aileen 0:90b7f20b3a8d 55 }
aileen 0:90b7f20b3a8d 56 int Paddle::get_score()
aileen 0:90b7f20b3a8d 57 {
aileen 0:90b7f20b3a8d 58 return _score;
aileen 0:90b7f20b3a8d 59 }
aileen 0:90b7f20b3a8d 60
aileen 0:90b7f20b3a8d 61 Vector2D Paddle::get_pos() {
aileen 0:90b7f20b3a8d 62 Vector2D p = {_x,_y};
aileen 0:90b7f20b3a8d 63 return p;
aileen 0:90b7f20b3a8d 64 }