Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Mon May 06 14:28:35 2019 +0000
Revision:
83:329da564799a
Parent:
82:c51ae8a501d1
Child:
94:4e603bd6c381
Now, I have put gamepad and lcd objects from the main() in a pointer, so that i can declare them in init() in every class i use them in and use them as global objects for each class, without having to send their addresses to all of the functions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AhmedPlaymaker 81:4c1641e10dcd 1 #include "Barriers.h"
AhmedPlaymaker 81:4c1641e10dcd 2
AhmedPlaymaker 81:4c1641e10dcd 3 Barriers::Barriers()
AhmedPlaymaker 81:4c1641e10dcd 4 {
AhmedPlaymaker 81:4c1641e10dcd 5
AhmedPlaymaker 81:4c1641e10dcd 6 }
AhmedPlaymaker 81:4c1641e10dcd 7
AhmedPlaymaker 81:4c1641e10dcd 8 Barriers::~Barriers()
AhmedPlaymaker 81:4c1641e10dcd 9 {
AhmedPlaymaker 81:4c1641e10dcd 10
AhmedPlaymaker 81:4c1641e10dcd 11 }
AhmedPlaymaker 81:4c1641e10dcd 12
AhmedPlaymaker 81:4c1641e10dcd 13 //Brrier Sprite.
AhmedPlaymaker 81:4c1641e10dcd 14 int Barrier[22][1] = {
AhmedPlaymaker 81:4c1641e10dcd 15 {1},
AhmedPlaymaker 81:4c1641e10dcd 16 {1},
AhmedPlaymaker 81:4c1641e10dcd 17 {1},
AhmedPlaymaker 81:4c1641e10dcd 18 {1},
AhmedPlaymaker 81:4c1641e10dcd 19 {1},
AhmedPlaymaker 81:4c1641e10dcd 20 {1},
AhmedPlaymaker 81:4c1641e10dcd 21 {1},
AhmedPlaymaker 81:4c1641e10dcd 22 {1},
AhmedPlaymaker 81:4c1641e10dcd 23 {1},
AhmedPlaymaker 81:4c1641e10dcd 24 {1},
AhmedPlaymaker 81:4c1641e10dcd 25 {1},
AhmedPlaymaker 81:4c1641e10dcd 26 {1},
AhmedPlaymaker 81:4c1641e10dcd 27 {1},
AhmedPlaymaker 81:4c1641e10dcd 28 {1},
AhmedPlaymaker 81:4c1641e10dcd 29 {1},
AhmedPlaymaker 81:4c1641e10dcd 30 {1},
AhmedPlaymaker 81:4c1641e10dcd 31 {1},
AhmedPlaymaker 81:4c1641e10dcd 32 {1},
AhmedPlaymaker 81:4c1641e10dcd 33 {1},
AhmedPlaymaker 81:4c1641e10dcd 34 {1},
AhmedPlaymaker 81:4c1641e10dcd 35 {1},
AhmedPlaymaker 81:4c1641e10dcd 36 {1},
AhmedPlaymaker 82:c51ae8a501d1 37 };
AhmedPlaymaker 83:329da564799a 38 void Barriers::init(N5110 *lcd)
AhmedPlaymaker 81:4c1641e10dcd 39 {
AhmedPlaymaker 83:329da564799a 40 _lcd = lcd;
AhmedPlaymaker 81:4c1641e10dcd 41 reset = 0;
AhmedPlaymaker 83:329da564799a 42 _barx = NULL;
AhmedPlaymaker 83:329da564799a 43 _bary = NULL;
AhmedPlaymaker 81:4c1641e10dcd 44 }
AhmedPlaymaker 81:4c1641e10dcd 45
AhmedPlaymaker 81:4c1641e10dcd 46
AhmedPlaymaker 83:329da564799a 47 void Barriers::draw(int b_y)
AhmedPlaymaker 81:4c1641e10dcd 48 {
AhmedPlaymaker 81:4c1641e10dcd 49 velocity.x = 0;
AhmedPlaymaker 81:4c1641e10dcd 50 velocity.y = 1;
AhmedPlaymaker 82:c51ae8a501d1 51
AhmedPlaymaker 82:c51ae8a501d1 52 if((reset == 0)&&(b_y < (_blockgap-14))) {
AhmedPlaymaker 82:c51ae8a501d1 53 switch(rand()%4) {
AhmedPlaymaker 82:c51ae8a501d1 54 case 0:
AhmedPlaymaker 82:c51ae8a501d1 55 _barx = 19;
AhmedPlaymaker 82:c51ae8a501d1 56 break;
AhmedPlaymaker 82:c51ae8a501d1 57 case 1:
AhmedPlaymaker 82:c51ae8a501d1 58 _barx = 35;
AhmedPlaymaker 82:c51ae8a501d1 59 break;
AhmedPlaymaker 82:c51ae8a501d1 60 case 2:
AhmedPlaymaker 82:c51ae8a501d1 61 _barx = 51;
AhmedPlaymaker 82:c51ae8a501d1 62 break;
AhmedPlaymaker 82:c51ae8a501d1 63 case 3:
AhmedPlaymaker 82:c51ae8a501d1 64 _barx = 67;
AhmedPlaymaker 82:c51ae8a501d1 65 break;
AhmedPlaymaker 82:c51ae8a501d1 66 }
AhmedPlaymaker 81:4c1641e10dcd 67 _bary = -22;
AhmedPlaymaker 81:4c1641e10dcd 68 reset = reset+1; //to stop this if function to keep executing.
AhmedPlaymaker 81:4c1641e10dcd 69 }
AhmedPlaymaker 83:329da564799a 70 _lcd->drawSprite(_barx,_bary,22,1,(int *)Barrier); //Function to draw the frame at all i coordinates as x.
AhmedPlaymaker 82:c51ae8a501d1 71
AhmedPlaymaker 81:4c1641e10dcd 72 }
AhmedPlaymaker 81:4c1641e10dcd 73
AhmedPlaymaker 81:4c1641e10dcd 74 Vector2D Barriers::get_pos() //Obtains the X and Y coordinate of the target.
AhmedPlaymaker 81:4c1641e10dcd 75 {
AhmedPlaymaker 81:4c1641e10dcd 76 Vector2D barrierpos = {_barx,_bary};
AhmedPlaymaker 81:4c1641e10dcd 77 //printf("barrierpos is = %f %f \n", barrierpos.x, barrierpos.y);
AhmedPlaymaker 81:4c1641e10dcd 78 return barrierpos;
AhmedPlaymaker 81:4c1641e10dcd 79 }
AhmedPlaymaker 81:4c1641e10dcd 80
AhmedPlaymaker 81:4c1641e10dcd 81 void Barriers::update(int blockgap)
AhmedPlaymaker 81:4c1641e10dcd 82 {
AhmedPlaymaker 81:4c1641e10dcd 83 _blockgap = blockgap;
AhmedPlaymaker 82:c51ae8a501d1 84 _barriergap = _blockgap/4;
AhmedPlaymaker 82:c51ae8a501d1 85 if (_blockgap <= 60) {
AhmedPlaymaker 82:c51ae8a501d1 86 _barriergap = 60;
AhmedPlaymaker 82:c51ae8a501d1 87 }
AhmedPlaymaker 81:4c1641e10dcd 88 // this if function makes sure the block appears at the rate of blockgap.
AhmedPlaymaker 82:c51ae8a501d1 89 if(_bary >= _barriergap) {
AhmedPlaymaker 81:4c1641e10dcd 90 reset = 0;
AhmedPlaymaker 81:4c1641e10dcd 91 }
AhmedPlaymaker 81:4c1641e10dcd 92 _barx += velocity.x;
AhmedPlaymaker 81:4c1641e10dcd 93 _bary += velocity.y;
AhmedPlaymaker 81:4c1641e10dcd 94
AhmedPlaymaker 81:4c1641e10dcd 95 }
AhmedPlaymaker 81:4c1641e10dcd 96
AhmedPlaymaker 81:4c1641e10dcd 97
AhmedPlaymaker 81:4c1641e10dcd 98 void Barriers::set_pos(Vector2D p)
AhmedPlaymaker 81:4c1641e10dcd 99 {
AhmedPlaymaker 81:4c1641e10dcd 100 _barx = p.x;
AhmedPlaymaker 81:4c1641e10dcd 101 _bary = p.y;
AhmedPlaymaker 81:4c1641e10dcd 102 }