![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
demo
Dependencies: mbed Gamepad N5110
the_wall/the_wall.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 "the_wall.h" |
Ting12138 | 0:ba32cfe0051e | 2 | |
Ting12138 | 0:ba32cfe0051e | 3 | the_wall::the_wall() |
Ting12138 | 0:ba32cfe0051e | 4 | { |
Ting12138 | 0:ba32cfe0051e | 5 | |
Ting12138 | 0:ba32cfe0051e | 6 | } |
Ting12138 | 0:ba32cfe0051e | 7 | |
Ting12138 | 0:ba32cfe0051e | 8 | the_wall::~the_wall() |
Ting12138 | 0:ba32cfe0051e | 9 | { |
Ting12138 | 0:ba32cfe0051e | 10 | |
Ting12138 | 0:ba32cfe0051e | 11 | } |
Ting12138 | 0:ba32cfe0051e | 12 | |
Ting12138 | 0:ba32cfe0051e | 13 | void the_wall::init(int x,int gap,int width,int speed) { |
Ting12138 | 0:ba32cfe0051e | 14 | _x = x; // wall x starting postion and updating position |
Ting12138 | 0:ba32cfe0051e | 15 | _gap = gap; // gap represents the distance between the wall |
Ting12138 | 0:ba32cfe0051e | 16 | int uncertainty = rand() % (47 - _gap); |
Ting12138 | 0:ba32cfe0051e | 17 | _height = uncertainty; // get random height for the wall |
Ting12138 | 0:ba32cfe0051e | 18 | _width = width; // fixed width |
Ting12138 | 0:ba32cfe0051e | 19 | _speed = speed; // adjuctable speed |
Ting12138 | 0:ba32cfe0051e | 20 | } |
Ting12138 | 0:ba32cfe0051e | 21 | |
Ting12138 | 0:ba32cfe0051e | 22 | void the_wall::draw1(N5110 &lcd) { |
Ting12138 | 0:ba32cfe0051e | 23 | // draw wall in screen buffer. |
Ting12138 | 0:ba32cfe0051e | 24 | lcd.drawRect(_x,0,_width,_height,FILL_BLACK); |
Ting12138 | 0:ba32cfe0051e | 25 | } |
Ting12138 | 0:ba32cfe0051e | 26 | |
Ting12138 | 0:ba32cfe0051e | 27 | void the_wall::draw2(N5110 &lcd) { |
Ting12138 | 0:ba32cfe0051e | 28 | lcd.drawRect(_x,(_gap + _height),_width,(HEIGHT - (_gap + _height)),FILL_BLACK); // draw the wall near the bottom |
Ting12138 | 0:ba32cfe0051e | 29 | } |
Ting12138 | 0:ba32cfe0051e | 30 | |
Ting12138 | 0:ba32cfe0051e | 31 | void the_wall::update() { |
Ting12138 | 0:ba32cfe0051e | 32 | _x -= _speed; // speed is arbitrary, could be changed in the future, and the wall is shifting to left |
Ting12138 | 0:ba32cfe0051e | 33 | } |
Ting12138 | 0:ba32cfe0051e | 34 | |
Ting12138 | 0:ba32cfe0051e | 35 | void the_wall::reset() { // when the wall is off from the left screen, replace it back to the right side and continue the game |
Ting12138 | 0:ba32cfe0051e | 36 | _x = 149; |
Ting12138 | 0:ba32cfe0051e | 37 | int uncertainty = rand() % (47 - _gap); |
Ting12138 | 0:ba32cfe0051e | 38 | _height = uncertainty; |
Ting12138 | 0:ba32cfe0051e | 39 | } |
Ting12138 | 0:ba32cfe0051e | 40 | |
Ting12138 | 0:ba32cfe0051e | 41 | int the_wall::get_x() { // get x position and wall height for collision logic function |
Ting12138 | 0:ba32cfe0051e | 42 | return _x; |
Ting12138 | 0:ba32cfe0051e | 43 | } |
Ting12138 | 0:ba32cfe0051e | 44 | int the_wall::get_height() { |
Ting12138 | 0:ba32cfe0051e | 45 | return _height; |
Ting12138 | 0:ba32cfe0051e | 46 | } |