Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
rocket/rocket.cpp@13:a57a48e5e256, 2020-05-14 (annotated)
- Committer:
- Ting12138
- Date:
- Thu May 14 15:42:14 2020 +0000
- Revision:
- 13:a57a48e5e256
- Parent:
- 12:3b7811c3502c
the 2645 project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ting12138 | 12:3b7811c3502c | 1 | #include "rocket.h" |
Ting12138 | 12:3b7811c3502c | 2 | |
Ting12138 | 12:3b7811c3502c | 3 | // nothing doing in the constructor and destructor |
Ting12138 | 12:3b7811c3502c | 4 | rocket::rocket() |
Ting12138 | 12:3b7811c3502c | 5 | { |
Ting12138 | 12:3b7811c3502c | 6 | |
Ting12138 | 12:3b7811c3502c | 7 | } |
Ting12138 | 12:3b7811c3502c | 8 | |
Ting12138 | 12:3b7811c3502c | 9 | rocket::~rocket() |
Ting12138 | 12:3b7811c3502c | 10 | { |
Ting12138 | 12:3b7811c3502c | 11 | |
Ting12138 | 12:3b7811c3502c | 12 | } |
Ting12138 | 12:3b7811c3502c | 13 | |
Ting12138 | 12:3b7811c3502c | 14 | const int the_player[6][11] = { |
Ting12138 | 12:3b7811c3502c | 15 | // The image shape of rocket |
Ting12138 | 12:3b7811c3502c | 16 | { 1,0,0,0,1,1,0,0,1,0,0 }, |
Ting12138 | 12:3b7811c3502c | 17 | { 1,1,0,0,0,0,1,0,1,1,0 }, |
Ting12138 | 12:3b7811c3502c | 18 | { 1,1,1,1,1,1,1,1,1,1,1 }, |
Ting12138 | 12:3b7811c3502c | 19 | { 1,1,1,1,1,1,1,1,1,1,1 }, |
Ting12138 | 12:3b7811c3502c | 20 | { 1,1,0,0,0,0,1,0,1,1,0 }, |
Ting12138 | 12:3b7811c3502c | 21 | { 1,0,0,0,1,1,0,0,1,0,0 }, |
Ting12138 | 12:3b7811c3502c | 22 | }; |
Ting12138 | 12:3b7811c3502c | 23 | |
Ting12138 | 12:3b7811c3502c | 24 | const int another_role[6][11] = { |
Ting12138 | 12:3b7811c3502c | 25 | { 0,0,0,0,0,0,0,0,0,0,0 }, |
Ting12138 | 12:3b7811c3502c | 26 | { 0,0,1,1,0,1,1,1,0,0,0 }, |
Ting12138 | 12:3b7811c3502c | 27 | { 0,1,1,1,1,1,1,1,1,0,0 }, |
Ting12138 | 12:3b7811c3502c | 28 | { 1,1,1,1,1,1,1,1,1,1,1 }, |
Ting12138 | 12:3b7811c3502c | 29 | { 0,1,1,1,1,1,0,1,1,0,0 }, |
Ting12138 | 12:3b7811c3502c | 30 | { 0,0,1,1,0,0,1,1,0,0,0 }, |
Ting12138 | 12:3b7811c3502c | 31 | }; |
Ting12138 | 12:3b7811c3502c | 32 | |
Ting12138 | 12:3b7811c3502c | 33 | void rocket::init(int x) { |
Ting12138 | 12:3b7811c3502c | 34 | |
Ting12138 | 12:3b7811c3502c | 35 | _x = x; // x value on screen is fixed |
Ting12138 | 12:3b7811c3502c | 36 | _y = HEIGHT/2 - 3; // y depends on height of screen and height of paddle |
Ting12138 | 12:3b7811c3502c | 37 | _scale = 5; // scale determines how sensitive the joystick is |
Ting12138 | 12:3b7811c3502c | 38 | _score = 1; // initiate the score |
Ting12138 | 12:3b7811c3502c | 39 | _easter = rand() % 100; |
Ting12138 | 12:3b7811c3502c | 40 | } |
Ting12138 | 12:3b7811c3502c | 41 | |
Ting12138 | 12:3b7811c3502c | 42 | void rocket::draw(N5110 &lcd) { |
Ting12138 | 12:3b7811c3502c | 43 | if (_easter < 5) { // there could be 5 percents playing as thor's hammer |
Ting12138 | 12:3b7811c3502c | 44 | lcd.drawSprite(_x,_y,6,11,(int *)another_role); |
Ting12138 | 12:3b7811c3502c | 45 | } else { |
Ting12138 | 12:3b7811c3502c | 46 | lcd.drawSprite(_x,_y,6,11,(int *)the_player); // draw player0 on the screen |
Ting12138 | 12:3b7811c3502c | 47 | } |
Ting12138 | 12:3b7811c3502c | 48 | } |
Ting12138 | 12:3b7811c3502c | 49 | |
Ting12138 | 12:3b7811c3502c | 50 | void rocket::replace(Direction d,float mag,Vector2D mapped) { |
Ting12138 | 12:3b7811c3502c | 51 | _y -= int(mapped.y * _scale); // scale is arbitrary, could be changed in future |
Ting12138 | 12:3b7811c3502c | 52 | |
Ting12138 | 12:3b7811c3502c | 53 | // check the y origin to ensure that the paddle doesn't go off screen |
Ting12138 | 12:3b7811c3502c | 54 | if (_y < 1) { |
Ting12138 | 12:3b7811c3502c | 55 | _y = 1; |
Ting12138 | 12:3b7811c3502c | 56 | } |
Ting12138 | 12:3b7811c3502c | 57 | if (_y > HEIGHT - 7) { |
Ting12138 | 12:3b7811c3502c | 58 | _y = HEIGHT - 7; |
Ting12138 | 12:3b7811c3502c | 59 | } |
Ting12138 | 12:3b7811c3502c | 60 | } |
Ting12138 | 12:3b7811c3502c | 61 | |
Ting12138 | 12:3b7811c3502c | 62 | |
Ting12138 | 12:3b7811c3502c | 63 | void rocket::add_score() { // add score when rocket retrieves the foods |
Ting12138 | 12:3b7811c3502c | 64 | _score++; |
Ting12138 | 12:3b7811c3502c | 65 | } |
Ting12138 | 12:3b7811c3502c | 66 | |
Ting12138 | 12:3b7811c3502c | 67 | void rocket::lose_score() { |
Ting12138 | 12:3b7811c3502c | 68 | _score--; |
Ting12138 | 12:3b7811c3502c | 69 | } |
Ting12138 | 12:3b7811c3502c | 70 | |
Ting12138 | 12:3b7811c3502c | 71 | int rocket::get_final_score() { |
Ting12138 | 12:3b7811c3502c | 72 | return _score; |
Ting12138 | 12:3b7811c3502c | 73 | } |
Ting12138 | 12:3b7811c3502c | 74 | |
Ting12138 | 13:a57a48e5e256 | 75 | Vector2D rocket::get_locations() { |
Ting12138 | 12:3b7811c3502c | 76 | Vector2D p = {_x,_y}; |
Ting12138 | 12:3b7811c3502c | 77 | return p; |
Ting12138 | 12:3b7811c3502c | 78 | } |