Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Mon May 06 09:05:09 2019 +0000
Revision:
81:4c1641e10dcd
Parent:
77:5c6bd659c32d
Child:
83:329da564799a
Made Some Barriers, not sure about them tough

Who changed what in which revision?

UserRevisionLine numberNew 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 9:d1d79d4ee673 20 void SnakeFood::init() //Delete Int
AhmedPlaymaker 9:d1d79d4ee673 21 {
AhmedPlaymaker 81:4c1641e10dcd 22 reset = 0;
AhmedPlaymaker 9:d1d79d4ee673 23 }
AhmedPlaymaker 9:d1d79d4ee673 24
AhmedPlaymaker 9:d1d79d4ee673 25
AhmedPlaymaker 56:142e9fdb77a8 26 void SnakeFood::draw(N5110 &lcd)
AhmedPlaymaker 63:205f0ca48473 27 {
AhmedPlaymaker 38:30e4e6191762 28 velocity.x = 0;
AhmedPlaymaker 38:30e4e6191762 29 velocity.y = 1;
AhmedPlaymaker 9:d1d79d4ee673 30 //add sideways velocity incase I want to make it dodgy.
AhmedPlaymaker 63:205f0ca48473 31
AhmedPlaymaker 9:d1d79d4ee673 32 //Resets SnakeFood position if it reaches the bottom of the screen.
AhmedPlaymaker 81:4c1641e10dcd 33 if(reset == 0) {
AhmedPlaymaker 36:dfdd619874ae 34 _fx = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis.
AhmedPlaymaker 36:dfdd619874ae 35 _fy = -3;
AhmedPlaymaker 81:4c1641e10dcd 36 reset = reset+1;
AhmedPlaymaker 36:dfdd619874ae 37 }
AhmedPlaymaker 9:d1d79d4ee673 38
AhmedPlaymaker 63:205f0ca48473 39 lcd.drawSprite(_fx,_fy,3,3,(int *)food_sprite); //Function to draw the food.
AhmedPlaymaker 9:d1d79d4ee673 40 }
AhmedPlaymaker 9:d1d79d4ee673 41
AhmedPlaymaker 9:d1d79d4ee673 42
AhmedPlaymaker 9:d1d79d4ee673 43 Vector2D SnakeFood::get_pos() //Obtains the X and Y coordinate of the target.
AhmedPlaymaker 9:d1d79d4ee673 44 {
AhmedPlaymaker 9:d1d79d4ee673 45 Vector2D snakefoodpos = {_fx,_fy};
AhmedPlaymaker 81:4c1641e10dcd 46 //printf("snakefoodpos from food = %f %f \reset", snakefoodpos.x, snakefoodpos.y);
AhmedPlaymaker 9:d1d79d4ee673 47 return snakefoodpos;
AhmedPlaymaker 9:d1d79d4ee673 48 }
AhmedPlaymaker 9:d1d79d4ee673 49
AhmedPlaymaker 9:d1d79d4ee673 50
AhmedPlaymaker 9:d1d79d4ee673 51 void SnakeFood::update()
AhmedPlaymaker 9:d1d79d4ee673 52 {
AhmedPlaymaker 77:5c6bd659c32d 53 //uses draw() to reset snake position after snake has gone past the screen.
AhmedPlaymaker 77:5c6bd659c32d 54 if(_fy >= 48) {
AhmedPlaymaker 81:4c1641e10dcd 55 reset = 0;
AhmedPlaymaker 77:5c6bd659c32d 56 }
AhmedPlaymaker 38:30e4e6191762 57 _fx += velocity.x;
AhmedPlaymaker 38:30e4e6191762 58 _fy += velocity.y;
AhmedPlaymaker 9:d1d79d4ee673 59 }
AhmedPlaymaker 9:d1d79d4ee673 60
AhmedPlaymaker 9:d1d79d4ee673 61
AhmedPlaymaker 9:d1d79d4ee673 62 void SnakeFood::set_pos(Vector2D p)
AhmedPlaymaker 9:d1d79d4ee673 63 {
AhmedPlaymaker 9:d1d79d4ee673 64 _fx = p.x;
AhmedPlaymaker 9:d1d79d4ee673 65 _fy = p.y;
AhmedPlaymaker 9:d1d79d4ee673 66 }