![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
demo
Dependencies: mbed Gamepad N5110
rocket/rocket.cpp@1:7b5a843acc05, 2020-05-14 (annotated)
- Committer:
- Ting12138
- Date:
- Thu May 14 13:00:19 2020 +0000
- Revision:
- 1:7b5a843acc05
- Parent:
- 0:ba32cfe0051e
homework
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ting12138 | 0:ba32cfe0051e | 1 | #include "rocket.h" |
Ting12138 | 0:ba32cfe0051e | 2 | |
Ting12138 | 0:ba32cfe0051e | 3 | // nothing doing in the constructor and destructor |
Ting12138 | 0:ba32cfe0051e | 4 | rocket::rocket() |
Ting12138 | 0:ba32cfe0051e | 5 | { |
Ting12138 | 0:ba32cfe0051e | 6 | |
Ting12138 | 0:ba32cfe0051e | 7 | } |
Ting12138 | 0:ba32cfe0051e | 8 | |
Ting12138 | 0:ba32cfe0051e | 9 | rocket::~rocket() |
Ting12138 | 0:ba32cfe0051e | 10 | { |
Ting12138 | 0:ba32cfe0051e | 11 | |
Ting12138 | 0:ba32cfe0051e | 12 | } |
Ting12138 | 0:ba32cfe0051e | 13 | |
Ting12138 | 0:ba32cfe0051e | 14 | const int the_player[6][11] = { |
Ting12138 | 0:ba32cfe0051e | 15 | // The image shape of rocket |
Ting12138 | 0:ba32cfe0051e | 16 | { 1,0,0,0,1,1,0,0,1,0,0 }, |
Ting12138 | 0:ba32cfe0051e | 17 | { 1,1,0,0,0,0,1,0,1,1,0 }, |
Ting12138 | 0:ba32cfe0051e | 18 | { 1,1,1,1,1,1,1,1,1,1,1 }, |
Ting12138 | 0:ba32cfe0051e | 19 | { 1,1,1,1,1,1,1,1,1,1,1 }, |
Ting12138 | 0:ba32cfe0051e | 20 | { 1,1,0,0,0,0,1,0,1,1,0 }, |
Ting12138 | 0:ba32cfe0051e | 21 | { 1,0,0,0,1,1,0,0,1,0,0 }, |
Ting12138 | 0:ba32cfe0051e | 22 | }; |
Ting12138 | 0:ba32cfe0051e | 23 | |
Ting12138 | 0:ba32cfe0051e | 24 | const int another_role[6][11] = { |
Ting12138 | 0:ba32cfe0051e | 25 | { 0,0,0,0,0,0,0,0,0,0,0 }, |
Ting12138 | 0:ba32cfe0051e | 26 | { 0,0,1,1,0,1,1,1,0,0,0 }, |
Ting12138 | 0:ba32cfe0051e | 27 | { 0,1,1,1,1,1,1,1,1,0,0 }, |
Ting12138 | 0:ba32cfe0051e | 28 | { 1,1,1,1,1,1,1,1,1,1,1 }, |
Ting12138 | 0:ba32cfe0051e | 29 | { 0,1,1,1,1,1,0,1,1,0,0 }, |
Ting12138 | 0:ba32cfe0051e | 30 | { 0,0,1,1,0,0,1,1,0,0,0 }, |
Ting12138 | 0:ba32cfe0051e | 31 | }; |
Ting12138 | 0:ba32cfe0051e | 32 | |
Ting12138 | 0:ba32cfe0051e | 33 | void rocket::init(int x) { |
Ting12138 | 0:ba32cfe0051e | 34 | |
Ting12138 | 0:ba32cfe0051e | 35 | _x = x; // x value on screen is fixed |
Ting12138 | 0:ba32cfe0051e | 36 | _y = HEIGHT/2 - 3; // y depends on height of screen and height of paddle |
Ting12138 | 0:ba32cfe0051e | 37 | _scale = 5; // scale determines how sensitive the joystick is |
Ting12138 | 0:ba32cfe0051e | 38 | _score = 1; // initiate the score |
Ting12138 | 0:ba32cfe0051e | 39 | _easter = rand() % 100; |
Ting12138 | 0:ba32cfe0051e | 40 | } |
Ting12138 | 0:ba32cfe0051e | 41 | |
Ting12138 | 0:ba32cfe0051e | 42 | void rocket::draw(N5110 &lcd) { |
Ting12138 | 0:ba32cfe0051e | 43 | if (_easter < 5) { // there could be 5 percents playing as thor's hammer |
Ting12138 | 0:ba32cfe0051e | 44 | lcd.drawSprite(_x,_y,6,11,(int *)another_role); |
Ting12138 | 0:ba32cfe0051e | 45 | } else { |
Ting12138 | 0:ba32cfe0051e | 46 | lcd.drawSprite(_x,_y,6,11,(int *)the_player); // draw player0 on the screen |
Ting12138 | 0:ba32cfe0051e | 47 | } |
Ting12138 | 0:ba32cfe0051e | 48 | } |
Ting12138 | 0:ba32cfe0051e | 49 | |
Ting12138 | 0:ba32cfe0051e | 50 | void rocket::replace(Direction d,float mag,Vector2D mapped) { |
Ting12138 | 0:ba32cfe0051e | 51 | _y -= int(mapped.y * _scale); // scale is arbitrary, could be changed in future |
Ting12138 | 0:ba32cfe0051e | 52 | |
Ting12138 | 0:ba32cfe0051e | 53 | // check the y origin to ensure that the paddle doesn't go off screen |
Ting12138 | 0:ba32cfe0051e | 54 | if (_y < 1) { |
Ting12138 | 0:ba32cfe0051e | 55 | _y = 1; |
Ting12138 | 0:ba32cfe0051e | 56 | } |
Ting12138 | 0:ba32cfe0051e | 57 | if (_y > HEIGHT - 7) { |
Ting12138 | 0:ba32cfe0051e | 58 | _y = HEIGHT - 7; |
Ting12138 | 0:ba32cfe0051e | 59 | } |
Ting12138 | 0:ba32cfe0051e | 60 | } |
Ting12138 | 0:ba32cfe0051e | 61 | |
Ting12138 | 0:ba32cfe0051e | 62 | |
Ting12138 | 0:ba32cfe0051e | 63 | void rocket::add_score() { // add score when rocket retrieves the foods |
Ting12138 | 0:ba32cfe0051e | 64 | _score++; |
Ting12138 | 0:ba32cfe0051e | 65 | } |
Ting12138 | 0:ba32cfe0051e | 66 | |
Ting12138 | 0:ba32cfe0051e | 67 | void rocket::lose_score() { |
Ting12138 | 0:ba32cfe0051e | 68 | _score--; |
Ting12138 | 0:ba32cfe0051e | 69 | } |
Ting12138 | 0:ba32cfe0051e | 70 | |
Ting12138 | 0:ba32cfe0051e | 71 | int rocket::get_final_score() { |
Ting12138 | 0:ba32cfe0051e | 72 | return _score; |
Ting12138 | 0:ba32cfe0051e | 73 | } |
Ting12138 | 0:ba32cfe0051e | 74 | |
Ting12138 | 0:ba32cfe0051e | 75 | Vector2D rocket::get_pos() { |
Ting12138 | 0:ba32cfe0051e | 76 | Vector2D p = {_x,_y}; |
Ting12138 | 0:ba32cfe0051e | 77 | return p; |
Ting12138 | 0:ba32cfe0051e | 78 | } |