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
Revision 8:86cb9a9f8a73, committed 2020-03-26
- Comitter:
- josh_ohara
- Date:
- Thu Mar 26 15:24:49 2020 +0000
- Parent:
- 7:06a2558155f0
- Child:
- 9:8e695df3cc36
- Commit message:
- 6 hours of debugging later.... Have a functioning simple ship
Changed in this revision
--- a/Alien/Alien.cpp Tue Mar 24 16:44:48 2020 +0000
+++ b/Alien/Alien.cpp Thu Mar 26 15:24:49 2020 +0000
@@ -1,23 +1,23 @@
-#include "Alien.h"
+// #include "Alien.h"
-void Alien::init(int y, int size) {
- X = WIDTH/2 - Size/2,
- Y = y,
- Size = size,
- Speed = 0.2,
-}
+// void Alien::init(int y, int size) {
+// X = WIDTH/2 - Size/2,
+// Y = y,
+// Size = size,
+// Speed = 0.2,
+// }
-void Alien::draw(N5110 &lcd) {
- lcd.drawRect(X, Y, Size, Size, FILL_BLACK);
-}
+// void Alien::draw(N5110 &lcd) {
+// lcd.drawRect(X, Y, Size, Size, FILL_BLACK);
+// }
-Vector2D Alien::get_position() {
- Vector2D p = {X,Y};
- return p;
-}
+// Vector2D Alien::get_position() {
+// Vector2D p = {X,Y};
+// return p;
+// }
-void Alien::update() {
- X+=Speed;
-}
+// void Alien::update() {
+// X+=Speed;
+// }
--- a/Alien/Alien.h Tue Mar 24 16:44:48 2020 +0000
+++ b/Alien/Alien.h Thu Mar 26 15:24:49 2020 +0000
@@ -1,21 +1,21 @@
-#include "Pad.h"
-#include "mbed.h"
-#include "N5110.h"
-#include "Gamepad.h"
+// #include "Pad.h"
+// #include "mbed.h"
+// #include "N5110.h"
+// #include "Gamepad.h"
-class Alien
-{
+// class Alien
+// {
-public:
- void init(int y, int size);
- void draw(N5110 &lcd);
- void get_position();
- void update();
+// public:
+// void init(int y, int size);
+// void draw(N5110 &lcd);
+// void get_position();
+// void update();
-public:
- int X;
- int Y;
- int Speed;
- int Size;
-};
\ No newline at end of file
+// public:
+// int X;
+// int Y;
+// int Speed;
+// int Size;
+// };
\ No newline at end of file
--- a/BulletS/BulletS.cpp Tue Mar 24 16:44:48 2020 +0000
+++ b/BulletS/BulletS.cpp Thu Mar 26 15:24:49 2020 +0000
@@ -1,32 +1,32 @@
-#include "BulletS.h"
+// #include "BulletS.h"
-Bullet::Bullet()
-{
-}
+// Bullet::Bullet()
+// {
+// }
-void Bullet::init(int size, int x) {
- Size = size;
- Speed = 0;
- X = x; //x=Middle of the ship
- Y = HEIGHT; //y=Top of the ship Height is ship Height
-}
+// void Bullet::init(int size, int x) {
+// Size = size;
+// Speed = 0;
+// X = x; //x=Middle of the ship
+// Y = HEIGHT; //y=Top of the ship Height is ship Height
+// }
-void Bullet::draw(N5110 &lcd)
-{
- lcd.drawRect(X,Y,Size,Size,FILL_BLACK);
-}
-
-void Bullet::update() {
- if(pad.A_pressed()) {
- Speed = 1; //shoots bullet if pad a pressed
- Y -= Speed;
- }
-}
+// void Bullet::draw(N5110 &lcd)
+// {
+// lcd.drawRect(X,Y,Size,Size,FILL_BLACK);
+// }
+//
+// void Bullet::update() {
+// if(pad.A_pressed()) {
+// Speed = 1; //shoots bullet if pad a pressed
+// Y -= Speed;
+// }
+// }
-Vector2D Bullet::get_position() {
- Vector2D p = {X,Y};
- return p;
-}
+// Vector2D Bullet::get_position() {
+// Vector2D p = {X,Y};
+// return p;
+// }
//void Bullet::set_hit(bool hit) {
// bool Hit == hit;
--- a/BulletS/BulletS.h Tue Mar 24 16:44:48 2020 +0000
+++ b/BulletS/BulletS.h Thu Mar 26 15:24:49 2020 +0000
@@ -1,25 +1,25 @@
-#include "mbed.h"
-#include "N5110.h"
-#include "Gamepad.h"
+// #include "mbed.h"
+// #include "N5110.h"
+// #include "Gamepad.h"
-class Bullet
-{
-public:
- Bullet();
- void init(int size, int x);
- void draw(N5110 &lcd);
- void update();
- Vector2D get_position();
+// class Bullet
+// {
+// public:
+// Bullet();
+// void init(int size, int x);
+// void draw(N5110 &lcd);
+// void update();
+// Vector2D get_position();
// void set_hit(bool hit);
-private:
- int Y;
- int X;
- int Speed;
- int Size;
+// private:
+// int Y;
+// int X;
+// int Speed;
+// int Size;
// bool Hit;
-};
+// };
--- a/Ship/Ship.cpp Tue Mar 24 16:44:48 2020 +0000
+++ b/Ship/Ship.cpp Thu Mar 26 15:24:49 2020 +0000
@@ -1,16 +1,39 @@
#include "Ship.h"
+N5110 lcdShip;
+
+Ship::Ship()
+{
+
+}
+
+Ship::~Ship()
+{
+
+}
+
void Ship::init(int height, int width)
{
X = WIDTH/2 - width/2;
- Y = HEIGHT - Height;
+ Y = HEIGHT - 2;
Height = height;
Width = width;
Speed = 0;
+
+//lcdShip.printString("ShipInit",0,1);
+//lcdShip.refresh();
+//wait(2);
+//char buffer[14];
+//sprintf(buffer,"X = %2d ",X);
+//lcdShip.printString(buffer,0,5);
+//lcdShip.refresh();
+wait(2);
+
}
-void Ship::draw(N5110 &lcd)
-{
+
+void Ship::render(N5110 &lcd)
+{
lcd.drawRect(X,Y,Width,Height,FILL_BLACK);
}
@@ -35,9 +58,19 @@
if (X < 1) {
X = 1;
}
- if (X > HEIGHT - Height - 1) {
- X = HEIGHT - Height - 1;
+ if (X > WIDTH - Width - 1) {
+ X = WIDTH - Width - 1;
}
+
+//char buffer[14];
+//sprintf(buffer,"X = %2d ",X);
+//lcdShip.printString(buffer,0,5);
+//lcdShip.refresh();
+//wait(2);
+
+//lcdShip.printString("ShipUpdate",0,1);
+//lcdShip.refresh();
+//wait(5);
}
Vector2D Ship::get_position() {
@@ -55,9 +88,6 @@
return width;
}
-//void Ship::set_life(bool l) {
- // Life = l;
-//}
\ No newline at end of file
--- a/Ship/Ship.h Tue Mar 24 16:44:48 2020 +0000
+++ b/Ship/Ship.h Thu Mar 26 15:24:49 2020 +0000
@@ -1,3 +1,5 @@
+#ifndef SHIP_H
+#define SHIP_H
#include "mbed.h"
#include "N5110.h"
@@ -7,8 +9,10 @@
{
public:
- void init(int y, int height, int width); //dimensions of the ship without shooter, y position is bottom of the screen
- void draw(N5110 &lcd); //Draws basic rectangle ship
+ Ship();
+ ~Ship();
+ void init(int height, int width); //dimensions of the ship without shooter, y position is bottom of the screen
+ void render(N5110 &lcd); //Draws basic rectangle ship
Vector2D get_position(); //Returns position of ship
void update(Direction d, float mag); //Interface between joystick and ship control
int get_height();
@@ -23,3 +27,5 @@
int Speed; //speed of ship
// bool Life;
};
+
+#endif
\ No newline at end of file
--- a/SpaceInvaderEngine/SpaceInvaderEngine.cpp Tue Mar 24 16:44:48 2020 +0000
+++ b/SpaceInvaderEngine/SpaceInvaderEngine.cpp Thu Mar 26 15:24:49 2020 +0000
@@ -1,25 +1,37 @@
#include "SpaceInvaderEngine.h"
+// N5110 lcd1;
+
SpaceInvaderEngine::SpaceInvaderEngine()
{
}
-void SpaceInvaderEngine::init(int ship_height, int ship_width, int ship_bullet_size, int alien_size) {
+SpaceInvaderEngine::~SpaceInvaderEngine()
+{
+
+}
+
+void SpaceInvaderEngine::init(int ship_height, int ship_width) {
+// int ship_bullet_size, int alien_size) {
S1_height = ship_height;
S1_width = ship_width;
- A1_size = alien_size;
- BS1_size = ship_bullet_size;
+// A1_size = alien_size;
+// BS1_size = ship_bullet_size;
+
+// A1y = BORDER + 5;
+// Vector2D ship_pos = S1.get_position;
+// BS1x = ship_pos.x;
- A1y = BORDER + 5;
- Vector2D ship_pos = S1.get_position;
- BS1x = ship_pos.x;
-
- A1.init(A1y,A1_size);
+// A1.init(A1y,A1_size);
S1.init(S1_height,S1_width);
- BS1.init(BS1_size,BS1x);
+// BS1.init(BS1_size,BS1x);
+
+// lcd1.printString("EngineInit",0,1);
+// lcd1.refresh();
+// wait(5);
}
@@ -28,4 +40,14 @@
D = pad.get_direction();
Mag = pad.get_mag();
}
-
\ No newline at end of file
+
+void SpaceInvaderEngine::render(N5110 &lcd)
+{
+ S1.render(lcd);
+}
+
+void SpaceInvaderEngine::update(Gamepad &pad)
+{
+ S1.update(D,Mag);
+}
+
--- a/SpaceInvaderEngine/SpaceInvaderEngine.h Tue Mar 24 16:44:48 2020 +0000
+++ b/SpaceInvaderEngine/SpaceInvaderEngine.h Thu Mar 26 15:24:49 2020 +0000
@@ -4,9 +4,9 @@
#include "mbed.h"
#include "N5110.h"
#include "Gamepad.h"
-#include "BulletS.h"
+// #include "BulletS.h"
#include "Ship.h"
-#include "Alien.h"
+// #include "Alien.h"
#define BORDER 2
@@ -15,28 +15,27 @@
public:
SpaceInvaderEngine();
- void init(int ship_height, int ship_width, int ship_bullet_size, int alien_size);
+ ~SpaceInvaderEngine();
+ void init(int ship_height, int ship_width);
void read_input(Gamepad &pad);
void update(Gamepad &pad);
- void draw(N5110 &lcd);
+ void render(N5110 &lcd);
private:
- void read_input(Gamepad &pad);
- void draw(N5110 &lcd);
- void check_alien_collision(Gamepad &pad);
+// void check_alien_collision(Gamepad &pad);
- Alien A1;
+// Alien A1;
Ship S1;
- BulletS BS1;
+// BulletS BS1;
int S1_height;
int S1_width;
- int A1_size;
- int BS1_size;
+// int A1_size;
+// int BS1_size;
- int A1y;
- int BS1x;
+// int A1y;
+// int BS1x;
Direction D;
float Mag;
--- a/main.cpp Tue Mar 24 16:44:48 2020 +0000
+++ b/main.cpp Thu Mar 26 15:24:49 2020 +0000
@@ -14,14 +14,76 @@
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
+#include "SpaceInvaderEngine.h"
+#define SHIP_WIDTH 8
+#define SHIP_HEIGHT 2
// objects
Gamepad pad;
N5110 lcd;
+SpaceInvaderEngine SpaceInvader;
+
+// structs
+struct UserInput {
+ Direction d;
+ float mag;
+};
+
+//functions
+void init();
+void draw_game();
+void start_menu();
+void update_game(UserInput input);
int main()
{
+ int fps = 6;
+ init();
+ start_menu();
+
+ draw_game();
+ wait(1.0f/fps);
+
+ while(1) {
+ SpaceInvader.read_input(pad);
+ SpaceInvader.update(pad);
+ draw_game();
+ wait(1.0f/fps);
+ }
}
+void start_menu() {
+ lcd.printString("Space Invaders",0,1);
+ lcd.printString(" Press Start ",0,4);
+ lcd.refresh();
+
+ while ( pad.start_pressed() == false) {
+ lcd.setContrast( pad.read_pot1());
+ pad.leds_on();
+ wait(0.1);
+ pad.leds_off();
+ wait(0.1);
+ }
+
+}
+
+void draw_game()
+{
+ lcd.clear();
+ SpaceInvader.render(lcd);
+ lcd.refresh();
+}
+
+void init()
+{
+ lcd.init();
+ pad.init();
+ SpaceInvader.init(SHIP_HEIGHT,SHIP_WIDTH);
+
+// lcd.printString("MainInit",0,1);
+// lcd.refresh();
+// wait(5);
+}
+
--- a/mbed.bld Tue Mar 24 16:44:48 2020 +0000 +++ b/mbed.bld Thu Mar 26 15:24:49 2020 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file +https://os.mbed.com/users/mbed_official/code/mbed/builds/994bdf8177cb \ No newline at end of file