testing documentation
Dependencies: mbed ll16j23s_test_docs
Food/Food.cpp@10:a2d643b3c782, 2020-05-26 (annotated)
- Committer:
- JoeShotton
- Date:
- Tue May 26 23:25:09 2020 +0000
- Revision:
- 10:a2d643b3c782
- Parent:
- 9:0571880085cc
Removed several bugs, started on thorough documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JoeShotton | 3:fcd6d70e9694 | 1 | #include "Food.h" |
JoeShotton | 3:fcd6d70e9694 | 2 | |
JoeShotton | 3:fcd6d70e9694 | 3 | Food::Food() |
JoeShotton | 3:fcd6d70e9694 | 4 | { |
JoeShotton | 3:fcd6d70e9694 | 5 | //constructor |
JoeShotton | 5:06fa7674622a | 6 | _x = 22; // starts off-screen |
JoeShotton | 4:ea3fa51c4386 | 7 | _y = 30; |
JoeShotton | 4:ea3fa51c4386 | 8 | _frame = 0; |
JoeShotton | 3:fcd6d70e9694 | 9 | } |
JoeShotton | 3:fcd6d70e9694 | 10 | |
JoeShotton | 3:fcd6d70e9694 | 11 | Food::~Food() |
JoeShotton | 3:fcd6d70e9694 | 12 | { |
JoeShotton | 3:fcd6d70e9694 | 13 | //deconstructor |
JoeShotton | 3:fcd6d70e9694 | 14 | } |
JoeShotton | 3:fcd6d70e9694 | 15 | |
JoeShotton | 10:a2d643b3c782 | 16 | void Food::init(Gamepad &pad, N5110 &lcd, FXOS8700CQ &mag) { |
JoeShotton | 7:dd84e0fab346 | 17 | |
JoeShotton | 5:06fa7674622a | 18 | mag.init(); |
JoeShotton | 5:06fa7674622a | 19 | Data _values = mag.get_values(); |
JoeShotton | 10:a2d643b3c782 | 20 | _seed = 1000000*(_values.mx + _values.my + _values.mz); //seed generated from combination of accelerometre values so any can change for seed to change |
JoeShotton | 5:06fa7674622a | 21 | srand(_seed); |
JoeShotton | 10:a2d643b3c782 | 22 | while (rand_pos(pad, lcd) == false){ //while new food coordinates are in the snake/walls |
JoeShotton | 10:a2d643b3c782 | 23 | rand_pos(pad, lcd); //choose new coords by rerunning the function |
JoeShotton | 10:a2d643b3c782 | 24 | printf("Reselected food position\n"); |
JoeShotton | 10:a2d643b3c782 | 25 | } |
JoeShotton | 4:ea3fa51c4386 | 26 | } |
JoeShotton | 4:ea3fa51c4386 | 27 | |
JoeShotton | 4:ea3fa51c4386 | 28 | |
JoeShotton | 8:bcc3403d7e79 | 29 | bool Food::rand_pos(Gamepad &pad, N5110 &lcd) { |
JoeShotton | 8:bcc3403d7e79 | 30 | wait_ms(10); |
JoeShotton | 8:bcc3403d7e79 | 31 | pad.led(3,0.1); |
JoeShotton | 4:ea3fa51c4386 | 32 | _x = 2 * (rand() % 42); //selects random x cell |
JoeShotton | 4:ea3fa51c4386 | 33 | //printf("Food x: %d\n", _x); |
JoeShotton | 4:ea3fa51c4386 | 34 | _y = 2 * (rand() % 24); //selects random y call |
JoeShotton | 4:ea3fa51c4386 | 35 | //printf("Food y: %d\n", _y); |
JoeShotton | 10:a2d643b3c782 | 36 | if (lcd.getPixel(_x,_y)) { //checks if selected pixel is already black (ie body or walls at this location) |
JoeShotton | 8:bcc3403d7e79 | 37 | return false; |
JoeShotton | 8:bcc3403d7e79 | 38 | } else { |
JoeShotton | 8:bcc3403d7e79 | 39 | return true; |
JoeShotton | 8:bcc3403d7e79 | 40 | } |
JoeShotton | 3:fcd6d70e9694 | 41 | } |
JoeShotton | 3:fcd6d70e9694 | 42 | |
JoeShotton | 4:ea3fa51c4386 | 43 | void Food::draw(N5110 &lcd, int &_frame) { |
JoeShotton | 3:fcd6d70e9694 | 44 | |
JoeShotton | 4:ea3fa51c4386 | 45 | // draw food, with alternating pixels depending on frame. |
JoeShotton | 4:ea3fa51c4386 | 46 | _frame++; |
JoeShotton | 9:0571880085cc | 47 | _frame = _frame % 12; |
JoeShotton | 4:ea3fa51c4386 | 48 | if (_frame > 6) { |
JoeShotton | 4:ea3fa51c4386 | 49 | lcd.drawLine(_x, _y, _x + 1,_y + 1,1); |
JoeShotton | 4:ea3fa51c4386 | 50 | } else { |
JoeShotton | 4:ea3fa51c4386 | 51 | lcd.drawLine(_x + 1, _y, _x,_y + 1,1); |
JoeShotton | 4:ea3fa51c4386 | 52 | } |
JoeShotton | 3:fcd6d70e9694 | 53 | } |
JoeShotton | 3:fcd6d70e9694 | 54 | |
JoeShotton | 9:0571880085cc | 55 | void Food::run(N5110 &lcd) |
JoeShotton | 3:fcd6d70e9694 | 56 | { |
JoeShotton | 4:ea3fa51c4386 | 57 | draw(lcd, _frame); |
JoeShotton | 3:fcd6d70e9694 | 58 | } |
JoeShotton | 4:ea3fa51c4386 | 59 |