Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Fri Apr 26 18:30:25 2019 +0000
Revision:
51:387249f9b333
Parent:
SnakeFood/SnakeFood.cpp@41:4edac50f010d
Child:
56:142e9fdb77a8
Sorted my game classes into folders to make it more organized

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 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 36:dfdd619874ae 26 void SnakeFood::draw(N5110 &lcd, int blockbuff)
AhmedPlaymaker 9:d1d79d4ee673 27 {
AhmedPlaymaker 9:d1d79d4ee673 28
AhmedPlaymaker 38:30e4e6191762 29 velocity.x = 0;
AhmedPlaymaker 38:30e4e6191762 30 velocity.y = 1;
AhmedPlaymaker 38:30e4e6191762 31 _blockbuff = blockbuff;
AhmedPlaymaker 9:d1d79d4ee673 32
AhmedPlaymaker 9:d1d79d4ee673 33 //add sideways velocity incase I want to make it dodgy.
AhmedPlaymaker 9:d1d79d4ee673 34
AhmedPlaymaker 9:d1d79d4ee673 35 //Resets SnakeFood position if it reaches the bottom of the screen.
AhmedPlaymaker 9:d1d79d4ee673 36 //Also prevents SnakeFood from going out of the side of the screen.
AhmedPlaymaker 36:dfdd619874ae 37 if(_fx < 0) {_fx = 0;}
AhmedPlaymaker 36:dfdd619874ae 38 if(_fx > 81) {_fx = 81;}
AhmedPlaymaker 36:dfdd619874ae 39 if(_fy >= 48) {n = 0;}
AhmedPlaymaker 36:dfdd619874ae 40 if(n == 0){
AhmedPlaymaker 36:dfdd619874ae 41 _fx = (rand() % 82); //this makes the food pop up at a random, unspecified location in the x axis.
AhmedPlaymaker 36:dfdd619874ae 42 _fy = -3;
AhmedPlaymaker 38:30e4e6191762 43 if(!((_blockbuff <= 11)&&(_blockbuff >= -11))) { //this makes sure that the snake food only appears when the block doesn't cover it.
AhmedPlaymaker 36:dfdd619874ae 44 n = n+1;
AhmedPlaymaker 9:d1d79d4ee673 45 }
AhmedPlaymaker 36:dfdd619874ae 46 }
AhmedPlaymaker 9:d1d79d4ee673 47
AhmedPlaymaker 9:d1d79d4ee673 48 lcd.drawSprite(_fx,_fy,3,3,(int *)food_sprite); //Function to draw the sprite.
AhmedPlaymaker 9:d1d79d4ee673 49 }
AhmedPlaymaker 9:d1d79d4ee673 50
AhmedPlaymaker 9:d1d79d4ee673 51
AhmedPlaymaker 9:d1d79d4ee673 52
AhmedPlaymaker 9:d1d79d4ee673 53 Vector2D SnakeFood::get_pos() //Obtains the X and Y coordinate of the target.
AhmedPlaymaker 9:d1d79d4ee673 54 {
AhmedPlaymaker 9:d1d79d4ee673 55 Vector2D snakefoodpos = {_fx,_fy};
AhmedPlaymaker 9:d1d79d4ee673 56 //printf("snakefoodpos from food = %f %f \n", snakefoodpos.x, snakefoodpos.y);
AhmedPlaymaker 9:d1d79d4ee673 57 return snakefoodpos;
AhmedPlaymaker 9:d1d79d4ee673 58 }
AhmedPlaymaker 9:d1d79d4ee673 59
AhmedPlaymaker 9:d1d79d4ee673 60
AhmedPlaymaker 9:d1d79d4ee673 61
AhmedPlaymaker 9:d1d79d4ee673 62
AhmedPlaymaker 9:d1d79d4ee673 63 void SnakeFood::update()
AhmedPlaymaker 9:d1d79d4ee673 64 {
AhmedPlaymaker 38:30e4e6191762 65 _fx += velocity.x;
AhmedPlaymaker 38:30e4e6191762 66 _fy += velocity.y;
AhmedPlaymaker 9:d1d79d4ee673 67 }
AhmedPlaymaker 9:d1d79d4ee673 68
AhmedPlaymaker 9:d1d79d4ee673 69
AhmedPlaymaker 9:d1d79d4ee673 70 void SnakeFood::set_pos(Vector2D p)
AhmedPlaymaker 9:d1d79d4ee673 71 {
AhmedPlaymaker 9:d1d79d4ee673 72 _fx = p.x;
AhmedPlaymaker 9:d1d79d4ee673 73 _fy = p.y;
AhmedPlaymaker 38:30e4e6191762 74 if(((_blockbuff <= 11)&&(_blockbuff >= -11))&&_fy == -3) { //this makes sure that the snake food only appears when the block doesn't cover it.
AhmedPlaymaker 38:30e4e6191762 75 n = 0;
AhmedPlaymaker 38:30e4e6191762 76 }
AhmedPlaymaker 9:d1d79d4ee673 77 }