James Heavey / Mbed 2 deprecated EL17JH

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Apr 16 18:58:18 2019 +0000
Revision:
0:7d4d2023ed9c
Child:
1:e18615d46071
Game changed to breakout form space invaders (new folder) template initialised, edited paddle, edited direction of movement, edited collision boundaries

Who changed what in which revision?

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