Li Ruofan 201199450

Dependencies:   mbed Gamepad Joystick

Committer:
DannyLee
Date:
Sat May 16 17:26:10 2020 +0000
Revision:
8:b4a2954dd74f
Parent:
myShip/spaceship.cpp@6:cbd9e1f26a10
Debugging

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DannyLee 3:cf9fead9c3f4 1 /* SPACE RACE Game using Arduino and Nokia 5110 LCD
DannyLee 3:cf9fead9c3f4 2 * Coded by: Ruofan Li
DannyLee 3:cf9fead9c3f4 3 * Date: 28-4-2020
DannyLee 3:cf9fead9c3f4 4 * Input -> Joystick (x0,y0)
DannyLee 3:cf9fead9c3f4 5 */
DannyLee 3:cf9fead9c3f4 6
DannyLee 3:cf9fead9c3f4 7 #include <spaceship.h> // Library for spaceship.cpp
DannyLee 3:cf9fead9c3f4 8
DannyLee 3:cf9fead9c3f4 9 //*Construct and Destruct
DannyLee 3:cf9fead9c3f4 10 Spaceship::Spaceship()
DannyLee 3:cf9fead9c3f4 11 {
DannyLee 3:cf9fead9c3f4 12
DannyLee 3:cf9fead9c3f4 13 }
DannyLee 3:cf9fead9c3f4 14
DannyLee 3:cf9fead9c3f4 15 Spaceship::~Spaceship()
DannyLee 3:cf9fead9c3f4 16 {
DannyLee 3:cf9fead9c3f4 17
DannyLee 3:cf9fead9c3f4 18 }
DannyLee 3:cf9fead9c3f4 19
DannyLee 3:cf9fead9c3f4 20 void Spaceship::init(int x,int y,int width,int height)
DannyLee 3:cf9fead9c3f4 21 {
DannyLee 3:cf9fead9c3f4 22 _x = x; //x value is fixed
DannyLee 3:cf9fead9c3f4 23 _y = y; //y is also fixed
DannyLee 3:cf9fead9c3f4 24 _width = width;
DannyLee 3:cf9fead9c3f4 25 _height = height;
DannyLee 3:cf9fead9c3f4 26 _speed = 4;
DannyLee 3:cf9fead9c3f4 27 }
DannyLee 3:cf9fead9c3f4 28
DannyLee 3:cf9fead9c3f4 29 void Spaceship::draw(N5110 &lcd)
DannyLee 3:cf9fead9c3f4 30 {
DannyLee 3:cf9fead9c3f4 31 // draw Spaceship in screen of N5110.
DannyLee 8:b4a2954dd74f 32 int spaceship[] = {
DannyLee 3:cf9fead9c3f4 33 0,0,0,0,0,0,0,0,0,0,
DannyLee 3:cf9fead9c3f4 34 0,0,0,0,1,1,0,0,0,0,
DannyLee 3:cf9fead9c3f4 35 0,0,0,0,1,1,0,0,0,0,
DannyLee 3:cf9fead9c3f4 36 0,0,1,1,0,0,1,1,0,0,
DannyLee 3:cf9fead9c3f4 37 0,1,1,1,1,1,1,1,1,0,
DannyLee 3:cf9fead9c3f4 38 0,1,1,1,1,1,1,1,1,0,
DannyLee 3:cf9fead9c3f4 39 1,1,1,1,1,1,1,1,1,1,
DannyLee 3:cf9fead9c3f4 40 1,1,1,1,1,1,1,1,1,1,
DannyLee 3:cf9fead9c3f4 41 1,1,1,1,1,1,1,1,1,1,
DannyLee 3:cf9fead9c3f4 42 1,1,1,1,1,1,1,1,1,1,
DannyLee 3:cf9fead9c3f4 43 1,1,1,1,1,1,1,1,1,1,
DannyLee 3:cf9fead9c3f4 44 1,1,1,1,0,0,1,1,1,1,
DannyLee 3:cf9fead9c3f4 45 0,1,1,1,0,0,1,1,1,0,
DannyLee 3:cf9fead9c3f4 46 0,0,1,1,0,0,1,1,0,0,
DannyLee 3:cf9fead9c3f4 47 0,0,0,1,1,1,1,0,0,0,
DannyLee 3:cf9fead9c3f4 48 0,0,0,0,0,0,0,0,0,0,
DannyLee 3:cf9fead9c3f4 49
DannyLee 8:b4a2954dd74f 50 Bitmap sprite(spaceship, _sizeX, _sizeY);
DannyLee 6:cbd9e1f26a10 51 sprite.render(lcd, _x, _y);
DannyLee 3:cf9fead9c3f4 52 };
DannyLee 3:cf9fead9c3f4 53
DannyLee 6:cbd9e1f26a10 54 void Spaceship::update(int d)
DannyLee 6:cbd9e1f26a10 55 {
DannyLee 3:cf9fead9c3f4 56
DannyLee 6:cbd9e1f26a10 57 if(d == 3){
DannyLee 6:cbd9e1f26a10 58 //turn right
DannyLee 6:cbd9e1f26a10 59 _x+=_speed;
DannyLee 6:cbd9e1f26a10 60
DannyLee 6:cbd9e1f26a10 61 if(_x>75){
DannyLee 6:cbd9e1f26a10 62
DannyLee 6:cbd9e1f26a10 63 _x = 75;
DannyLee 6:cbd9e1f26a10 64 }
DannyLee 6:cbd9e1f26a10 65 }else if(d == 7){
DannyLee 6:cbd9e1f26a10 66 //turn left
DannyLee 6:cbd9e1f26a10 67 _x-=_speed;
DannyLee 6:cbd9e1f26a10 68
DannyLee 6:cbd9e1f26a10 69 if(_x<0) {
DannyLee 6:cbd9e1f26a10 70 _x = 0;
DannyLee 6:cbd9e1f26a10 71 }
DannyLee 6:cbd9e1f26a10 72 }
DannyLee 6:cbd9e1f26a10 73 }
DannyLee 3:cf9fead9c3f4 74
DannyLee 5:e3a9f0548922 75 // get the position of the spaceship
DannyLee 6:cbd9e1f26a10 76 Vector2D Spaceship::get_Pos()
DannyLee 5:e3a9f0548922 77 {
DannyLee 3:cf9fead9c3f4 78 Vector2D p = {_x,_y};
DannyLee 3:cf9fead9c3f4 79 return p;
DannyLee 3:cf9fead9c3f4 80 }