class for obstacles in Car_race game

Committer:
fy14aaz
Date:
Wed Apr 19 20:55:18 2017 +0000
Revision:
8:c8007ccac5c0
Parent:
7:55d1306f5df5
Child:
9:d5f90988545e
added the score feature that controls the ability to shooting the obstacles; need;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fy14aaz 0:f8968ec0ec1b 1 #include "Obstacles.h"
fy14aaz 0:f8968ec0ec1b 2
fy14aaz 0:f8968ec0ec1b 3 Obstacles::Obstacles()
fy14aaz 0:f8968ec0ec1b 4 {
fy14aaz 0:f8968ec0ec1b 5
fy14aaz 0:f8968ec0ec1b 6 }
fy14aaz 0:f8968ec0ec1b 7
fy14aaz 0:f8968ec0ec1b 8 Obstacles::~Obstacles()
fy14aaz 0:f8968ec0ec1b 9 {
fy14aaz 0:f8968ec0ec1b 10
fy14aaz 0:f8968ec0ec1b 11 }
fy14aaz 0:f8968ec0ec1b 12
fy14aaz 0:f8968ec0ec1b 13 void Obstacles::init(int seed)
fy14aaz 0:f8968ec0ec1b 14 {
fy14aaz 0:f8968ec0ec1b 15 _Obstacle_x = seed;
fy14aaz 8:c8007ccac5c0 16 _score = 0;
fy14aaz 0:f8968ec0ec1b 17 // try to draw them here in this function as we need the draw function for another purpose
fy14aaz 0:f8968ec0ec1b 18 // are we done though? we might need to split the draw function into two, one for drawing the car
fy14aaz 0:f8968ec0ec1b 19 // and the other for drawing the obstacles as they are drawn in a time basis
fy14aaz 0:f8968ec0ec1b 20 }
fy14aaz 0:f8968ec0ec1b 21
fy14aaz 1:a735e7396af4 22 void Obstacles::draw(N5110 &lcd,int seed)
fy14aaz 0:f8968ec0ec1b 23 {
fy14aaz 1:a735e7396af4 24 _Obstacle_x = seed;
fy14aaz 0:f8968ec0ec1b 25 switch(_Obstacle_x){
fy14aaz 0:f8968ec0ec1b 26
fy14aaz 0:f8968ec0ec1b 27 case 0:
fy14aaz 5:b000cefeb3c5 28 lcd.drawRect(2,2,25,2,FILL_BLACK);
fy14aaz 0:f8968ec0ec1b 29 break;
fy14aaz 0:f8968ec0ec1b 30
fy14aaz 0:f8968ec0ec1b 31 case 1:
fy14aaz 5:b000cefeb3c5 32 lcd.drawRect(28,2,27,2,FILL_BLACK);
fy14aaz 0:f8968ec0ec1b 33 break;
fy14aaz 0:f8968ec0ec1b 34
fy14aaz 0:f8968ec0ec1b 35 case 2:
fy14aaz 5:b000cefeb3c5 36 lcd.drawRect(56,2,25,2,FILL_BLACK);
fy14aaz 0:f8968ec0ec1b 37 break;
fy14aaz 0:f8968ec0ec1b 38 }
fy14aaz 0:f8968ec0ec1b 39 }
fy14aaz 0:f8968ec0ec1b 40
fy14aaz 1:a735e7396af4 41 void Obstacles::update(N5110 &lcd) // adjust this function to be able to store all pixels values
fy14aaz 0:f8968ec0ec1b 42 { // then they are used to shift th screen
fy14aaz 1:a735e7396af4 43 // _Obstacle_x = seed;
fy14aaz 8:c8007ccac5c0 44 addScore(lcd);
fy14aaz 2:4430d4fd91c2 45 char pixelstate[82][46];
fy14aaz 2:4430d4fd91c2 46 for (int i=1; i<83; i+=1) {
fy14aaz 2:4430d4fd91c2 47 for (int j=1; j<47; j+=1) {
fy14aaz 1:a735e7396af4 48 if (lcd.getPixel(i,j)) {
fy14aaz 2:4430d4fd91c2 49 pixelstate[i-1][j-1]=1;
fy14aaz 1:a735e7396af4 50 }
fy14aaz 1:a735e7396af4 51 else {
fy14aaz 2:4430d4fd91c2 52 pixelstate[i-1][j-1]=0;
fy14aaz 2:4430d4fd91c2 53 }
fy14aaz 1:a735e7396af4 54 }
fy14aaz 1:a735e7396af4 55 }
fy14aaz 2:4430d4fd91c2 56 for (int i=1; i<83; i+=1) {
fy14aaz 2:4430d4fd91c2 57 for (int j=1; j<47; j+=1) {
fy14aaz 2:4430d4fd91c2 58 if ((pixelstate[i-1][j-1]) ) {
fy14aaz 7:55d1306f5df5 59 lcd.setPixel(i,j+2);
fy14aaz 4:b1648ba28d3f 60 } // CAN INCREASE THE SPEED BY ADDING 1 FOR Y VALUES
fy14aaz 2:4430d4fd91c2 61 if ((pixelstate[i-1][j-2]==0) ) {
fy14aaz 7:55d1306f5df5 62 lcd.setPixel(i,j+1,false);
fy14aaz 2:4430d4fd91c2 63 }
fy14aaz 1:a735e7396af4 64 }
fy14aaz 2:4430d4fd91c2 65 }
fy14aaz 8:c8007ccac5c0 66 }
fy14aaz 8:c8007ccac5c0 67
fy14aaz 8:c8007ccac5c0 68 void Obstacles::addScore(N5110 &lcd)
fy14aaz 8:c8007ccac5c0 69 {
fy14aaz 8:c8007ccac5c0 70 for (int i=2; i<83; i+=1) {
fy14aaz 8:c8007ccac5c0 71 if (lcd.getPixel(i,46)) {
fy14aaz 8:c8007ccac5c0 72 _score++;
fy14aaz 8:c8007ccac5c0 73 break;
fy14aaz 8:c8007ccac5c0 74 }
fy14aaz 8:c8007ccac5c0 75 }
fy14aaz 8:c8007ccac5c0 76 }
fy14aaz 8:c8007ccac5c0 77
fy14aaz 8:c8007ccac5c0 78 int Obstacles::getScore()
fy14aaz 8:c8007ccac5c0 79 {
fy14aaz 8:c8007ccac5c0 80 return _score;
fy14aaz 8:c8007ccac5c0 81 }
fy14aaz 8:c8007ccac5c0 82
fy14aaz 8:c8007ccac5c0 83 void Obstacles::resetScore()
fy14aaz 8:c8007ccac5c0 84 {
fy14aaz 8:c8007ccac5c0 85 _score = 0;
fy14aaz 0:f8968ec0ec1b 86 }