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@12:660458c41c8e, 2019-05-09 (annotated)
- Committer:
 - davidwst421
 - Date:
 - Thu May 09 04:26:16 2019 +0000
 - Revision:
 - 12:660458c41c8e
 - Parent:
 - 8:97576c8761a8
 - Child:
 - 14:13e82f720bea
 
Add easter egg
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 | 7:193c0fd7afdd | 14 | const int player0[6][11] = { | 
| 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 | 12:660458c41c8e | 23 | const int easteregg[6][11] = { | 
| 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 | 8:97576c8761a8 | 36 | _scale = 5; | 
| davidwst421 | 0:fd8eda608206 | 37 | _score = 1; | 
| 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 | 12:660458c41c8e | 42 | // draw paddle in screen buffer. | 
| davidwst421 | 12:660458c41c8e | 43 | if (_easter > 99) { | 
| davidwst421 | 12:660458c41c8e | 44 | lcd.drawSprite(_x,_y,6,11,(int *)easteregg); | 
| davidwst421 | 12:660458c41c8e | 45 | } else { | 
| davidwst421 | 12:660458c41c8e | 46 | lcd.drawSprite(_x,_y,6,11,(int *)player0); | 
| davidwst421 | 12:660458c41c8e | 47 | } | 
| davidwst421 | 0:fd8eda608206 | 48 | } | 
| davidwst421 | 0:fd8eda608206 | 49 | |
| davidwst421 | 6:a0f3dbbc8d33 | 50 | void Avenger::update(Direction d,float mag,Vector2D mapped) { | 
| davidwst421 | 0:fd8eda608206 | 51 | |
| davidwst421 | 8:97576c8761a8 | 52 | _y -= int(mapped.y * _scale); // scale is arbitrary, could be changed in future | 
| davidwst421 | 0:fd8eda608206 | 53 | |
| davidwst421 | 0:fd8eda608206 | 54 | // update y value depending on direction of movement | 
| davidwst421 | 0:fd8eda608206 | 55 | // North is decrement as origin is at the top-left so decreasing moves up | 
| davidwst421 | 6:a0f3dbbc8d33 | 56 | |
| davidwst421 | 0:fd8eda608206 | 57 | // check the y origin to ensure that the paddle doesn't go off screen | 
| davidwst421 | 0:fd8eda608206 | 58 | if (_y < 1) { | 
| davidwst421 | 0:fd8eda608206 | 59 | _y = 1; | 
| davidwst421 | 0:fd8eda608206 | 60 | } | 
| davidwst421 | 8:97576c8761a8 | 61 | if (_y > HEIGHT - 7) { | 
| davidwst421 | 8:97576c8761a8 | 62 | _y = HEIGHT - 7; | 
| davidwst421 | 0:fd8eda608206 | 63 | } | 
| davidwst421 | 0:fd8eda608206 | 64 | } | 
| davidwst421 | 0:fd8eda608206 | 65 | |
| davidwst421 | 0:fd8eda608206 | 66 | void Avenger::add_score() { | 
| davidwst421 | 0:fd8eda608206 | 67 | _score++; | 
| davidwst421 | 0:fd8eda608206 | 68 | } | 
| davidwst421 | 0:fd8eda608206 | 69 | |
| davidwst421 | 0:fd8eda608206 | 70 | void Avenger::lose_score() { | 
| davidwst421 | 0:fd8eda608206 | 71 | _score--; | 
| davidwst421 | 0:fd8eda608206 | 72 | } | 
| davidwst421 | 0:fd8eda608206 | 73 | |
| davidwst421 | 0:fd8eda608206 | 74 | int Avenger::get_score() { | 
| davidwst421 | 0:fd8eda608206 | 75 | return _score; | 
| davidwst421 | 0:fd8eda608206 | 76 | } | 
| davidwst421 | 0:fd8eda608206 | 77 | |
| davidwst421 | 0:fd8eda608206 | 78 | Vector2D Avenger::get_pos() { | 
| davidwst421 | 0:fd8eda608206 | 79 | Vector2D p = {_x,_y}; | 
| davidwst421 | 0:fd8eda608206 | 80 | return p; | 
| davidwst421 | 0:fd8eda608206 | 81 | } |