contains my game for the embedded systems project 2645

Dependencies:   mbed FXOS8700CQQQ

GameEngine/RocketRacer.cpp

Committer:
OmarAlebiary
Date:
2019-03-27
Revision:
9:edb39a8334ee
Parent:
8:b547037f42be
Child:
10:7323785c071c

File content as of revision 9:edb39a8334ee:

#include "RocketRacer.h"

Gamepad Mypad;
const int enemy[11][9] =   {
    { 0,0,0,0,1,0,0,0,0 },
    { 0,0,0,1,1,1,0,0,0 },
    { 0,0,1,1,1,1,1,0,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1 },
    { 1,1,1,1,1,1,1,1,1 },
};

const int rocket[11][9] =   {
    { 0,0,0,0,1,0,0,0,0 },
    { 0,0,0,1,1,1,0,0,0 },
    { 0,0,1,1,1,1,1,0,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,1,1,1,1,1,1,1,0 },
    { 0,0,1,1,1,1,1,0,0 },
    { 0,0,0,1,1,1,0,0,0 },
    { 0,0,0,0,1,0,0,0,0 },
    { 0,0,1,1,1,1,1,0,0 },
    { 0,0,1,1,1,1,1,0,0 },
};

int enemy_0_pos, enemy_1_pos, enemy_phase;
int Joy_X;
int game_speed = 0;
int score = 0;
char POS=2;
bool enemy_dead = true;
bool control = true;

 

void RocketRacer::MainGameDisplay(N5110 &lcd){
    
    lcd.clear();
    lcd.drawRect(0,0,50,47,FILL_TRANSPARENT);
    char buffer[14];
    char buffer1[14];
//    int score=64;
    
    sprintf(buffer,"score");
    lcd.printString(buffer,55,0);
    sprintf(buffer1,"%d",score);
    lcd.printString(buffer1,58,1);
    lcd.refresh();
    
    }

void RocketRacer::GameLoop(N5110 &lcd,Gamepad &pad){
    
lcd.clear(); 
//MainGameDisplay(lcd);
//    POS=2;
//    score=0;
//    enemy_dead = true;
//    control = true;
    
    Direction d=pad.get_direction();
    
    if(d==E && POS!=3 && control==true){
        POS++; 
        control = false;
        printf("its Right\n");
    }
    else if(d==W && POS!=1 && control==true){
        POS--;
        control = false;  
        printf("its left\n");
    }
    else if(d==CENTRE){
        control = true;
        printf("its middle\n");
        }
        
        player_position(lcd,POS);
        
        srand(time(NULL));
  
        if (enemy_dead){ 
            enemy_0_pos = POS; 
            enemy_1_pos = (rand() % 3)+1; 
            enemy_phase = 0; 
            enemy_dead = false;
            }
         enemy_position(lcd,enemy_0_pos, enemy_phase);
         enemy_phase++;
         enemy_position(lcd,enemy_1_pos, enemy_phase);
         enemy_phase++;
         
         if (enemy_phase>22 && ((enemy_0_pos == POS) || (enemy_1_pos == POS)) ){
             EndGame(lcd);
             }
         if (enemy_phase>40){
             enemy_dead = true; 
             score++;
             }  
             wait(0.1);
             lcd.refresh();
    }





void RocketRacer::enemy_position(N5110 &lcd,int place, int phase){
  if (place==1){
      lcd.drawSprite(2,phase,11,9,(int *)enemy);
      }
  
  if (place==2){
      lcd.drawSprite(18,phase,11,9,(int *)enemy);
      }
  
  if (place==3){
      lcd.drawSprite(34,phase,11,9,(int *)enemy);
      }
      lcd.refresh();
  
}

void RocketRacer::player_position(N5110 &lcd,char RocketPosition){
  
  MainGameDisplay(lcd);
  
  
  if (RocketPosition==1){   
      lcd.drawSprite(2,34,11,9,(int *)rocket);
      }
  if (RocketPosition==2){
      lcd.drawSprite(18,34,11,9,(int *)rocket);
      }
  if (RocketPosition==3){
      lcd.drawSprite(34,34,11,9,(int *)rocket);
      }
      lcd.refresh();
}


void RocketRacer::EndGame(N5110 &lcd){
    lcd.clear(); while(1){
    lcd.printString("Game over!!!",5,0);
    lcd.printString("Better Luck ",2,2);
    lcd.printString("next time",2,3);
    }
//    lcd.refresh();
    
    }