Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameObjects/SnakeFood/SnakeFood.cpp@95:b068b0735f45, 2019-05-09 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Thu May 09 08:35:02 2019 +0000
- Revision:
- 95:b068b0735f45
- Parent:
- 83:329da564799a
- Child:
- 97:819c0689efa7
documented the blocks class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AhmedPlaymaker | 9:d1d79d4ee673 | 1 | #include "SnakeFood.h" |
AhmedPlaymaker | 9:d1d79d4ee673 | 2 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 3 | SnakeFood::SnakeFood() |
AhmedPlaymaker | 9:d1d79d4ee673 | 4 | { |
AhmedPlaymaker | 9:d1d79d4ee673 | 5 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 6 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 7 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 8 | SnakeFood::~SnakeFood() |
AhmedPlaymaker | 9:d1d79d4ee673 | 9 | { |
AhmedPlaymaker | 9:d1d79d4ee673 | 10 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 11 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 12 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 13 | //SnakeFood Sprite. |
AhmedPlaymaker | 9:d1d79d4ee673 | 14 | int food_sprite[3][3] = { |
AhmedPlaymaker | 9:d1d79d4ee673 | 15 | {0,1,0}, |
AhmedPlaymaker | 9:d1d79d4ee673 | 16 | {1,0,1}, |
AhmedPlaymaker | 9:d1d79d4ee673 | 17 | {0,1,0}, |
AhmedPlaymaker | 63:205f0ca48473 | 18 | }; |
AhmedPlaymaker | 63:205f0ca48473 | 19 | |
AhmedPlaymaker | 83:329da564799a | 20 | void SnakeFood::init(N5110 *lcd) //Delete Int |
AhmedPlaymaker | 9:d1d79d4ee673 | 21 | { |
AhmedPlaymaker | 83:329da564799a | 22 | _lcd = lcd; |
AhmedPlaymaker | 81:4c1641e10dcd | 23 | reset = 0; |
AhmedPlaymaker | 95:b068b0735f45 | 24 | _fx = -100; //to make sure these coordinates do not interfere with any collisions as this is a default value if conditions do not satisfy, |
AhmedPlaymaker | 95:b068b0735f45 | 25 | _fy = -100; //(NULL is in range of collisions in this game and is therefore not used. |
AhmedPlaymaker | 9:d1d79d4ee673 | 26 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 27 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 28 | |
AhmedPlaymaker | 83:329da564799a | 29 | void SnakeFood::draw() |
AhmedPlaymaker | 63:205f0ca48473 | 30 | { |
AhmedPlaymaker | 38:30e4e6191762 | 31 | velocity.x = 0; |
AhmedPlaymaker | 38:30e4e6191762 | 32 | velocity.y = 1; |
AhmedPlaymaker | 9:d1d79d4ee673 | 33 | //add sideways velocity incase I want to make it dodgy. |
AhmedPlaymaker | 63:205f0ca48473 | 34 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 35 | //Resets SnakeFood position if it reaches the bottom of the screen. |
AhmedPlaymaker | 81:4c1641e10dcd | 36 | if(reset == 0) { |
AhmedPlaymaker | 36:dfdd619874ae | 37 | _fx = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis. |
AhmedPlaymaker | 36:dfdd619874ae | 38 | _fy = -3; |
AhmedPlaymaker | 81:4c1641e10dcd | 39 | reset = reset+1; |
AhmedPlaymaker | 36:dfdd619874ae | 40 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 41 | |
AhmedPlaymaker | 83:329da564799a | 42 | _lcd->drawSprite(_fx,_fy,3,3,(int *)food_sprite); //Function to draw the food. |
AhmedPlaymaker | 9:d1d79d4ee673 | 43 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 44 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 45 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 46 | Vector2D SnakeFood::get_pos() //Obtains the X and Y coordinate of the target. |
AhmedPlaymaker | 9:d1d79d4ee673 | 47 | { |
AhmedPlaymaker | 9:d1d79d4ee673 | 48 | Vector2D snakefoodpos = {_fx,_fy}; |
AhmedPlaymaker | 81:4c1641e10dcd | 49 | //printf("snakefoodpos from food = %f %f \reset", snakefoodpos.x, snakefoodpos.y); |
AhmedPlaymaker | 9:d1d79d4ee673 | 50 | return snakefoodpos; |
AhmedPlaymaker | 9:d1d79d4ee673 | 51 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 52 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 53 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 54 | void SnakeFood::update() |
AhmedPlaymaker | 9:d1d79d4ee673 | 55 | { |
AhmedPlaymaker | 77:5c6bd659c32d | 56 | //uses draw() to reset snake position after snake has gone past the screen. |
AhmedPlaymaker | 77:5c6bd659c32d | 57 | if(_fy >= 48) { |
AhmedPlaymaker | 81:4c1641e10dcd | 58 | reset = 0; |
AhmedPlaymaker | 77:5c6bd659c32d | 59 | } |
AhmedPlaymaker | 38:30e4e6191762 | 60 | _fx += velocity.x; |
AhmedPlaymaker | 38:30e4e6191762 | 61 | _fy += velocity.y; |
AhmedPlaymaker | 9:d1d79d4ee673 | 62 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 63 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 64 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 65 | void SnakeFood::set_pos(Vector2D p) |
AhmedPlaymaker | 9:d1d79d4ee673 | 66 | { |
AhmedPlaymaker | 9:d1d79d4ee673 | 67 | _fx = p.x; |
AhmedPlaymaker | 9:d1d79d4ee673 | 68 | _fy = p.y; |
AhmedPlaymaker | 9:d1d79d4ee673 | 69 | } |