Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Wed May 01 22:35:40 2019 +0000
Revision:
63:205f0ca48473
Parent:
56:142e9fdb77a8
Child:
77:5c6bd659c32d
Sorted out some functions, specifically related to their functionality

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 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 63:205f0ca48473 27 {
AhmedPlaymaker 9:d1d79d4ee673 28
AhmedPlaymaker 38:30e4e6191762 29 velocity.x = 0;
AhmedPlaymaker 38:30e4e6191762 30 velocity.y = 1;
AhmedPlaymaker 63:205f0ca48473 31
AhmedPlaymaker 9:d1d79d4ee673 32 //add sideways velocity incase I want to make it dodgy.
AhmedPlaymaker 63:205f0ca48473 33
AhmedPlaymaker 9:d1d79d4ee673 34 //Resets SnakeFood position if it reaches the bottom of the screen.
AhmedPlaymaker 63:205f0ca48473 35
AhmedPlaymaker 63:205f0ca48473 36 if(_fy >= 48) {
AhmedPlaymaker 63:205f0ca48473 37 n = 0;
AhmedPlaymaker 63:205f0ca48473 38 }
AhmedPlaymaker 63:205f0ca48473 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 63:205f0ca48473 42 n = n+1;
AhmedPlaymaker 36:dfdd619874ae 43 }
AhmedPlaymaker 9:d1d79d4ee673 44
AhmedPlaymaker 63:205f0ca48473 45 lcd.drawSprite(_fx,_fy,3,3,(int *)food_sprite); //Function to draw the food.
AhmedPlaymaker 9:d1d79d4ee673 46 }
AhmedPlaymaker 9:d1d79d4ee673 47
AhmedPlaymaker 9:d1d79d4ee673 48
AhmedPlaymaker 9:d1d79d4ee673 49 Vector2D SnakeFood::get_pos() //Obtains the X and Y coordinate of the target.
AhmedPlaymaker 9:d1d79d4ee673 50 {
AhmedPlaymaker 9:d1d79d4ee673 51 Vector2D snakefoodpos = {_fx,_fy};
AhmedPlaymaker 9:d1d79d4ee673 52 //printf("snakefoodpos from food = %f %f \n", snakefoodpos.x, snakefoodpos.y);
AhmedPlaymaker 9:d1d79d4ee673 53 return snakefoodpos;
AhmedPlaymaker 9:d1d79d4ee673 54 }
AhmedPlaymaker 9:d1d79d4ee673 55
AhmedPlaymaker 9:d1d79d4ee673 56
AhmedPlaymaker 9:d1d79d4ee673 57 void SnakeFood::update()
AhmedPlaymaker 9:d1d79d4ee673 58 {
AhmedPlaymaker 38:30e4e6191762 59 _fx += velocity.x;
AhmedPlaymaker 38:30e4e6191762 60 _fy += velocity.y;
AhmedPlaymaker 9:d1d79d4ee673 61 }
AhmedPlaymaker 9:d1d79d4ee673 62
AhmedPlaymaker 9:d1d79d4ee673 63
AhmedPlaymaker 9:d1d79d4ee673 64 void SnakeFood::set_pos(Vector2D p)
AhmedPlaymaker 9:d1d79d4ee673 65 {
AhmedPlaymaker 9:d1d79d4ee673 66 _fx = p.x;
AhmedPlaymaker 9:d1d79d4ee673 67 _fy = p.y;
AhmedPlaymaker 9:d1d79d4ee673 68 }