Nathan Gay
/
IRSensorGame
Game with the IR sensor.
Fork of Ticker_HelloWorld by
main.cpp@1:a0bf30d5af2f, 2015-10-22 (annotated)
- Committer:
- doctorgrue
- Date:
- Thu Oct 22 20:30:58 2015 +0000
- Revision:
- 1:a0bf30d5af2f
- Parent:
- 0:5014bf742e9b
First publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:5014bf742e9b | 1 | #include "mbed.h" |
doctorgrue | 1:a0bf30d5af2f | 2 | #include "uLCD_4DGL.h" |
doctorgrue | 1:a0bf30d5af2f | 3 | #include "rtos.h" |
doctorgrue | 1:a0bf30d5af2f | 4 | |
doctorgrue | 1:a0bf30d5af2f | 5 | uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin for uLCD; |
doctorgrue | 1:a0bf30d5af2f | 6 | AnalogIn distance(p20); //Read IR distance from pin 20 |
doctorgrue | 1:a0bf30d5af2f | 7 | |
doctorgrue | 1:a0bf30d5af2f | 8 | //Variables |
doctorgrue | 1:a0bf30d5af2f | 9 | int height; //which height the player is on |
doctorgrue | 1:a0bf30d5af2f | 10 | int tier; |
doctorgrue | 1:a0bf30d5af2f | 11 | int oldTier; |
doctorgrue | 1:a0bf30d5af2f | 12 | int dodged; //the number dodged |
doctorgrue | 1:a0bf30d5af2f | 13 | int gameOver; //Whether the player has lost |
doctorgrue | 1:a0bf30d5af2f | 14 | |
doctorgrue | 1:a0bf30d5af2f | 15 | int nm1=0; |
doctorgrue | 1:a0bf30d5af2f | 16 | int stage1=0; |
doctorgrue | 1:a0bf30d5af2f | 17 | |
doctorgrue | 1:a0bf30d5af2f | 18 | int nm2=0; |
doctorgrue | 1:a0bf30d5af2f | 19 | int stage2=0; |
doctorgrue | 1:a0bf30d5af2f | 20 | |
doctorgrue | 1:a0bf30d5af2f | 21 | int main() |
doctorgrue | 1:a0bf30d5af2f | 22 | { |
doctorgrue | 1:a0bf30d5af2f | 23 | |
doctorgrue | 1:a0bf30d5af2f | 24 | uLCD.baudrate(1000000); //baud rate max for faster display |
doctorgrue | 1:a0bf30d5af2f | 25 | uLCD.background_color(BLACK); |
doctorgrue | 1:a0bf30d5af2f | 26 | uLCD.text_width(2); //4X size text |
doctorgrue | 1:a0bf30d5af2f | 27 | uLCD.text_height(2); |
doctorgrue | 1:a0bf30d5af2f | 28 | while (true) { |
doctorgrue | 1:a0bf30d5af2f | 29 | dodged=0; //resets dodged |
doctorgrue | 1:a0bf30d5af2f | 30 | gameOver=0; //removes game over flag |
doctorgrue | 1:a0bf30d5af2f | 31 | tier=1; |
doctorgrue | 1:a0bf30d5af2f | 32 | oldTier=1; |
doctorgrue | 1:a0bf30d5af2f | 33 | uLCD.cls(); |
doctorgrue | 1:a0bf30d5af2f | 34 | uLCD.printf("Dodge the lasers. \nGame begins in \n3...\n"); |
doctorgrue | 1:a0bf30d5af2f | 35 | wait(1); |
doctorgrue | 1:a0bf30d5af2f | 36 | uLCD.printf("2...\n"); |
doctorgrue | 1:a0bf30d5af2f | 37 | wait(1); |
doctorgrue | 1:a0bf30d5af2f | 38 | uLCD.printf("1\n"); |
doctorgrue | 1:a0bf30d5af2f | 39 | wait(1); |
doctorgrue | 1:a0bf30d5af2f | 40 | uLCD.printf("Start"); |
doctorgrue | 1:a0bf30d5af2f | 41 | wait(0.3); |
doctorgrue | 1:a0bf30d5af2f | 42 | uLCD.cls(); |
doctorgrue | 1:a0bf30d5af2f | 43 | |
doctorgrue | 1:a0bf30d5af2f | 44 | while(gameOver==0){ |
doctorgrue | 1:a0bf30d5af2f | 45 | height=int(distance*100); |
doctorgrue | 1:a0bf30d5af2f | 46 | if(height>30) tier = 3; |
doctorgrue | 1:a0bf30d5af2f | 47 | else if(height>15) tier = 2; |
doctorgrue | 1:a0bf30d5af2f | 48 | else if(height<=15) tier = 1; |
doctorgrue | 1:a0bf30d5af2f | 49 | //uLCD.printf("%d",tier); |
doctorgrue | 1:a0bf30d5af2f | 50 | wait(0.1); |
doctorgrue | 1:a0bf30d5af2f | 51 | if(oldTier!=tier){ |
doctorgrue | 1:a0bf30d5af2f | 52 | uLCD.filled_circle(10, 33*oldTier, 30, BLACK); //delete old |
doctorgrue | 1:a0bf30d5af2f | 53 | oldTier=tier; |
doctorgrue | 1:a0bf30d5af2f | 54 | } |
doctorgrue | 1:a0bf30d5af2f | 55 | if(tier==1){ |
doctorgrue | 1:a0bf30d5af2f | 56 | uLCD.filled_circle(10, 33, 10, BLUE); //x,y,size,color |
doctorgrue | 1:a0bf30d5af2f | 57 | } |
doctorgrue | 1:a0bf30d5af2f | 58 | if(tier==2){ |
doctorgrue | 1:a0bf30d5af2f | 59 | uLCD.filled_circle(10, 66, 10, BLUE); |
doctorgrue | 1:a0bf30d5af2f | 60 | } |
doctorgrue | 1:a0bf30d5af2f | 61 | if(tier==3){ |
doctorgrue | 1:a0bf30d5af2f | 62 | uLCD.filled_circle(10, 99, 10, BLUE); |
doctorgrue | 1:a0bf30d5af2f | 63 | } |
doctorgrue | 1:a0bf30d5af2f | 64 | |
doctorgrue | 1:a0bf30d5af2f | 65 | if(stage1>0){ |
doctorgrue | 1:a0bf30d5af2f | 66 | stage1++; |
doctorgrue | 1:a0bf30d5af2f | 67 | if(stage1==7) uLCD.filled_circle(100, 33*nm1, 15, YELLOW); |
doctorgrue | 1:a0bf30d5af2f | 68 | if(stage1==14) { |
doctorgrue | 1:a0bf30d5af2f | 69 | //stage2=1; |
doctorgrue | 1:a0bf30d5af2f | 70 | uLCD.filled_circle(100, 33*nm1, 15, WHITE); |
doctorgrue | 1:a0bf30d5af2f | 71 | } |
doctorgrue | 1:a0bf30d5af2f | 72 | if(stage1==20) { |
doctorgrue | 1:a0bf30d5af2f | 73 | stage1=0; |
doctorgrue | 1:a0bf30d5af2f | 74 | uLCD.filled_circle(100, 33*nm1, 15, BLACK); |
doctorgrue | 1:a0bf30d5af2f | 75 | uLCD.line(0, 33*nm1, 100, 33*nm1, RED); |
doctorgrue | 1:a0bf30d5af2f | 76 | wait(0.2); |
doctorgrue | 1:a0bf30d5af2f | 77 | uLCD.line(0, 33*nm1, 100, 33*nm1, BLACK); |
doctorgrue | 1:a0bf30d5af2f | 78 | if(nm1==tier)gameOver=1; |
doctorgrue | 1:a0bf30d5af2f | 79 | else dodged++; |
doctorgrue | 1:a0bf30d5af2f | 80 | } |
doctorgrue | 1:a0bf30d5af2f | 81 | } |
doctorgrue | 1:a0bf30d5af2f | 82 | if(stage1==0){ |
doctorgrue | 1:a0bf30d5af2f | 83 | stage1++; |
doctorgrue | 1:a0bf30d5af2f | 84 | nm1=rand()%3+1; |
doctorgrue | 1:a0bf30d5af2f | 85 | //if(nm1==nm2) nm1++; |
doctorgrue | 1:a0bf30d5af2f | 86 | uLCD.filled_circle(100, 33*nm1, 15, RED); |
doctorgrue | 1:a0bf30d5af2f | 87 | // uLCD.filled_rectangle(90, 20, 20, 20, RED);//length,height,x,y,color |
doctorgrue | 1:a0bf30d5af2f | 88 | } |
doctorgrue | 1:a0bf30d5af2f | 89 | |
doctorgrue | 1:a0bf30d5af2f | 90 | /*if(stage2>0){ |
doctorgrue | 1:a0bf30d5af2f | 91 | if(stage2==1){ |
doctorgrue | 1:a0bf30d5af2f | 92 | nm2=rand()%3+1; |
doctorgrue | 1:a0bf30d5af2f | 93 | if(nm2==nm1) nm2--; |
doctorgrue | 1:a0bf30d5af2f | 94 | uLCD.filled_circle(100, 33*nm2, 15, RED); |
doctorgrue | 1:a0bf30d5af2f | 95 | } |
doctorgrue | 1:a0bf30d5af2f | 96 | stage2++; |
doctorgrue | 1:a0bf30d5af2f | 97 | if(stage1==10) uLCD.filled_circle(100, 33*nm1, 15, YELLOW); |
doctorgrue | 1:a0bf30d5af2f | 98 | if(stage1==20) uLCD.filled_circle(100, 33*nm1, 15, WHITE); |
doctorgrue | 1:a0bf30d5af2f | 99 | if(stage1==35) { |
doctorgrue | 1:a0bf30d5af2f | 100 | stage2=0; |
doctorgrue | 1:a0bf30d5af2f | 101 | uLCD.filled_circle(100, 33*nm1, 15, BLACK);} |
doctorgrue | 1:a0bf30d5af2f | 102 | }*/ |
doctorgrue | 1:a0bf30d5af2f | 103 | |
doctorgrue | 1:a0bf30d5af2f | 104 | } |
doctorgrue | 1:a0bf30d5af2f | 105 | uLCD.cls(); |
doctorgrue | 1:a0bf30d5af2f | 106 | uLCD.printf("GAME OVER\n"); |
doctorgrue | 1:a0bf30d5af2f | 107 | uLCD.printf("Lasers dodged:%d\n",dodged); |
doctorgrue | 1:a0bf30d5af2f | 108 | wait(3); |
doctorgrue | 1:a0bf30d5af2f | 109 | |
doctorgrue | 1:a0bf30d5af2f | 110 | |
doctorgrue | 1:a0bf30d5af2f | 111 | |
doctorgrue | 1:a0bf30d5af2f | 112 | |
doctorgrue | 1:a0bf30d5af2f | 113 | } |
mbed_official | 0:5014bf742e9b | 114 | } |