class for obstacles in Car_race game
Obstacles.cpp@11:c0922d4fda7b, 2017-05-04 (annotated)
- Committer:
- fy14aaz
- Date:
- Thu May 04 12:04:51 2017 +0000
- Revision:
- 11:c0922d4fda7b
- Parent:
- 10:515df7535b2f
final version
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 | 11:c0922d4fda7b | 15 | _Obstacle_pos_x = seed; |
fy14aaz | 11:c0922d4fda7b | 16 | _score = 0; // initialise scores |
fy14aaz | 9:d5f90988545e | 17 | _totalscore = 0; |
fy14aaz | 11:c0922d4fda7b | 18 | // printf("_Obstacle_pos_x=%d \n",_Obstacle_pos_x); |
fy14aaz | 0:f8968ec0ec1b | 19 | } |
fy14aaz | 0:f8968ec0ec1b | 20 | |
fy14aaz | 1:a735e7396af4 | 21 | void Obstacles::draw(N5110 &lcd,int seed) |
fy14aaz | 0:f8968ec0ec1b | 22 | { |
fy14aaz | 11:c0922d4fda7b | 23 | _Obstacle_pos_x = seed; // give a random number to the x position of the obstacle |
fy14aaz | 11:c0922d4fda7b | 24 | // printf("_Obstacle_pos_x=%d \n",_Obstacle_pos_x); |
fy14aaz | 11:c0922d4fda7b | 25 | switch(_Obstacle_pos_x) { |
fy14aaz | 10:515df7535b2f | 26 | case 0: |
fy14aaz | 11:c0922d4fda7b | 27 | lcd.drawRect(2,11,25,2,FILL_BLACK); // draw in the left |
fy14aaz | 10:515df7535b2f | 28 | break; |
fy14aaz | 10:515df7535b2f | 29 | |
fy14aaz | 10:515df7535b2f | 30 | case 1: |
fy14aaz | 11:c0922d4fda7b | 31 | lcd.drawRect(28,11,27,2,FILL_BLACK); // draw in the middle |
fy14aaz | 10:515df7535b2f | 32 | break; |
fy14aaz | 10:515df7535b2f | 33 | |
fy14aaz | 10:515df7535b2f | 34 | case 2: |
fy14aaz | 11:c0922d4fda7b | 35 | lcd.drawRect(56,11,25,2,FILL_BLACK); // draw in the right |
fy14aaz | 10:515df7535b2f | 36 | break; |
fy14aaz | 10:515df7535b2f | 37 | } |
fy14aaz | 0:f8968ec0ec1b | 38 | } |
fy14aaz | 0:f8968ec0ec1b | 39 | |
fy14aaz | 11:c0922d4fda7b | 40 | void Obstacles::update(N5110 &lcd) |
fy14aaz | 10:515df7535b2f | 41 | { |
fy14aaz | 11:c0922d4fda7b | 42 | add_Score(lcd); |
fy14aaz | 11:c0922d4fda7b | 43 | |
fy14aaz | 11:c0922d4fda7b | 44 | char pixelstate[82][37]; // generate an array to store pixels' values |
fy14aaz | 11:c0922d4fda7b | 45 | for (int i=1; i<83; i+=1) { |
fy14aaz | 11:c0922d4fda7b | 46 | for (int j=10; j<47; j+=1) { // notice that we start from the 10th row as to allow enough for the |
fy14aaz | 11:c0922d4fda7b | 47 | if (lcd.getPixel(i,j)) { // real time prints to be displayed in the first 10 rows |
fy14aaz | 11:c0922d4fda7b | 48 | pixelstate[i-1][j-10]=1; |
fy14aaz | 10:515df7535b2f | 49 | } else { |
fy14aaz | 11:c0922d4fda7b | 50 | pixelstate[i-1][j-10]=0; |
fy14aaz | 10:515df7535b2f | 51 | } |
fy14aaz | 10:515df7535b2f | 52 | } |
fy14aaz | 10:515df7535b2f | 53 | } |
fy14aaz | 11:c0922d4fda7b | 54 | for (int i=1; i<83; i+=1) { // loop through the array values now and then |
fy14aaz | 11:c0922d4fda7b | 55 | for (int j=10; j<47; j+=1) { // shift all the pixels down |
fy14aaz | 11:c0922d4fda7b | 56 | if ((pixelstate[i-1][j-10]) ) { |
fy14aaz | 11:c0922d4fda7b | 57 | lcd.setPixel(i,j+2); // pixels move in an increment of two for higher speed |
fy14aaz | 11:c0922d4fda7b | 58 | } |
fy14aaz | 11:c0922d4fda7b | 59 | if ((pixelstate[i-1][j-11]==0) ) { |
fy14aaz | 11:c0922d4fda7b | 60 | lcd.setPixel(i,j+1,false); |
fy14aaz | 10:515df7535b2f | 61 | } |
fy14aaz | 10:515df7535b2f | 62 | } |
fy14aaz | 10:515df7535b2f | 63 | } |
fy14aaz | 8:c8007ccac5c0 | 64 | } |
fy14aaz | 8:c8007ccac5c0 | 65 | |
fy14aaz | 11:c0922d4fda7b | 66 | void Obstacles::add_Score(N5110 &lcd) |
fy14aaz | 8:c8007ccac5c0 | 67 | { |
fy14aaz | 11:c0922d4fda7b | 68 | // loop through the last row of the screen and check if a pixel is detected |
fy14aaz | 11:c0922d4fda7b | 69 | for (int i=2; i<83; i+=1) { // if so, then an obstacle is passing, hence break out from the loop since |
fy14aaz | 11:c0922d4fda7b | 70 | if (lcd.getPixel(i,46)) { // only one pixel is sufficient to prove that |
fy14aaz | 11:c0922d4fda7b | 71 | _score++; // increase adjustable score |
fy14aaz | 11:c0922d4fda7b | 72 | _totalscore++; // increase total/final score |
fy14aaz | 11:c0922d4fda7b | 73 | // printf("Done \n"); |
fy14aaz | 10:515df7535b2f | 74 | break; |
fy14aaz | 10:515df7535b2f | 75 | } |
fy14aaz | 10:515df7535b2f | 76 | } |
fy14aaz | 10:515df7535b2f | 77 | } |
fy14aaz | 10:515df7535b2f | 78 | |
fy14aaz | 11:c0922d4fda7b | 79 | void Obstacles::reduce_Score() |
fy14aaz | 10:515df7535b2f | 80 | { |
fy14aaz | 11:c0922d4fda7b | 81 | _score -= 10; // reduce score when feature is used in other functions |
fy14aaz | 8:c8007ccac5c0 | 82 | } |
fy14aaz | 8:c8007ccac5c0 | 83 | |
fy14aaz | 11:c0922d4fda7b | 84 | int Obstacles::get_Score() |
fy14aaz | 8:c8007ccac5c0 | 85 | { |
fy14aaz | 11:c0922d4fda7b | 86 | return _score; // adjustable score |
fy14aaz | 8:c8007ccac5c0 | 87 | } |
fy14aaz | 11:c0922d4fda7b | 88 | int Obstacles::get_TotalScore() |
fy14aaz | 8:c8007ccac5c0 | 89 | { |
fy14aaz | 11:c0922d4fda7b | 90 | return _totalscore; // total/final score |
fy14aaz | 9:d5f90988545e | 91 | } |
fy14aaz | 9:d5f90988545e | 92 |