![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Li Ruofan 201199450
Dependencies: mbed Gamepad Joystick
myShip/spaceship.cpp@5:e3a9f0548922, 2020-05-15 (annotated)
- Committer:
- DannyLee
- Date:
- Fri May 15 19:57:40 2020 +0000
- Revision:
- 5:e3a9f0548922
- Parent:
- 3:cf9fead9c3f4
- Child:
- 6:cbd9e1f26a10
need to debug
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:cf9fead9c3f4 | 32 | int Spaceship[15][10] = { |
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 | 3:cf9fead9c3f4 | 50 | lcd.drawSprite(lcd, _x,_y,_height,_width,(int)Spaceship);// Specify rows and columns in sprite |
DannyLee | 3:cf9fead9c3f4 | 51 | }; |
DannyLee | 3:cf9fead9c3f4 | 52 | |
DannyLee | 3:cf9fead9c3f4 | 53 | void loop() { |
DannyLee | 3:cf9fead9c3f4 | 54 | display.clearDisplay(); // clears the screen and start new |
DannyLee | 3:cf9fead9c3f4 | 55 | |
DannyLee | 3:cf9fead9c3f4 | 56 | gamescreen(); //Displays the box, score and speed values |
DannyLee | 3:cf9fead9c3f4 | 57 | |
DannyLee | 3:cf9fead9c3f4 | 58 | //Get input from user |
DannyLee | 3:cf9fead9c3f4 | 59 | Joy_X = analogRead(A1); //It will read the X value from Joystick |
DannyLee | 3:cf9fead9c3f4 | 60 | if (Joy_X < 312 && POS!=1 && control==true) //If joy stick moves right |
DannyLee | 3:cf9fead9c3f4 | 61 | { POS--; control = false;} //Decreace position of spaceship |
DannyLee | 3:cf9fead9c3f4 | 62 | else if (Joy_X > 712 && POS!=3 && control==true) //If joy stick moves right |
DannyLee | 3:cf9fead9c3f4 | 63 | { POS++; control = false;} //Increace position of spaceship |
DannyLee | 3:cf9fead9c3f4 | 64 | else if (Joy_X >502 && Joy_X<522) //If joystick back to initial position |
DannyLee | 3:cf9fead9c3f4 | 65 | control = true; //Prepare for next move |
DannyLee | 3:cf9fead9c3f4 | 66 | //Input from user received |
DannyLee | 3:cf9fead9c3f4 | 67 | |
DannyLee | 5:e3a9f0548922 | 68 | // get the position of the spaceship |
DannyLee | 5:e3a9f0548922 | 69 | Vector2D Spaceship::get_pos() |
DannyLee | 5:e3a9f0548922 | 70 | { |
DannyLee | 3:cf9fead9c3f4 | 71 | Vector2D p = {_x,_y}; |
DannyLee | 3:cf9fead9c3f4 | 72 | return p; |
DannyLee | 5:e3a9f0548922 | 73 | } |
DannyLee | 5:e3a9f0548922 | 74 | // set the position of the spaceship |
DannyLee | 5:e3a9f0548922 | 75 | void Spaceship::set_pos(Vector2D p) |
DannyLee | 5:e3a9f0548922 | 76 | { |
DannyLee | 5:e3a9f0548922 | 77 | _x = p.x; |
DannyLee | 5:e3a9f0548922 | 78 | _y = p.y; |
DannyLee | 5:e3a9f0548922 | 79 | } |
DannyLee | 5:e3a9f0548922 | 80 | |
DannyLee | 5:e3a9f0548922 | 81 | // add score when pass pillars |
DannyLee | 5:e3a9f0548922 | 82 | void Spaceship::add_score() |
DannyLee | 5:e3a9f0548922 | 83 | { |
DannyLee | 5:e3a9f0548922 | 84 | _score++; |
DannyLee | 5:e3a9f0548922 | 85 | } |
DannyLee | 5:e3a9f0548922 | 86 | |
DannyLee | 5:e3a9f0548922 | 87 | // get score |
DannyLee | 5:e3a9f0548922 | 88 | int Spaceship::get_score() |
DannyLee | 5:e3a9f0548922 | 89 | { |
DannyLee | 5:e3a9f0548922 | 90 | return _score; |
DannyLee | 5:e3a9f0548922 | 91 | } |
DannyLee | 5:e3a9f0548922 | 92 | |
DannyLee | 5:e3a9f0548922 | 93 | // the life of the spaceship |
DannyLee | 5:e3a9f0548922 | 94 | void Spaceship::set_life() |
DannyLee | 5:e3a9f0548922 | 95 | { |
DannyLee | 5:e3a9f0548922 | 96 | _life++; |
DannyLee | 5:e3a9f0548922 | 97 | } |
DannyLee | 5:e3a9f0548922 | 98 | |
DannyLee | 5:e3a9f0548922 | 99 | // get life |
DannyLee | 5:e3a9f0548922 | 100 | int Spaceship::get_life() |
DannyLee | 5:e3a9f0548922 | 101 | { |
DannyLee | 5:e3a9f0548922 | 102 | return _life; |
DannyLee | 3:cf9fead9c3f4 | 103 | } |