Laila Al Badwawi 200906179 SpaceInvaders I declare this my own independent work and understand the university rules on plagiarism.

Dependencies:   mbed

Committer:
fy14lkaa
Date:
Sun May 05 15:33:46 2019 +0000
Revision:
127:8bd8cf136f19
Parent:
92:8a1b14488ca5
Child:
128:a3f581b8461c
modified the main.cpp due to the previous changes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fy14lkaa 3:985fab783b92 1 ///////// pre-processor directives ////////
fy14lkaa 0:e2af4c8e462e 2 #include "mbed.h"
fy14lkaa 0:e2af4c8e462e 3 #include "Gamepad.h"
fy14lkaa 0:e2af4c8e462e 4 #include "N5110.h"
fy14lkaa 127:8bd8cf136f19 5 #include "SpaceEngine.h"
fy14lkaa 8:eec0d7fc365f 6
fy14lkaa 127:8bd8cf136f19 7 /////////////// structs /////////////////
fy14lkaa 0:e2af4c8e462e 8 struct UserInput {
fy14lkaa 0:e2af4c8e462e 9 Direction d;
fy14lkaa 0:e2af4c8e462e 10 float mag;
fy14lkaa 0:e2af4c8e462e 11 };
fy14lkaa 127:8bd8cf136f19 12 /////////////// objects ///////////////
fy14lkaa 50:f538885a788b 13 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
fy14lkaa 0:e2af4c8e462e 14 Gamepad pad;
fy14lkaa 127:8bd8cf136f19 15 SpaceEngine space;
fy14lkaa 13:9d6ee753eca6 16
fy14lkaa 3:985fab783b92 17 ///////////// prototypes ///////////////
fy14lkaa 0:e2af4c8e462e 18 void init();
fy14lkaa 0:e2af4c8e462e 19 void update_game(UserInput input);
fy14lkaa 0:e2af4c8e462e 20 void render();
fy14lkaa 0:e2af4c8e462e 21 void welcome();
fy14lkaa 12:45b1249b3d9a 22
fy14lkaa 13:9d6ee753eca6 23 ///////////// functions ////////////////
fy14lkaa 54:095eae44895b 24 int main()
fy14lkaa 54:095eae44895b 25 {
fy14lkaa 12:45b1249b3d9a 26
fy14lkaa 0:e2af4c8e462e 27 int fps = 8; // frames per second
fy14lkaa 54:095eae44895b 28
fy14lkaa 0:e2af4c8e462e 29 init(); // initialise and then display welcome screen...
fy14lkaa 0:e2af4c8e462e 30 welcome(); // waiting for the user to start
fy14lkaa 127:8bd8cf136f19 31
fy14lkaa 127:8bd8cf136f19 32 render(); // first draw the initial frame
fy14lkaa 0:e2af4c8e462e 33 wait(1.0f/fps); // and wait for one frame period
fy14lkaa 54:095eae44895b 34
fy14lkaa 54:095eae44895b 35
fy14lkaa 3:985fab783b92 36 // game loop - read input, update the game state and render the display
fy14lkaa 0:e2af4c8e462e 37 while (1) {
fy14lkaa 54:095eae44895b 38 space.read_input(pad);
fy14lkaa 12:45b1249b3d9a 39 space.update(pad);
fy14lkaa 54:095eae44895b 40 render();
fy14lkaa 127:8bd8cf136f19 41 wait(1.0f/fps);
fy14lkaa 57:45c5de9cefdf 42 }
fy14lkaa 54:095eae44895b 43 }
fy14lkaa 54:095eae44895b 44
fy14lkaa 54:095eae44895b 45 // initialies all classes and libraries
fy14lkaa 54:095eae44895b 46 void init()
fy14lkaa 54:095eae44895b 47 {
fy14lkaa 127:8bd8cf136f19 48 // need to initialise LCD and Gamepad
fy14lkaa 0:e2af4c8e462e 49 lcd.init();
fy14lkaa 54:095eae44895b 50 pad.init();
fy14lkaa 127:8bd8cf136f19 51
fy14lkaa 127:8bd8cf136f19 52 // initialise the game with correct ball and paddle sizes
fy14lkaa 127:8bd8cf136f19 53 space.init(0,10, 10, 10,0,70,10, 1, 1, 1);
fy14lkaa 54:095eae44895b 54
fy14lkaa 3:985fab783b92 55 }
fy14lkaa 54:095eae44895b 56
fy14lkaa 54:095eae44895b 57 // this function draws each frame on the LCD
fy14lkaa 54:095eae44895b 58 void render()
fy14lkaa 54:095eae44895b 59 {
fy14lkaa 0:e2af4c8e462e 60 // clear screen, re-draw and refresh
fy14lkaa 127:8bd8cf136f19 61 lcd.clear();
fy14lkaa 12:45b1249b3d9a 62 space.draw(lcd);
fy14lkaa 0:e2af4c8e462e 63 lcd.refresh();
fy14lkaa 0:e2af4c8e462e 64 }
fy14lkaa 3:985fab783b92 65
fy14lkaa 0:e2af4c8e462e 66 // simple splash screen displayed on start-up
fy14lkaa 127:8bd8cf136f19 67 void welcome() {
fy14lkaa 127:8bd8cf136f19 68
fy14lkaa 127:8bd8cf136f19 69 lcd.printString(" Spaceship! ",0,1);
fy14lkaa 127:8bd8cf136f19 70 lcd.printString(" Press Start ",0,4);
fy14lkaa 0:e2af4c8e462e 71 lcd.refresh();
fy14lkaa 127:8bd8cf136f19 72
fy14lkaa 127:8bd8cf136f19 73 // wait flashing LEDs until start button is pressed
fy14lkaa 0:e2af4c8e462e 74 while ( pad.check_event(Gamepad::START_PRESSED) == false) {
fy14lkaa 0:e2af4c8e462e 75 pad.leds_on();
fy14lkaa 127:8bd8cf136f19 76 wait(0.1);
fy14lkaa 0:e2af4c8e462e 77 pad.leds_off();
fy14lkaa 0:e2af4c8e462e 78 wait(0.1);
fy14lkaa 0:e2af4c8e462e 79 }
fy14lkaa 127:8bd8cf136f19 80
fy14lkaa 127:8bd8cf136f19 81 }
fy14lkaa 54:095eae44895b 82
fy14lkaa 54:095eae44895b 83
fy14lkaa 54:095eae44895b 84