class for obstacles in Car_race game
Obstacles.cpp@10:515df7535b2f, 2017-05-02 (annotated)
- Committer:
- fy14aaz
- Date:
- Tue May 02 22:25:19 2017 +0000
- Revision:
- 10:515df7535b2f
- Parent:
- 9:d5f90988545e
- Child:
- 11:c0922d4fda7b
polishing the code, encapsulating the functions into smaller ones and added the instructions part
Who changed what in which revision?
User | Revision | Line number | New 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 | 10:515df7535b2f | 5 | |
fy14aaz | 0:f8968ec0ec1b | 6 | } |
fy14aaz | 0:f8968ec0ec1b | 7 | |
fy14aaz | 0:f8968ec0ec1b | 8 | Obstacles::~Obstacles() |
fy14aaz | 0:f8968ec0ec1b | 9 | { |
fy14aaz | 10:515df7535b2f | 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 | 9:d5f90988545e | 17 | _totalscore = 0; |
fy14aaz | 10:515df7535b2f | 18 | // try to draw them here in this function as we need the draw function for another purpose |
fy14aaz | 0:f8968ec0ec1b | 19 | // are we done though? we might need to split the draw function into two, one for drawing the car |
fy14aaz | 0:f8968ec0ec1b | 20 | // and the other for drawing the obstacles as they are drawn in a time basis |
fy14aaz | 0:f8968ec0ec1b | 21 | } |
fy14aaz | 0:f8968ec0ec1b | 22 | |
fy14aaz | 1:a735e7396af4 | 23 | void Obstacles::draw(N5110 &lcd,int seed) |
fy14aaz | 0:f8968ec0ec1b | 24 | { |
fy14aaz | 1:a735e7396af4 | 25 | _Obstacle_x = seed; |
fy14aaz | 10:515df7535b2f | 26 | |
fy14aaz | 10:515df7535b2f | 27 | switch(_Obstacle_x) { |
fy14aaz | 10:515df7535b2f | 28 | case 0: |
fy14aaz | 10:515df7535b2f | 29 | lcd.drawRect(2,11,25,2,FILL_BLACK); // it was lcd.drawRect(2,2,25,2,FILL_BLACK); |
fy14aaz | 10:515df7535b2f | 30 | break; |
fy14aaz | 10:515df7535b2f | 31 | |
fy14aaz | 10:515df7535b2f | 32 | case 1: |
fy14aaz | 10:515df7535b2f | 33 | lcd.drawRect(28,11,27,2,FILL_BLACK); // it was lcd.drawRect(28,2,27,2,FILL_BLACK); |
fy14aaz | 10:515df7535b2f | 34 | break; |
fy14aaz | 10:515df7535b2f | 35 | |
fy14aaz | 10:515df7535b2f | 36 | case 2: |
fy14aaz | 10:515df7535b2f | 37 | lcd.drawRect(56,11,25,2,FILL_BLACK); // it was lcd.drawRect(56,2,25,2,FILL_BLACK); |
fy14aaz | 10:515df7535b2f | 38 | break; |
fy14aaz | 10:515df7535b2f | 39 | } |
fy14aaz | 0:f8968ec0ec1b | 40 | } |
fy14aaz | 0:f8968ec0ec1b | 41 | |
fy14aaz | 1:a735e7396af4 | 42 | void Obstacles::update(N5110 &lcd) // adjust this function to be able to store all pixels values |
fy14aaz | 10:515df7535b2f | 43 | { |
fy14aaz | 10:515df7535b2f | 44 | // then they are used to shift the screen |
fy14aaz | 10:515df7535b2f | 45 | addScore(lcd); |
fy14aaz | 10:515df7535b2f | 46 | char pixelstate[82][37]; // it was char pixelstate[82][46]; |
fy14aaz | 10:515df7535b2f | 47 | for (int i=1; i<83; i+=1) { // it was for (int i=1; i<83; i+=1) { |
fy14aaz | 10:515df7535b2f | 48 | for (int j=10; j<47; j+=1) { // it was for (int j=1; j<47; j+=1) { |
fy14aaz | 10:515df7535b2f | 49 | if (lcd.getPixel(i,j)) { // if (lcd.getPixel(i,j)) { |
fy14aaz | 10:515df7535b2f | 50 | pixelstate[i-1][j-10]=1; // pixelstate[i-1][j-1]=1; |
fy14aaz | 10:515df7535b2f | 51 | } else { |
fy14aaz | 10:515df7535b2f | 52 | pixelstate[i-1][j-10]=0; // pixelstate[i-1][j-1]=0; |
fy14aaz | 10:515df7535b2f | 53 | } |
fy14aaz | 10:515df7535b2f | 54 | } |
fy14aaz | 10:515df7535b2f | 55 | } |
fy14aaz | 10:515df7535b2f | 56 | for (int i=1; i<83; i+=1) { // it was for (int i=1; i<83; i+=1) { |
fy14aaz | 10:515df7535b2f | 57 | for (int j=10; j<47; j+=1) { // it was for (int j=1; j<47; j+=1) { |
fy14aaz | 10:515df7535b2f | 58 | if ((pixelstate[i-1][j-10]) ) { // it was if ((pixelstate[i-1][j-1]) ) { |
fy14aaz | 10:515df7535b2f | 59 | lcd.setPixel(i,j+2); // lcd.setPixel(i,j+2); |
fy14aaz | 10:515df7535b2f | 60 | } // CAN INCREASE THE SPEED BY ADDING 1 FOR Y VALUES |
fy14aaz | 10:515df7535b2f | 61 | if ((pixelstate[i-1][j-11]==0) ) { // if ((pixelstate[i-1][j-2]==0) ) { |
fy14aaz | 10:515df7535b2f | 62 | lcd.setPixel(i,j+1,false); // lcd.setPixel(i,j+1,false); |
fy14aaz | 10:515df7535b2f | 63 | } |
fy14aaz | 10:515df7535b2f | 64 | } |
fy14aaz | 10:515df7535b2f | 65 | } |
fy14aaz | 8:c8007ccac5c0 | 66 | } |
fy14aaz | 8:c8007ccac5c0 | 67 | |
fy14aaz | 10:515df7535b2f | 68 | void Obstacles::addScore(N5110 &lcd) |
fy14aaz | 8:c8007ccac5c0 | 69 | { |
fy14aaz | 8:c8007ccac5c0 | 70 | for (int i=2; i<83; i+=1) { |
fy14aaz | 10:515df7535b2f | 71 | if (lcd.getPixel(i,46)) { |
fy14aaz | 10:515df7535b2f | 72 | _score++; |
fy14aaz | 10:515df7535b2f | 73 | _totalscore++; |
fy14aaz | 10:515df7535b2f | 74 | break; |
fy14aaz | 10:515df7535b2f | 75 | } |
fy14aaz | 10:515df7535b2f | 76 | } |
fy14aaz | 10:515df7535b2f | 77 | } |
fy14aaz | 10:515df7535b2f | 78 | |
fy14aaz | 10:515df7535b2f | 79 | void Obstacles::updateScore() |
fy14aaz | 10:515df7535b2f | 80 | { |
fy14aaz | 10:515df7535b2f | 81 | _score -= 10; |
fy14aaz | 8:c8007ccac5c0 | 82 | } |
fy14aaz | 8:c8007ccac5c0 | 83 | |
fy14aaz | 8:c8007ccac5c0 | 84 | int Obstacles::getScore() |
fy14aaz | 8:c8007ccac5c0 | 85 | { |
fy14aaz | 8:c8007ccac5c0 | 86 | return _score; |
fy14aaz | 8:c8007ccac5c0 | 87 | } |
fy14aaz | 9:d5f90988545e | 88 | int Obstacles::getTotalScore() |
fy14aaz | 8:c8007ccac5c0 | 89 | { |
fy14aaz | 9:d5f90988545e | 90 | return _totalscore; |
fy14aaz | 9:d5f90988545e | 91 | } |
fy14aaz | 9:d5f90988545e | 92 |