Li Ruofan 201199450
Dependencies: mbed Gamepad Joystick
Diff: myShip/spaceship.cpp
- Revision:
- 3:cf9fead9c3f4
- Child:
- 5:e3a9f0548922
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/myShip/spaceship.cpp Thu May 14 13:12:28 2020 +0000 @@ -0,0 +1,71 @@ +/* SPACE RACE Game using Arduino and Nokia 5110 LCD + * Coded by: Ruofan Li + * Date: 28-4-2020 + * Input -> Joystick (x0,y0) +*/ + +#include <spaceship.h> // Library for spaceship.cpp + +//*Construct and Destruct +Spaceship::Spaceship() +{ + +} + +Spaceship::~Spaceship() +{ + +} + +void Spaceship::init(int x,int y,int width,int height) +{ + _x = x; //x value is fixed + _y = y; //y is also fixed + _width = width; + _height = height; + _speed = 4; +} + +void Spaceship::draw(N5110 &lcd) +{ + // draw Spaceship in screen of N5110. +int Spaceship[15][10] = { + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,1,1,0,0,0,0, + 0,0,0,0,1,1,0,0,0,0, + 0,0,1,1,0,0,1,1,0,0, + 0,1,1,1,1,1,1,1,1,0, + 0,1,1,1,1,1,1,1,1,0, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,0,0,1,1,1,1, + 0,1,1,1,0,0,1,1,1,0, + 0,0,1,1,0,0,1,1,0,0, + 0,0,0,1,1,1,1,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + + lcd.drawSprite(lcd, _x,_y,_height,_width,(int)Spaceship);// Specify rows and columns in sprite +}; + +void loop() { + display.clearDisplay(); // clears the screen and start new + + gamescreen(); //Displays the box, score and speed values + + //Get input from user + Joy_X = analogRead(A1); //It will read the X value from Joystick + if (Joy_X < 312 && POS!=1 && control==true) //If joy stick moves right + { POS--; control = false;} //Decreace position of spaceship + else if (Joy_X > 712 && POS!=3 && control==true) //If joy stick moves right + { POS++; control = false;} //Increace position of spaceship + else if (Joy_X >502 && Joy_X<522) //If joystick back to initial position + control = true; //Prepare for next move + //Input from user received + +Vector2D Spaceship::getPos(){ + Vector2D p = {_x,_y}; + return p; +} \ No newline at end of file