demo
Dependencies: mbed Gamepad N5110
foods/foods.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 "foods.h" |
Ting12138 | 0:ba32cfe0051e | 2 | |
Ting12138 | 0:ba32cfe0051e | 3 | foods::foods() |
Ting12138 | 0:ba32cfe0051e | 4 | { |
Ting12138 | 0:ba32cfe0051e | 5 | |
Ting12138 | 0:ba32cfe0051e | 6 | } |
Ting12138 | 0:ba32cfe0051e | 7 | |
Ting12138 | 0:ba32cfe0051e | 8 | foods::~foods() |
Ting12138 | 0:ba32cfe0051e | 9 | { |
Ting12138 | 0:ba32cfe0051e | 10 | |
Ting12138 | 0:ba32cfe0051e | 11 | } |
Ting12138 | 0:ba32cfe0051e | 12 | |
Ting12138 | 0:ba32cfe0051e | 13 | void foods::init(int x,int size,int velocity) { |
Ting12138 | 0:ba32cfe0051e | 14 | _x = x; // foods x beginning postion and updating position |
Ting12138 | 0:ba32cfe0051e | 15 | _size = size; // the food's size |
Ting12138 | 0:ba32cfe0051e | 16 | int uncertainty = rand() % (48 - _size * 2); |
Ting12138 | 0:ba32cfe0051e | 17 | _y = _size + uncertainty; // the starting vertical position |
Ting12138 | 0:ba32cfe0051e | 18 | _velocity = velocity; // sliding velocity |
Ting12138 | 0:ba32cfe0051e | 19 | } |
Ting12138 | 0:ba32cfe0051e | 20 | |
Ting12138 | 0:ba32cfe0051e | 21 | void foods::draw(N5110 &lcd) { |
Ting12138 | 0:ba32cfe0051e | 22 | lcd.drawCircle(_x,_y,_size,FILL_TRANSPARENT); // draw foods on lcd |
Ting12138 | 0:ba32cfe0051e | 23 | } |
Ting12138 | 0:ba32cfe0051e | 24 | |
Ting12138 | 0:ba32cfe0051e | 25 | void foods::update() { |
Ting12138 | 0:ba32cfe0051e | 26 | _x -= _velocity; // the foods is moving leftward from the right screen |
Ting12138 | 0:ba32cfe0051e | 27 | } |
Ting12138 | 0:ba32cfe0051e | 28 | |
Ting12138 | 0:ba32cfe0051e | 29 | void foods::replace() { |
Ting12138 | 0:ba32cfe0051e | 30 | _x = 149; // replace to the right side when the stone exits to the left side of the screen |
Ting12138 | 0:ba32cfe0051e | 31 | int uncertainty = rand() % (45 - (_size * 2)); |
Ting12138 | 0:ba32cfe0051e | 32 | _y = 1 + _size + uncertainty; // regenerate the vertical position for the stone |
Ting12138 | 0:ba32cfe0051e | 33 | } |
Ting12138 | 0:ba32cfe0051e | 34 | |
Ting12138 | 0:ba32cfe0051e | 35 | Vector2D foods::get_pos() { // get position for collision logic function |
Ting12138 | 0:ba32cfe0051e | 36 | Vector2D p = {_x,_y}; |
Ting12138 | 0:ba32cfe0051e | 37 | return p; |
Ting12138 | 0:ba32cfe0051e | 38 | } |