wu weilong / Mbed 2 deprecated el18w2w

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Gamepad.h"
00003 #include "N5110.h"
00004 #include "element.h"
00005 
00006 
00007 void init(int n);
00008 void render(int n);
00009 void update(int &n);
00010 void hitwall(int &n);
00011 void welcome();
00012 void eat(int &n);
00013 void hitself(int &n);
00014 
00015 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00016 Gamepad pad;
00017 element e;
00018 int main(){
00019     int n=20;
00020     int fps =8;
00021     init(n);
00022     welcome();
00023     render(n);
00024     wait(1.0f/fps); 
00025     while(1){
00026         
00027         update(n);
00028         render(n);
00029         wait(1.0f/fps); 
00030     }
00031 }
00032 
00033 void init(int n){
00034     lcd.init();
00035     pad.init();
00036     e.init(2,20,20,30,30,n);
00037   
00038 }
00039 void render(int n){
00040     lcd.clear();
00041     e.draw(lcd,n);
00042     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
00043     lcd.refresh();
00044 }
00045 void update(int &n){
00046     e.input(pad);
00047     e.move(n);
00048     eat(n);
00049     hitwall(n);
00050     hitself(n);
00051     
00052 }
00053 void hitwall(int &n){
00054     if(e.getsnakepos(0).x<=0 || e.getsnakepos(0).x+1>=WIDTH ||e.getsnakepos(0).y<=0||e.getsnakepos(0).y+1>=HEIGHT)
00055     {
00056        
00057        while ( pad.check_event(Gamepad::BACK_PRESSED) == false){
00058         lcd.clear();
00059         lcd.printString("    FAILED    ",0,1);
00060         lcd.printString(" YOU HIT WALL",0,2);
00061         lcd.printString("  PRESS BACK    ",0,3);  
00062         char buffer[14];
00063          sprintf(buffer,"SCORE = %2d ",n-20);
00064          lcd.printString(buffer,0,5);  
00065         lcd.refresh();
00066       
00067         
00068     }
00069     n=20;
00070     e.init(2,20,20,30,30,n);
00071     
00072     
00073 }
00074 
00075 }
00076 void welcome() {
00077     
00078     lcd.printString("     SNAKE!    ",0,1);  
00079     lcd.printString("  Press Start to play ",0,4);
00080     lcd.refresh();
00081      
00082     // wait flashing LEDs until start button is pressed 
00083     while ( pad.check_event(Gamepad::START_PRESSED) == false) {
00084         pad.leds_on();
00085         wait(0.1);
00086         pad.leds_off();
00087         wait(0.1);
00088     }
00089  
00090 } 
00091 void eat(int &n ){
00092 
00093     for(int i=0;i<2;i++)
00094     {
00095         if(e.getsnakepos(0).y+i ==e.getfoodpos().y && e.getsnakepos(0).x ==e.getfoodpos().x )
00096         {
00097             e.update();
00098             n+=1;
00099            
00100         }
00101         if(e.getsnakepos(0).y+1==e.getfoodpos().y &&e.getsnakepos(0).x+i==e.getfoodpos().x)
00102         {
00103             e.update();
00104             n+=1;
00105           
00106         }
00107         if(e.getsnakepos(0).y==e.getfoodpos().y &&e.getsnakepos(0).x+i==e.getfoodpos().x)
00108         {
00109             e.update();
00110             n+=1;
00111           
00112         }
00113           if(e.getsnakepos(0).y+i==e.getfoodpos().y &&e.getsnakepos(0).x+1==e.getfoodpos().x)
00114         {
00115             e.update();
00116             n+=1;
00117           
00118         }
00119        //x,y
00120         if(e.getsnakepos(0).y+i ==e.getfoodpos().y && e.getsnakepos(0).x ==e.getfoodpos().x+1 )
00121         {
00122             e.update();
00123             n+=1;
00124           
00125         }
00126         if(e.getsnakepos(0).y+1==e.getfoodpos().y &&e.getsnakepos(0).x+i==e.getfoodpos().x+1)
00127         {
00128             e.update();
00129             n+=1;
00130            
00131         }
00132         if(e.getsnakepos(0).y==e.getfoodpos().y &&e.getsnakepos(0).x+i==e.getfoodpos().x+1)
00133         {
00134             e.update();
00135             n+=1;
00136          
00137         }
00138           if(e.getsnakepos(0).y+i==e.getfoodpos().y &&e.getsnakepos(0).x+1==e.getfoodpos().x+1)
00139         {
00140             e.update();
00141             n+=1;
00142            
00143         }
00144         //x+1,y
00145         
00146          if(e.getsnakepos(0).y+i ==e.getfoodpos().y+1 && e.getsnakepos(0).x ==e.getfoodpos().x )
00147         {
00148             e.update();
00149             n+=1;
00150            
00151         }
00152         if(e.getsnakepos(0).y+1==e.getfoodpos().y+1 &&e.getsnakepos(0).x+i==e.getfoodpos().x)
00153         {
00154             e.update();
00155             n+=1;
00156           
00157         }
00158         if(e.getsnakepos(0).y==e.getfoodpos().y+1&&e.getsnakepos(0).x+i==e.getfoodpos().x)
00159         {
00160             e.update();
00161             n+=1;
00162          
00163         }
00164           if(e.getsnakepos(0).y+i==e.getfoodpos().y+1 &&e.getsnakepos(0).x+1==e.getfoodpos().x)
00165         {
00166             e.update();
00167             n+=1;
00168          
00169         }
00170         //x,y-1
00171         if(e.getsnakepos(0).y+i ==e.getfoodpos().y+1 && e.getsnakepos(0).x ==e.getfoodpos().x +1)
00172         {
00173             e.update();
00174             n+=1;
00175           
00176         }
00177         if(e.getsnakepos(0).y+1==e.getfoodpos().y+1 &&e.getsnakepos(0).x+i==e.getfoodpos().x+1)
00178         {
00179             e.update();
00180             n+=1;
00181             
00182         }
00183         if(e.getsnakepos(0).y==e.getfoodpos().y+1 &&e.getsnakepos(0).x+i==e.getfoodpos().x+1)
00184         {
00185             e.update();
00186             n+=1;
00187        
00188         }
00189           if(e.getsnakepos(0).y+i==e.getfoodpos().y+1 &&e.getsnakepos(0).x+1==e.getfoodpos().x+1)
00190         {
00191             e.update();
00192             n+=1;
00193          
00194         }
00195     }
00196         
00197 }
00198 void hitself(int &n){
00199     for(int i=2;i<n;i++)
00200     {
00201         for(int j=0;j<2;j++)
00202         {
00203         if(e.getsnakepos(0).x==e.getsnakepos(i).x+1 && e.getsnakepos(0).y+j==e.getsnakepos(i).y+1)
00204         {
00205             while ( pad.check_event(Gamepad::BACK_PRESSED) == false)
00206             {
00207             lcd.clear();
00208             lcd.printString(" FAILED    ",0,1);
00209             lcd.printString(" YOU HIT SELF!    ",0,2);
00210             lcd.printString(" PRESS BACK    ",0,3);  
00211             char buffer[14];
00212             sprintf(buffer,"SCORE = %2d ",n-20);
00213             lcd.printString(buffer,0,4);  
00214             lcd.refresh();
00215             lcd.clear();
00216             
00217         }
00218          n=20;
00219         e.init(2,20,20,30,30,n);
00220       
00221        
00222         }
00223         
00224         
00225         
00226         
00227          if(e.getsnakepos(0).x+j==e.getsnakepos(i).x+1 && e.getsnakepos(0).y==e.getsnakepos(i).y+1)
00228         {
00229             while ( pad.check_event(Gamepad::BACK_PRESSED) == false)
00230             {
00231             lcd.clear();
00232             lcd.printString(" HIT SELF!    ",0,2);
00233             lcd.printString(" PRESS BACK    ",0,3);
00234             char buffer[14];
00235             sprintf(buffer,"SCORE = %2d ",n-20);
00236             lcd.printString(buffer,0,4);      
00237             lcd.refresh();
00238             lcd.clear();
00239            
00240             }
00241              n=20;
00242             e.init(2,20,20,30,30,n);
00243        
00244         }
00245         
00246         
00247         
00248         
00249         
00250         
00251          if(e.getsnakepos(0).x+j==e.getsnakepos(i).x+1 && e.getsnakepos(0).y+2==e.getsnakepos(i).y+1)
00252         {
00253             while ( pad.check_event(Gamepad::BACK_PRESSED) == false)
00254             {
00255             lcd.clear();
00256             lcd.printString(" HIT SELF!    ",0,2);
00257             lcd.printString(" PRESS BACK    ",0,3); 
00258             char buffer[14];
00259             sprintf(buffer,"SCORE = %2d ",n-20);
00260             lcd.printString(buffer,0,4);   
00261             lcd.refresh();
00262             lcd.clear();
00263           
00264                  
00265             }
00266             n=20;
00267             e.init(2,20,20,30,30,n);
00268        
00269         }
00270         
00271         
00272          if(e.getsnakepos(0).x+2==e.getsnakepos(i).x+1 && e.getsnakepos(0).y+j==e.getsnakepos(i).y+1)
00273         {
00274             while ( pad.check_event(Gamepad::BACK_PRESSED) == false)
00275             {
00276             lcd.clear();
00277             lcd.printString(" HIT SELF!    ",0,2);
00278             lcd.printString(" PRESS BACK    ",0,3);
00279             char buffer[14];
00280             sprintf(buffer,"SCORE = %2d ",n-20);
00281             lcd.printString(buffer,0,4);    
00282             lcd.refresh();
00283             lcd.clear();
00284            
00285                  
00286             }
00287             n=20;
00288             e.init(2,20,20,30,30,n);
00289        
00290         }
00291         
00292         }
00293         }
00294 }
00295 
00296