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:
- 127:8bd8cf136f19
- Parent:
- 92:8a1b14488ca5
- Child:
- 128:a3f581b8461c
--- a/main.cpp Sun May 05 15:30:32 2019 +0000 +++ b/main.cpp Sun May 05 15:33:46 2019 +0000 @@ -2,25 +2,17 @@ #include "mbed.h" #include "Gamepad.h" #include "N5110.h" -#include "SpaceInvadersEngine.h" - +#include "SpaceEngine.h" -/*#ifdef WITH_TESTING -# include "tests.h" -#endif*/ - -// structs // +/////////////// structs ///////////////// struct UserInput { Direction d; float mag; - }; - -////////////// objects /////////////// +/////////////// objects /////////////// N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); Gamepad pad; -SpaceInvadersEngine space; - +SpaceEngine space; ///////////// prototypes /////////////// void init(); @@ -28,142 +20,37 @@ 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 }, - { 0,0,0,1,1,1,1,0,0,0 }, - { 0,0,1,1,1,1,0,1,0,0 }, - { 0,0,1,1,1,1,0,1,0,0 }, - { 0,0,1,1,1,1,0,1,0,0 }, - { 0,0,1,1,1,1,0,1,0,0 }, - { 0,0,1,1,1,1,0,1,0,0 }, - { 0,0,1,1,1,1,0,1,0,0 }, - { 0,0,1,1,1,1,0,1,0,0 }, - { 0,0,1,1,1,1,0,1,0,0 }, - { 0,0,1,1,1,1,0,1,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}, - -}; -*/ - ///////////// 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... welcome(); // waiting for the user to start - - render(); // first draw the initial frame + + render(); // first draw the initial frame wait(1.0f/fps); // and wait for one frame period - int y=0; - - //define the x position of bullet from the space ship - int x_bullet=0; - - //define the y position of the bullet from the space ship - int y_bullet=0; - int y_Alien=10; - int x_Alien=70; - 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(); - - } - - Direction _d2 = pad.get_direction(); - - lcd.drawSprite(x_Alien,y_Alien,12,10,(int *)Alien); - lcd.drawSprite(x_bullet,y_bullet,4,10,(int *)bullet); - 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; + wait(1.0f/fps); } - - 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) { - y=0; - - - wait(1.0f/fps); - - - } - // initialies all classes and libraries void init() { - // need to initialise LCD and Gamepad + // need to initialise LCD and Gamepad lcd.init(); pad.init(); - int space_ship_width = 5; - int space_ship_height = 5; - int bullet_size = 3; - int Alien_size = 5; - int speed = 2; - - space.init(space_ship_width,space_ship_height,bullet_size,Alien_size,speed); + + // initialise the game with correct ball and paddle sizes + space.init(0,10, 10, 10,0,70,10, 1, 1, 1); } @@ -171,27 +58,27 @@ void render() { // clear screen, re-draw and refresh - lcd.clear(); + lcd.clear(); space.draw(lcd); lcd.refresh(); } // simple splash screen displayed on start-up -void welcome() -{ - lcd.printString("SpaceInvaders!",0,1); - lcd.printString("Press Start ",0,4); +void welcome() { + + lcd.printString(" Spaceship! ",0,1); + lcd.printString(" Press Start ",0,4); lcd.refresh(); - - // wait flashing LEDs until start button is pressed + + // wait flashing LEDs until start button is pressed while ( pad.check_event(Gamepad::START_PRESSED) == false) { pad.leds_on(); - wait(0.1); //wait 0.1 seconds + wait(0.1); pad.leds_off(); wait(0.1); } + +} - -} \ No newline at end of file