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.
Dependencies: mbed
Avenger/Avenger.cpp@14:13e82f720bea, 2019-05-09 (annotated)
- Committer:
 - davidwst421
 - Date:
 - Thu May 09 06:19:58 2019 +0000
 - Revision:
 - 14:13e82f720bea
 - Parent:
 - 12:660458c41c8e
 
final version with full comment on most functions/variables
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| davidwst421 | 0:fd8eda608206 | 1 | #include "Avenger.h" | 
| davidwst421 | 0:fd8eda608206 | 2 | |
| davidwst421 | 0:fd8eda608206 | 3 | // nothing doing in the constructor and destructor | 
| davidwst421 | 0:fd8eda608206 | 4 | Avenger::Avenger() | 
| davidwst421 | 0:fd8eda608206 | 5 | { | 
| davidwst421 | 0:fd8eda608206 | 6 | |
| davidwst421 | 0:fd8eda608206 | 7 | } | 
| davidwst421 | 0:fd8eda608206 | 8 | |
| davidwst421 | 0:fd8eda608206 | 9 | Avenger::~Avenger() | 
| davidwst421 | 0:fd8eda608206 | 10 | { | 
| davidwst421 | 0:fd8eda608206 | 11 | |
| davidwst421 | 0:fd8eda608206 | 12 | } | 
| davidwst421 | 0:fd8eda608206 | 13 | |
| davidwst421 | 14:13e82f720bea | 14 | const int player0[6][11] = { // avenger image | 
| davidwst421 | 7:193c0fd7afdd | 15 | { 0,0,0,0,1,1,1,0,0,0,0 }, | 
| davidwst421 | 7:193c0fd7afdd | 16 | { 0,1,0,0,0,0,1,1,0,1,0 }, | 
| davidwst421 | 7:193c0fd7afdd | 17 | { 1,1,1,1,1,1,1,1,1,1,1 }, | 
| davidwst421 | 7:193c0fd7afdd | 18 | { 1,1,1,1,1,1,1,1,1,1,1 }, | 
| davidwst421 | 7:193c0fd7afdd | 19 | { 0,1,0,0,0,0,1,1,0,1,0 }, | 
| davidwst421 | 7:193c0fd7afdd | 20 | { 0,0,0,0,1,1,1,0,0,0,0 }, | 
| davidwst421 | 7:193c0fd7afdd | 21 | }; | 
| davidwst421 | 7:193c0fd7afdd | 22 | |
| davidwst421 | 14:13e82f720bea | 23 | const int easteregg[6][11] = { //easter egg ;) | 
| davidwst421 | 12:660458c41c8e | 24 | { 0,0,0,0,0,0,0,1,1,1,1 }, | 
| davidwst421 | 12:660458c41c8e | 25 | { 1,0,0,0,0,0,0,1,1,1,1 }, | 
| davidwst421 | 12:660458c41c8e | 26 | { 1,1,1,1,1,1,1,1,1,1,1 }, | 
| davidwst421 | 12:660458c41c8e | 27 | { 1,1,1,1,1,1,1,1,1,1,1 }, | 
| davidwst421 | 12:660458c41c8e | 28 | { 1,0,0,0,0,0,0,1,1,1,1 }, | 
| davidwst421 | 12:660458c41c8e | 29 | { 0,0,0,0,0,0,0,1,1,1,1 }, | 
| davidwst421 | 12:660458c41c8e | 30 | }; | 
| davidwst421 | 12:660458c41c8e | 31 | |
| davidwst421 | 7:193c0fd7afdd | 32 | void Avenger::init(int x) { | 
| davidwst421 | 0:fd8eda608206 | 33 | |
| davidwst421 | 0:fd8eda608206 | 34 | _x = x; // x value on screen is fixed | 
| davidwst421 | 7:193c0fd7afdd | 35 | _y = HEIGHT/2 - 3; // y depends on height of screen and height of paddle | 
| davidwst421 | 14:13e82f720bea | 36 | _scale = 5; // scale determines how sensitive the joystick is | 
| davidwst421 | 14:13e82f720bea | 37 | _score = 1; // initiate the score | 
| davidwst421 | 12:660458c41c8e | 38 | _easter = rand() % 100; | 
| davidwst421 | 0:fd8eda608206 | 39 | } | 
| davidwst421 | 0:fd8eda608206 | 40 | |
| davidwst421 | 0:fd8eda608206 | 41 | void Avenger::draw(N5110 &lcd) { | 
| davidwst421 | 14:13e82f720bea | 42 | if (_easter < 5) { // there could be 5 percents playing as thor's hammer | 
| davidwst421 | 12:660458c41c8e | 43 | lcd.drawSprite(_x,_y,6,11,(int *)easteregg); | 
| davidwst421 | 12:660458c41c8e | 44 | } else { | 
| davidwst421 | 14:13e82f720bea | 45 | lcd.drawSprite(_x,_y,6,11,(int *)player0); // draw player0 on the screen | 
| davidwst421 | 12:660458c41c8e | 46 | } | 
| davidwst421 | 0:fd8eda608206 | 47 | } | 
| davidwst421 | 0:fd8eda608206 | 48 | |
| davidwst421 | 6:a0f3dbbc8d33 | 49 | void Avenger::update(Direction d,float mag,Vector2D mapped) { | 
| davidwst421 | 0:fd8eda608206 | 50 | |
| davidwst421 | 8:97576c8761a8 | 51 | _y -= int(mapped.y * _scale); // scale is arbitrary, could be changed in future | 
| davidwst421 | 0:fd8eda608206 | 52 | |
| davidwst421 | 0:fd8eda608206 | 53 | // check the y origin to ensure that the paddle doesn't go off screen | 
| davidwst421 | 0:fd8eda608206 | 54 | if (_y < 1) { | 
| davidwst421 | 0:fd8eda608206 | 55 | _y = 1; | 
| davidwst421 | 0:fd8eda608206 | 56 | } | 
| davidwst421 | 8:97576c8761a8 | 57 | if (_y > HEIGHT - 7) { | 
| davidwst421 | 8:97576c8761a8 | 58 | _y = HEIGHT - 7; | 
| davidwst421 | 0:fd8eda608206 | 59 | } | 
| davidwst421 | 0:fd8eda608206 | 60 | } | 
| davidwst421 | 0:fd8eda608206 | 61 | |
| davidwst421 | 14:13e82f720bea | 62 | void Avenger::add_score() { // add score when avenger retrieves the stone | 
| davidwst421 | 0:fd8eda608206 | 63 | _score++; | 
| davidwst421 | 0:fd8eda608206 | 64 | } | 
| davidwst421 | 0:fd8eda608206 | 65 | |
| davidwst421 | 14:13e82f720bea | 66 | void Avenger::lose_score() { // subtract the score when avenger collides with time-line | 
| davidwst421 | 0:fd8eda608206 | 67 | _score--; | 
| davidwst421 | 0:fd8eda608206 | 68 | } | 
| davidwst421 | 0:fd8eda608206 | 69 | |
| davidwst421 | 14:13e82f720bea | 70 | int Avenger::get_score() { // get score to turns on leds | 
| davidwst421 | 0:fd8eda608206 | 71 | return _score; | 
| davidwst421 | 0:fd8eda608206 | 72 | } | 
| davidwst421 | 0:fd8eda608206 | 73 | |
| davidwst421 | 14:13e82f720bea | 74 | Vector2D Avenger::get_pos() { // get position for collision logic | 
| davidwst421 | 0:fd8eda608206 | 75 | Vector2D p = {_x,_y}; | 
| davidwst421 | 0:fd8eda608206 | 76 | return p; | 
| davidwst421 | 0:fd8eda608206 | 77 | } |