projeto de corrida

Dependencies:   TermControl

main.cpp

Committer:
diegocavalcanti
Date:
2016-12-10
Revision:
0:476c153c1126

File content as of revision 0:476c153c1126:

#include "mbed.h"
#include "stdlib.h"
#include "rtos.h"
#include "TermControl.h"

Serial pc(USBTX, USBRX);
Serial pc2(USBTX, USBRX);
char tela[20][6];
bool jogando;

// main() runs in its own thread in the OS
// (note the calls to Thread::wait below for delays)

bool verificaesquerda(){
    if(
    tela[16][1] == 'o' && 
    tela[17][0] == 'o' &&  
    tela[17][1] == 'o' && 
    tela[17][2] == 'o' && 
    tela[18][1] == 'o' && 
    tela[19][0] == 'o' && 
    tela[19][2] == 'o'){
        return true;               
    }else{
        return false;    
    }    
}
bool verificadireita(){
    if( tela[16][4] == 'o' || 
            tela[17][3] == 'o' || 
            tela[17][4] == 'o' || 
            tela[17][5] == 'o' || 
            tela[18][4] == 'o' || 
            tela[19][3] == 'o' || 
            tela[19][5] == 'o'){
        return true;               
    }else{
        return false;    
    }    
}

void car_left(){
        
       
        if(tela[16][1] == 'o' && tela[17][0] == 'o' &&  tela[17][1] == 'o' && tela[17][2] == 'o' && tela[18][1] == 'o' && tela[19][0] == 'o' && tela[19][2] == 'o'){
            
        
                tela[16][1] = 'o';
                tela[17][0] = 'o';
                tela[17][1] = 'o';
                tela[17][2] = 'o';
                tela[18][1] = 'o';
                tela[19][0] = 'o';
                tela[19][2] = 'o';
            
        }else{
            tela[16][4] = ' ';
            tela[17][3] = ' ';
            tela[17][4] = ' ';
            tela[17][5] = ' ';
            tela[18][4] = ' ';
            tela[19][3] = ' ';
            tela[19][5] = ' ';
            
            tela[16][1] = 'o';
            tela[17][0] = 'o';
            tela[17][1] = 'o';
            tela[17][2] = 'o';
            tela[18][1] = 'o';
            tela[19][0] = 'o';
            tela[19][2] = 'o';
        }
        
            
        
}
void car_right(){
        if(tela[16][1] == 'o' && tela[17][0] == 'o' &&  tela[17][1] == 'o' && tela[17][2] == 'o' && tela[18][1] == 'o' && tela[19][0] == 'o' && tela[19][2] == 'o'){
            tela[16][4] = 'o';
            tela[17][3] = 'o';
            tela[17][4] = 'o';
            tela[17][5] = 'o';
            tela[18][4] = 'o';
            tela[19][3] = 'o';
            tela[19][5] = 'o';
            
            tela[16][1] = ' ';
            tela[17][0] = ' ';
            tela[17][1] = ' ';
            tela[17][2] = ' ';
            tela[18][1] = ' ';
            tela[19][0] = ' ';
            tela[19][2] = ' ';

        }else{
            tela[16][4] = 'o';
            tela[17][3] = 'o';
            tela[17][4] = 'o';
            tela[17][5] = 'o';
            tela[18][4] = 'o';
            tela[19][3] = 'o';
            tela[19][5] = 'o';
        }
        
}

void keyboard(void) {
    while(jogando) {
            char comando = pc2.getc();
            
                if(comando == 97){
                    car_left();
                }
                if(comando == 100){
                    car_right();
                }
                
                Thread::wait(500);
    }
}

int main() {
    jogando=true;
    TermControl ctrl;
    ctrl.SetTerminal(&pc);
    Thread t1;
    t1.start(keyboard);
    int pontuacao =0;
    int velocidade = 500;
   
    
    int mov = 1;
    int new_car = 1;
    
    pc.baud(115200);
    
    for(int i=0; i < 20; i++){
        for(int j=0; j < 6; j++){
            tela[i][j] = ' ';
            
        }
    }
    
    int side_car = rand()%2;
    
    if(side_car == 0){
        car_left();
    }
    if(side_car == 1){
        car_right();
    }
    
    while (jogando){
            
            if(new_car%8 == 0){
                pontuacao++;  
                int lado_carros = rand()%2;
                pc.printf("%d", lado_carros);
                pc.printf("\r\n");
                
                
                    if(lado_carros == 0){
                            tela[0][1] = '#';
                            tela[1][0] = '#';
                            tela[1][1] = '#';
                            tela[1][2] = '#';
                            tela[2][1] = '#';
                            tela[3][0] = '#';
                            tela[3][2] = '#';
                    }
                    if(lado_carros == 1){
                            tela[0][4] = '#';
                            tela[1][3] = '#';
                            tela[1][4] = '#';
                            tela[1][5] = '#';
                            tela[2][4] = '#';
                            tela[3][3] = '#';
                            tela[3][5] = '#'; 
                    }
                }
            new_car++;
            
            
        for(int i = 0; i< 20; i++) {
            for(int j = 0; j < 6; j++) {
                pc.printf("%c", tela[i][j]);
                
            }
            pc.printf("\r\n");
            
        }    
            
       
        
        for(int i = 19; i >= 0; i--) {
             for(int j = 5; j >= 0; j--) {
                
                             
                    bool verifica = verificadireita();
                    if(verifica){
                        if(
                            tela[16][4] == '#' || 
                            tela[17][3] == '#' || 
                            tela[17][4] == '#' || 
                            tela[17][5] == '#' || 
                            tela[18][4] == '#' || 
                            tela[19][3] == '#' || 
                            tela[19][5] == '#' ){            
                            
                            
                            jogando=false;
                          
                            break;
                           
                        }else if(tela[i][j] == '#'){
                             tela[i][j] = ' ';
                              tela[i+mov][j] = '#';
                              
                        }
                                            
                        
                    }else {
                       if(
                            tela[16][1] == '#' ||               
                            tela[17][0] == '#' ||
                            tela[17][1] == '#' ||
                            tela[17][2] == '#' ||                
                            tela[18][1] == '#' ||                
                            tela[19][0] == '#' ||                
                            tela[19][2] == '#' ){            
                            
                            jogando=false;
                            
                            break;
                           
                        }else if(tela[i][j] == '#'){
                             tela[i][j] = ' ';
                              tela[i+mov][j] = '#';
                              
                            
                        }                    
                        
                    }//IF
            }//FOR
           
        }//FOR
        
        
         
              
        pc.printf("%d", pontuacao);
        velocidade=velocidade-2;
        Thread::wait(velocidade);
        //ctrl.Clear();
        pc.printf("\x1B[2J");
    }
    pc.printf("%s", "GAME OVER!");
    pc.printf("%s", "SUA PONTUACAO: ");
    pc.printf("%d", pontuacao);

  
}