Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameObjects/SnakeFood/SnakeFood.cpp@56:142e9fdb77a8, 2019-04-28 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Sun Apr 28 16:56:08 2019 +0000
- Revision:
- 56:142e9fdb77a8
- Parent:
- 51:387249f9b333
- Child:
- 63:205f0ca48473
Removed some old feature that seems to never make a difference and also added some logos for joystick and motion control, along with changing some code style
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 | 9:d1d79d4ee673 | 18 | }; |
AhmedPlaymaker | 9:d1d79d4ee673 | 19 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 20 | void SnakeFood::init() //Delete Int |
AhmedPlaymaker | 9:d1d79d4ee673 | 21 | { |
AhmedPlaymaker | 30:461231877c89 | 22 | n = 0; |
AhmedPlaymaker | 9:d1d79d4ee673 | 23 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 24 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 25 | |
AhmedPlaymaker | 56:142e9fdb77a8 | 26 | void SnakeFood::draw(N5110 &lcd) |
AhmedPlaymaker | 9:d1d79d4ee673 | 27 | { |
AhmedPlaymaker | 9:d1d79d4ee673 | 28 | |
AhmedPlaymaker | 38:30e4e6191762 | 29 | velocity.x = 0; |
AhmedPlaymaker | 38:30e4e6191762 | 30 | velocity.y = 1; |
AhmedPlaymaker | 9:d1d79d4ee673 | 31 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 32 | //add sideways velocity incase I want to make it dodgy. |
AhmedPlaymaker | 9:d1d79d4ee673 | 33 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 34 | //Resets SnakeFood position if it reaches the bottom of the screen. |
AhmedPlaymaker | 9:d1d79d4ee673 | 35 | //Also prevents SnakeFood from going out of the side of the screen. |
AhmedPlaymaker | 36:dfdd619874ae | 36 | if(_fx < 0) {_fx = 0;} |
AhmedPlaymaker | 36:dfdd619874ae | 37 | if(_fx > 81) {_fx = 81;} |
AhmedPlaymaker | 36:dfdd619874ae | 38 | if(_fy >= 48) {n = 0;} |
AhmedPlaymaker | 36:dfdd619874ae | 39 | if(n == 0){ |
AhmedPlaymaker | 36:dfdd619874ae | 40 | _fx = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis. |
AhmedPlaymaker | 36:dfdd619874ae | 41 | _fy = -3; |
AhmedPlaymaker | 56:142e9fdb77a8 | 42 | n = n+1; |
AhmedPlaymaker | 36:dfdd619874ae | 43 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 44 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 45 | lcd.drawSprite(_fx,_fy,3,3,(int *)food_sprite); //Function to draw the sprite. |
AhmedPlaymaker | 9:d1d79d4ee673 | 46 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 47 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 48 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 49 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 50 | Vector2D SnakeFood::get_pos() //Obtains the X and Y coordinate of the target. |
AhmedPlaymaker | 9:d1d79d4ee673 | 51 | { |
AhmedPlaymaker | 9:d1d79d4ee673 | 52 | Vector2D snakefoodpos = {_fx,_fy}; |
AhmedPlaymaker | 9:d1d79d4ee673 | 53 | //printf("snakefoodpos from food = %f %f \n", snakefoodpos.x, snakefoodpos.y); |
AhmedPlaymaker | 9:d1d79d4ee673 | 54 | return snakefoodpos; |
AhmedPlaymaker | 9:d1d79d4ee673 | 55 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 56 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 57 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 58 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 59 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 60 | void SnakeFood::update() |
AhmedPlaymaker | 9:d1d79d4ee673 | 61 | { |
AhmedPlaymaker | 38:30e4e6191762 | 62 | _fx += velocity.x; |
AhmedPlaymaker | 38:30e4e6191762 | 63 | _fy += velocity.y; |
AhmedPlaymaker | 9:d1d79d4ee673 | 64 | } |
AhmedPlaymaker | 9:d1d79d4ee673 | 65 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 66 | |
AhmedPlaymaker | 9:d1d79d4ee673 | 67 | void SnakeFood::set_pos(Vector2D p) |
AhmedPlaymaker | 9:d1d79d4ee673 | 68 | { |
AhmedPlaymaker | 9:d1d79d4ee673 | 69 | _fx = p.x; |
AhmedPlaymaker | 9:d1d79d4ee673 | 70 | _fy = p.y; |
AhmedPlaymaker | 9:d1d79d4ee673 | 71 | } |