Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 4:d744920089ea
- Parent:
- 2:794ed187ffcd
- Child:
- 5:0da65740cd5e
--- a/main.cpp Sat May 04 16:02:52 2019 +0000
+++ b/main.cpp Sun May 05 13:53:39 2019 +0000
@@ -1,46 +1,86 @@
-
+///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
+#include "SpaceRebEngine.h"
-// y x button
+
+#define SPACEINVADER_WIDTH 2
+#define SPACEINVADER_HEIGHT 8
+#define MISSILES_SIZE 2
+#define MISSILES_SPEED 3
+
+/////////////// structs /////////////////
+struct UserInput {
+ Direction d;
+ float mag;
+};
+/////////////// objects ///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
-int sprite[8][11] = {
- { 0,0,1,0,0,0,0,0,1,0,0 },
- { 0,0,0,1,0,0,0,1,0,0,0 },
- { 0,0,1,1,1,1,1,1,1,0,0 },
- { 0,1,1,0,1,1,1,0,1,1,0 },
- { 1,1,1,1,1,1,1,1,1,1,1 },
- { 1,0,1,1,1,1,1,1,1,0,1 },
- { 1,0,1,0,0,0,0,0,1,0,1 },
- { 0,0,0,1,1,0,1,1,0,0,0 },
-};
+SpaceRebEngine spacerebellion;
+
+///////////// prototypes ///////////////
+void init();
+void update_game(UserInput input);
+void render();
+void welcome();
-int main() {
-
- lcd.init();
- pad.init();
- lcd.setContrast(0.6);
+///////////// functions ////////////////
+int main()
+{
+ int fps = 8; // frames per second
+
+ init(); // initialise and then display welcome screen...
+ welcome(); // waiting for the user to start
- float SUM_x = WIDTH/2-6;
- float SUM_y = HEIGHT/2-4;
-
- while(1) {
-
- lcd.normalMode(); // normal colour mode
- lcd.setBrightness(0.9); // put LED backlight on 50%
+ render(); // first draw the initial frame
+ wait(1.0f/fps); // and wait for one frame period
+
+
+ // game loop - read input, update the game state and render the display
+ while (1) {
+ spacerebellion.read_input(pad);
+ spacerebellion.update(pad);
+ render();
+ wait(1.0f/fps);
+ }
+}
+
+// initialies all classes and libraries
+void init()
+{
+ // need to initialise LCD and Gamepad
+ lcd.init();
+ pad.init();
+
+ // initialise the game with correct ball and paddle sizes
+ spacerebellion.init(SPACEINVADER_WIDTH,SPACEINVADER_HEIGHT,MISSILES_SIZE,MISSILES_SPEED);
+
+}
- lcd.clear();
- pad.get_mapped_coord();
- Vector2D mapped_coord = pad.get_mapped_coord();
- SUM_x = SUM_x + (mapped_coord.x*7);
- SUM_y = SUM_y - (mapped_coord.y*7);
-
- wait(0.0001);
- lcd.drawSprite(SUM_x,SUM_y,8,11,(int *)sprite);
- lcd.refresh();
+// this function draws each frame on the LCD
+void render()
+{
+ // clear screen, re-draw and refresh
+ lcd.clear();
+ spacerebellion.draw(lcd);
+ lcd.refresh();
+}
+
+// simple splash screen displayed on start-up
+void welcome() {
+
+ lcd.printString(" Space Rebellion! ",0,1);
+ lcd.printString(" Press Start ",0,4);
+ lcd.refresh();
+
+ // wait flashing LEDs until start button is pressed
+ while ( pad.check_event(Gamepad::START_PRESSED) == false) {
+ pad.leds_on();
+ wait(0.1);
+ pad.leds_off();
+ wait(0.1);
}
-
-
+
}