Haoyan Zhang / Mbed 2 deprecated el17h2z1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mian.cpp Source File

mian.cpp

00001 /* 
00002 ELEC2645 Embedded Systems Project 
00003 School of Electronic & Electrical Engineering 
00004 University of Leeds 
00005 Name: Haoyan Zhang
00006 Username: el17h2z
00007 Student ID Number: 201199698
00008 Date: 14/05/2020
00009  */ 
00010  
00011  ///////// pre-processor directives ////////
00012 #include "mbed.h"
00013 #include "N5110.h"
00014 #include "Gamepad.h"
00015 #include "StarcraftEngine.h"
00016 
00017 #define BATTLESHIP_HEIGHT 6
00018 #define BATTLESHIP_WIDTH 4
00019 #define LASER_HEIGHT 2
00020 #define LASER_WIDTH 1
00021 #define SWARM_HEIGHT 6
00022 #define SWARM_WIDTH 6 
00023 #define SWARM_SPEED 1
00024 #define BOSS_HEIGHT 4
00025 #define BOSS_WIDTH 6
00026 #define ACID_HEIGHT 3
00027 #define ACID_WIDTH 1
00028 
00029 
00030 
00031 /////////////// structs /////////////////
00032 struct UserInput {
00033     Direction d;
00034     float mag;
00035 };
00036 
00037 /////////////// objects ///////////////
00038 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
00039 Gamepad pad;
00040 StarcraftEngine Starcraft;
00041 
00042 ///////////// prototypes ///////////////
00043 void init();
00044 void update_game(UserInput input);
00045 void render();
00046 void welcome();
00047 void victory();
00048 void gameover();
00049 void background();
00050 void score();
00051 
00052 ///////////// functions ////////////////
00053 int main()
00054 {
00055     int fps = 8;  // frames per second
00056     S1:init();     // initialise and then display welcome screen...
00057     welcome();  // waiting for the user to start
00058     background();  //introduce background story
00059     render();  // first draw the initial frame 
00060     wait(1.0f/fps);  // and wait for one frame period
00061     
00062     // game loop - read input, update the game state and render the display
00063     while(Starcraft.find_life() > 0) {
00064          Starcraft.read_input(pad);
00065          Starcraft.update(pad);
00066          render();
00067          wait(1.0f/fps);
00068          if (Starcraft.find_score() == 15) {
00069              break;
00070          }     
00071     }     
00072     
00073     score();
00074     if (Starcraft.find_score() == 15) {   // judge victory or fault
00075       victory();
00076       wait(1);
00077       goto S1;
00078     }  
00079       
00080     else{  
00081         gameover();
00082         wait(1);
00083         goto S1;
00084         }
00085 }
00086 
00087 // initialies all classes and libraries
00088 void init()
00089 {
00090      // need to initialise LCD and Gamepad 
00091      lcd.init();
00092      pad.init();
00093      
00094      // initialise the game with correct battleship, laser, boss and acid's sizes
00095      Starcraft.init(BATTLESHIP_HEIGHT, BATTLESHIP_WIDTH, LASER_HEIGHT, LASER_WIDTH,SWARM_HEIGHT, SWARM_WIDTH, BOSS_HEIGHT, BOSS_WIDTH, ACID_HEIGHT, ACID_WIDTH, SWARM_SPEED);
00096 }
00097 
00098 // this function draws each frame on the LCD
00099 void render()
00100 {
00101     // clear screen, re-draw and refresh
00102     lcd.clear();  
00103     Starcraft.draw(lcd);
00104     lcd.refresh();
00105 }
00106 
00107 // simple splash screen displayed on start-up
00108 void welcome()
00109 {
00110      lcd.printString("Starcraft!",0,1);  
00111      lcd.printString("Press Start",0,4);
00112      lcd.refresh();
00113      
00114      // wait flashing LEDs until start button is pressed 
00115      while ( pad.check_event(Gamepad::START_PRESSED) == false) {
00116            pad.leds_on();
00117            wait(0.1);
00118            pad.leds_off();
00119            wait(0.1);
00120      }
00121  
00122 }
00123 
00124 // introduce background story       
00125 void background()
00126 {
00127      lcd.clear(); 
00128      lcd.printString(" WARNING!!! ",1,1);
00129      lcd.printString(" WARNING!!! ",1,3);
00130      lcd.refresh();
00131      wait(2);
00132      
00133      lcd.clear(); 
00134      lcd.printString(" Swarm invide ",1,1);
00135      lcd.printString(" Our galaxy ",1,3);
00136      lcd.refresh();
00137      wait(2);
00138      
00139      lcd.clear(); 
00140      lcd.printString(" Our fleet ",1,1);
00141      lcd.printString(" Has been ",1,2);
00142      lcd.printString(" Destroyed ",2,3);
00143      lcd.refresh();
00144      wait(2);
00145      
00146      lcd.clear(); 
00147      lcd.printString(" You're the ",1,1);
00148      lcd.printString(" Last ",2,2);
00149      lcd.printString(" Battleship ",3,4);
00150      lcd.refresh();
00151      wait(2);
00152      
00153      lcd.clear(); 
00154      lcd.printString(" Kill them ",1,1);
00155      lcd.printString(" All!!! ",3,3);
00156      lcd.refresh();
00157      wait(2);
00158      
00159      lcd.clear(); 
00160      lcd.printString(" Good luck! ",2,2);
00161      lcd.refresh();
00162      wait(2);
00163 }
00164 
00165 // when game over the screen will display the score
00166 void score()
00167 {
00168     lcd.clear();  
00169     int battleship_score = Starcraft.find_score();
00170     char buffer3[14];
00171     sprintf(buffer3,"%2d",battleship_score);
00172     lcd.printString("SCORE",25 ,2);
00173     lcd.printString(buffer3,35 ,3);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
00174     lcd.refresh();
00175     wait(1.5);
00176 }
00177 
00178 void victory()
00179 {
00180     lcd.clear();
00181     lcd.printString(" Victory! ",1,1);
00182     lcd.printString(" You protect ",1,2);
00183     lcd.printString(" the earth! ",1,3);
00184     lcd.refresh();
00185     wait(2);
00186     
00187     lcd.clear();
00188     lcd.printString(" Press back ",1,1);
00189     lcd.printString(" to play again ",1,3);
00190     lcd.refresh();
00191      
00192     while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::BACK_PRESSED) == false) {
00193           pad.leds_on();
00194           wait(0.1);
00195           pad.leds_off();
00196           wait(0.1);
00197     }
00198 } 
00199 
00200 void gameover()
00201 {
00202     lcd.clear();
00203     lcd.printString(" You failed! ",1,1);
00204     lcd.printString(" Press back ",1,3);
00205     lcd.printString(" to play again ",1,4);
00206     lcd.refresh();
00207      
00208     while ( pad.check_event(Gamepad::START_PRESSED) == false && pad.check_event(Gamepad::BACK_PRESSED) == false) {
00209           pad.leds_on();
00210           wait(0.1);
00211           pad.leds_off();
00212           wait(0.1);
00213     }
00214 }