Laila Al Badwawi 200906179 SpaceInvaders I declare this my own independent work and understand the university rules on plagiarism.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 13:9d6ee753eca6
- Parent:
- 12:45b1249b3d9a
- Child:
- 28:4786e81ce3e3
--- a/main.cpp Mon Apr 15 14:36:07 2019 +0000 +++ b/main.cpp Fri Apr 19 17:33:57 2019 +0000 @@ -1,4 +1,3 @@ - ///////// pre-processor directives //////// #include "mbed.h" #include "Gamepad.h" @@ -6,43 +5,44 @@ #include "SpaceInvadersEngine.h" - -#ifdef WITH_TESTING -# include "tests.h" -#endif - -//#define PADDLE_WIDTH 2 -//#define PADDLE_HEIGHT 8 -//#define BALL_SIZE 2 -//#define BALL_SPEED 3 - // structs // struct UserInput { Direction d; float mag; }; +Direction _d2; -/////////////// objects /////////////// +int space_ship_width; +int space_ship_height; +int bullet_size; +int alien_size; +int speed; + + +////////////// objects /////////////// N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); Gamepad pad; -Direction _d; SpaceInvadersEngine space; + ///////////// prototypes /////////////// void init(); void update_game(UserInput input); void render(); void welcome(); + const int bullet[4][10] = { { 0,0,0,0,0,1,1,1,0,0 }, { 0,0,0,0,0,1,1,1,0,0 }, { 0,0,0,1,1,1,1,0,0,0 }, { 0,0,1,1,1,1,0,1,0,0 }, }; + + const int alien[12][10] = { { 0,0,0,0,0,1,1,1,0,0 }, { 0,0,0,0,0,1,1,1,0,0 }, @@ -62,35 +62,25 @@ -const int space_ship[10][12]={ -{0,0,0,0,0,0,0,0,0,0,0,0}, -{0,0,0,1,1,0,0,0,0,0,0,0}, -{0,0,0,1,1,0,0,0,0,0,0,0}, -{0,1,1,1,1,1,1,1,0,0,0,0}, -{0,1,1,1,1,1,1,1,1,0,0,0}, -{0,1,1,1,1,1,1,1,1,1,1,0}, -{0,1,1,1,1,1,1,1,1,0,0,0}, -{0,1,1,1,1,1,1,1,0,0,0,0}, -{0,0,0,1,1,0,0,0,0,0,0,0}, -{0,0,0,1,1,0,0,0,0,0,0,0}, + const int space_ship[10][12]={ + {0,0,0,0,0,0,0,0,0,0,0,0}, + {0,0,0,1,1,0,0,0,0,0,0,0}, + {0,0,0,1,1,0,0,0,0,0,0,0}, + {0,1,1,1,1,1,1,1,0,0,0,0}, + {0,1,1,1,1,1,1,1,1,0,0,0}, + {0,1,1,1,1,1,1,1,1,1,1,0}, + {0,1,1,1,1,1,1,1,1,0,0,0}, + {0,1,1,1,1,1,1,1,0,0,0,0}, + {0,0,0,1,1,0,0,0,0,0,0,0}, + {0,0,0,1,1,0,0,0,0,0,0,0}, }; - -Direction _d2; +///////////// functions //////////////// +int main(){ -///////////// functions //////////////// -int main() -{ - /* -#ifdef WITH_TESTING - int number_of_failures = run_all_tests(); - - if(number_of_failures > 0) return number_of_failures; -#endif -*/ int fps = 8; // frames per second init(); // initialise and then display welcome screen... @@ -111,61 +101,68 @@ int bullet_fired=0; int x_space_ship=0; int y_space_ship=40; + + // game loop - read input, update the game state and render the display while (1) { space.read_input(pad); space.update(pad); - render(); + render(); + _d2 = pad.get_direction(); + + lcd.clear(); - - lcd.drawSprite(x_space_ship,y_space_ship,12,10,(int *)space_ship); lcd.drawSprite(x_alien,y_alien,12,10,(int *)alien); lcd.drawSprite(x_bullet,y_bullet,4,10,(int *)bullet); - - space.read_input(pad); - _d2 = pad.get_direction(); - if(_d2==N) { - - y=y-2 ; - } - else if(_d2==S) { - y=y+2; - } - else if(_d2==E){ + lcd.drawSprite(x_space_ship,y_space_ship,10,12,(int *) space_ship); + + if (_d2==N){ + y= y-2; + } + + else if (_d2==E){ bullet_fired=1; x_bullet=11; y_bullet=y; - } - if(y>=40){ y=40;} - else if(y <= 0){ y = 0;} - - if(bullet_fired==1){ + } + + if (y>=40){ + y=40; + } + + else if(y<=0){ + y =0; + } + + if(bullet_fired==1){ x_bullet+=4; - } - if(x_bullet>=x_alien && y_bullet >=y_alien && y_bullet <=y_alien+10){ + } + + if(x_bullet>=x_alien && y_bullet >=y_alien && y_bullet <=y_alien+10) + { y=0; } - //if (x_bullet == x_alien && y_bullet ==y_alien) - // score ++; - wait(1.0f/fps); + + wait(1.0f/fps); lcd.refresh(); - } -} - -// initialies all classes and libraries -void init() -{ + + + } + } + + + // initialies all classes and libraries +void init() { // need to initialise LCD and Gamepad lcd.init(); - pad.init(); - - space.init(space_ship_width,space_ship_height,bullet_size,alien_size,speed); + pad.init(); + space.init(space_ship_width,space_ship_height,bullet_size,alien_size,speed); + } - -// this function draws each frame on the LCD -void render() -{ + + // this function draws each frame on the LCD +void render(){ // clear screen, re-draw and refresh lcd.clear(); space.draw(lcd); @@ -176,8 +173,7 @@ void welcome() { lcd.printString("SpaceInvaders!",0,1); - lcd.printString(" Press Start ",0,4); - + lcd.printString("Press Start ",0,4); lcd.refresh(); // wait flashing LEDs until start button is pressed @@ -188,4 +184,7 @@ wait(0.1); } + + + } \ No newline at end of file