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
main.cpp
- Committer:
- henririgby98
- Date:
- 2019-05-08
- Revision:
- 8:3c15554e6723
- Parent:
- 7:e0cab3e73df9
- Child:
- 9:436b88b70fdd
File content as of revision 8:3c15554e6723:
///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "SpaceRebEngine.h"
#define SPACEINVADER_WIDTH 8
#define SPACEINVADER_HEIGHT 11
#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;
SpaceRebEngine spacerebellion;
///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void render();
void welcome();
void Game1Over();
void x1_1();
void x1_2();
void x2_1();
void x2_2();
void x3_1();
void x3_2();
///////////// gloabal variable ////////////////
bool end = false;
float counter = 1.0;
int score = 0;
int speed = MISSILES_SPEED;
int i=0;
int X_1[11][11] = {
{ 1,1,0,0,0,0,0,0,0,0,0 },
{ 1,1,1,0,0,0,0,0,0,0,0 },
{ 0,1,1,1,0,0,0,0,0,0,0 },
{ 0,0,1,1,1,0,0,0,0,0,0 },
{ 0,0,0,1,1,1,0,0,0,0,0 },
{ 0,0,0,0,1,1,1,0,0,0,0 },
{ 0,0,0,0,0,1,1,1,0,0,0 },
{ 0,0,0,0,0,0,1,1,1,0,0 },
{ 0,0,0,0,0,0,0,1,1,1,0 },
{ 0,0,0,0,0,0,0,0,1,1,1 },
{ 0,0,0,0,0,0,0,0,0,1,1 },
};
int X_2[11][11] = {
{ 1,1,0,0,0,0,0,0,0,1,1 },
{ 1,1,1,0,0,0,0,0,1,1,1 },
{ 0,1,1,1,0,0,0,1,1,1,0 },
{ 0,0,1,1,1,0,1,1,1,0,0 },
{ 0,0,0,1,1,1,1,1,0,0,0 },
{ 0,0,0,0,1,1,1,0,0,0,0 },
{ 0,0,0,1,1,1,1,1,0,0,0 },
{ 0,0,1,1,1,0,1,1,1,0,0 },
{ 0,1,1,1,0,0,0,1,1,1,0 },
{ 1,1,1,0,0,0,0,0,1,1,1 },
{ 1,1,0,0,0,0,0,0,0,1,1 },
};
int invader[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 },
};
///////////// functions ////////////////
int main()
{
start:
end = false;
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
wait(1.0f/fps); // and wait for one frame period
// game loop - read input, update the game state and render the display
while (!end) {
spacerebellion.read_input(pad);
spacerebellion.update(pad);
render();
end = spacerebellion.game_end();
wait(1.0f/fps);
counter = counter + 1/8;
}
lcd.clear();
i = 0;
while(i < 200) {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_BLACK);
lcd.refresh();
i++;
}
lcd.clear();
i = 0;
while(i < 200) {
x1_1();
lcd.refresh();
pad.tone(1200.0,0.2);
i++;
}
i = 0;
while(i < 200) {
x1_2();
lcd.refresh();
pad.tone(1500.0,0.2);
i++;
}
i = 0;
while(i < 200) {
x2_1();
lcd.refresh();
pad.tone(1200.0,0.2);
i++;
}
i = 0;
while(i < 200) {
x2_2();
lcd.refresh();
pad.tone(1500.0,0.2);
i++;
}
i = 0;
while(i < 200) {
x3_1();
lcd.refresh();
pad.tone(1200.0,0.2);
i++;
}
i = 0;
while(i < 200) {
x3_2();
lcd.refresh();
pad.tone(1500.0,0.2);
i++;
}
lcd.clear();
Game1Over();
goto start;
}
// 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);
}
// 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 ",0,1);
lcd.printString(" Rebellion! ",0,2);
lcd.printString(" Press Start...",0,4);
lcd.drawSprite(8,3,8,11,(int *)invader); //adds spaceinvader into top left corner
lcd.drawSprite(WIDTH - 16,3,8,11,(int *)invader); //adds spaceinvader into top right corner
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);
}
}
void Game1Over() {
lcd.printString(" You died! ",0,1);
lcd.printString(" Score =",0,2);
spacerebellion.print_scores(lcd);
lcd.drawSprite(5,3,11,11,(int *)X_2);
lcd.drawSprite(WIDTH - 13,3,11,11,(int *)X_2);
lcd.printString(" Press BACK...",0,4);
lcd.refresh();
// wait flashing LEDs until start button is pressed
while ( pad.check_event(Gamepad::BACK_PRESSED) == false) {
pad.leds_on();
wait(0.1);
pad.leds_off();
wait(0.1);
}
}
void x1_1() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
lcd.drawSprite(WIDTH/2 - 27,HEIGHT/2 - 6,11,11,(int *)X_1);
}
void x1_2() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
lcd.drawSprite(WIDTH/2 - 27,HEIGHT/2 - 6,11,11,(int *)X_2);
}
void x2_1() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
lcd.drawSprite(WIDTH/2 - 6,HEIGHT/2 - 6,11,11,(int *)X_1);
}
void x2_2() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
lcd.drawSprite(WIDTH/2 - 6,HEIGHT/2 - 6,11,11,(int *)X_2);
}
void x3_1() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
lcd.drawSprite(WIDTH/2 + 15,HEIGHT/2 - 6,11,11,(int *)X_1);
}
void x3_2() {
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
lcd.drawSprite(WIDTH/2 + 15,HEIGHT/2 - 6,11,11,(int *)X_2);
}