Ben Evans University Second Year Project. Game Called Defender.

Dependencies:   mbed

https://os.mbed.com/media/uploads/evanso/84bc1a30759fd6a1e3f1fd1fae3e97c2.png

Hello, soldier, you have been specially selected as the defender of planet earth.

Your mission, if you choose to accept it. Fly around the planet and pulverise invading alien ships for as long as you can. Stop the aliens abducting the innocent people on the ground. Be warned if an alien ship manages to abduct a person and take them to top of the screen, they will no longer move randomly and will begin to hunt you down. This sounds like a challenge you were trained for.

But don’t worry soldier you’re not going into battle empty-handed. Your ship is equipped with a state of the art laser beam that has unlimited ammo and four smart bombs that will destroy anything on the screen. The ship also has three lives so use them wisely.

As time goes on more alien ships will arrive on planet earth increasing the difficulty of your mission. And remember the landscape bellow loops around so if you continually fly in the same direction you go to your original position. Good luck soldier.

Revision:
47:49fa1adc10b4
Parent:
45:fc3238cd28c6
Child:
49:ed569eceeaa4
--- a/GameEngine/GameEngine.cpp	Tue May 19 12:53:39 2020 +0000
+++ b/GameEngine/GameEngine.cpp	Tue May 19 14:57:09 2020 +0000
@@ -1,4 +1,7 @@
 #include "GameEngine.h"
+  
+/** Define acelleromter object */
+FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
 
 GameEngine::GameEngine() {
     //attach ticker to isr 
@@ -16,6 +19,7 @@
     spaceship.init();
     map.init(pad);
     menu.init();
+    accelerometer.init();
     
     // Set seed value 
     srand(pad.read_adc()*64000);
@@ -95,16 +99,17 @@
     lcd.clear();
     
     // creat objects 
-    create_weapons_bullets();
-    create_weapons_smart_bomb();
     spawn_aliens();
     spawn_people();
 
     //If spaceship is destroyed stop joystick input, dont draw spaceship sprite
     if (!spaceship_destroyed_){
-        read_joystick_direction();
+        //read_joystick_direction();
+        read_accelerometer_direction();
         spaceship.movement(d_);
         spaceship.draw(lcd);
+        create_weapons_bullets();
+        create_weapons_smart_bomb();
     }
     
     // Draws objects
@@ -143,6 +148,41 @@
     d_ = pad.get_direction();
 }
 
+void GameEngine::read_accelerometer_direction(){  
+    float roll = accelerometer.get_roll_angle();
+    float pitch = accelerometer.get_pitch_angle();
+    // printf("Pitch = %f  Roll = %f\n", pitch, roll);  
+     
+    //north
+    if(pitch >= -35){
+        if(roll >= 10){
+            d_ = NE;
+        }else if (roll <= -10){
+            d_ = NW;
+        }else {
+            d_ = N;
+        }
+    //south
+    }else if(pitch <= -55){
+        if (roll >= 10){
+            d_ = SE;
+        }else if(roll <= -10){
+            d_ = SW;
+        }else {
+            d_ = S; 
+        }
+    // East/west
+    }else {
+        if (roll >= 10){
+            d_ = E;
+        }else if(roll <= -10){
+            d_ = W;
+        }else{
+            d_ = CENTRE;
+        }
+    }      
+}
+
 void GameEngine::spaceship_lives_leds(){
     pad.leds_off();