ELEC2645 (2017/18) / Mbed 2 deprecated el17yw

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
RickYu
Date:
Thu Apr 12 22:46:30 2018 +0000
Parent:
1:0ccd71dedda4
Child:
3:1a134243e2f0
Commit message:
create the bullet and rect

Changed in this revision

Gamepad/Gamepad.h Show annotated file Show diff for this revision Revisions of this file
engine/engine.cpp Show annotated file Show diff for this revision Revisions of this file
engine/engine.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
rec/rect.cpp Show annotated file Show diff for this revision Revisions of this file
rec/rect.h Show annotated file Show diff for this revision Revisions of this file
--- a/Gamepad/Gamepad.h	Mon Mar 05 09:39:56 2018 +0000
+++ b/Gamepad/Gamepad.h	Thu Apr 12 22:46:30 2018 +0000
@@ -1,4 +1,4 @@
-#ifndef GAMEPAD_H
+          #ifndef GAMEPAD_H
 #define GAMEPAD_H
 
 #include <bitset>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/engine/engine.cpp	Thu Apr 12 22:46:30 2018 +0000
@@ -0,0 +1,47 @@
+#include "engine.h"
+
+engine::engine()
+{
+
+}
+
+engine::~engine()
+{
+
+}
+
+void engine::init(int rect_height,int speed,int rect_width)
+{
+    // initialise the game parameters
+    _rect_height = rect_height;
+    _rect_width = rect_width;
+    _speed = speed;
+
+   
+
+
+    // puts rects and ball in middle
+    //rect.init(_rectx,_rect_height);
+}
+void engine::draw(N5110 &lcd)
+{
+  
+    _rect.draw(lcd);
+    
+    
+}
+
+void engine::read_input(Gamepad &pad)
+{
+    _d = pad.get_direction();
+    _mag = pad.get_mag();
+}
+
+
+void engine::update(Gamepad &pad)
+{
+    _rect.update(_d,_mag);
+
+   
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/engine/engine.h	Thu Apr 12 22:46:30 2018 +0000
@@ -0,0 +1,42 @@
+#ifndef ENGINE_H
+#define ENGINE_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "rect.h"
+
+
+#define GAP 2
+class engine
+{
+public:
+    engine();
+    ~engine();
+   
+    void init(int rect_height,int speed,int rect_width);
+    void read_input(Gamepad &pad);
+    void update(Gamepad &pad);
+    void draw(N5110 &lcd);
+
+private:
+    rect _rect;
+    
+    int _speed;
+    int _recx;
+    int _rect_height;
+    int _rect_width;
+    
+   
+    
+    
+    Direction _d;
+    float _mag;
+
+
+};
+
+
+
+
+#endif
\ No newline at end of file
--- a/main.cpp	Mon Mar 05 09:39:56 2018 +0000
+++ b/main.cpp	Thu Apr 12 22:46:30 2018 +0000
@@ -1,13 +1,77 @@
 #include "mbed.h"
+#include "stdio.h"
+#include "stdlib.h"
+#include "Gamepad.h"
+#include "N5110.h"
+#include "rect.h"
+#include "engine.h"
 
 DigitalOut gpo(D0);
 DigitalOut led(LED_RED);
 
+
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
+Gamepad pad;
+engine rect;
+
+void init();
+void welcome();
+void render();
+
+
+
 int main()
 {
-    while (true) {
-        gpo = !gpo; // toggle pin
-        led = !led; // toggle led
-        wait(0.2f);
+
+     int fps = 10;
+     
+     init();
+     welcome();
+      
+     //lcd.clear();
+     //lcd.refresh();
+     
+     render();
+     wait(1.0f/fps);
+     
+while(1){
+    
+     rect.read_input(pad);
+     rect.update(pad);
+     render();
+     
+     wait(1.0f/fps);
+ }
+     
+}
+    
+void init()
+{
+    // need to initialise LCD and Gamepad 
+    lcd.init();
+    pad.init();
+     
+   
+}
+void welcome() {
+    
+    lcd.printString("     TXZ    ",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);
     }
-}
\ No newline at end of file
+ }
+ 
+void render()
+{
+    // clear screen, re-draw and refresh
+    lcd.clear();  
+    rect.draw(lcd);
+    lcd.refresh();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rec/rect.cpp	Thu Apr 12 22:46:30 2018 +0000
@@ -0,0 +1,75 @@
+#include "rect.h"
+
+rect::rect()
+{
+
+}
+
+rect::~rect()
+{
+
+}
+
+void rect::init(int x)
+{
+    rect_x = x;  // x value on screen is fixed
+    rect_speed = 1;  // default speed
+   
+
+}
+
+void rect::draw(N5110 &lcd)
+{
+    lcd.drawRect(rect_x,rect_y,4,4,FILL_BLACK);
+    lcd.drawRect(bullet_x,bullet_y,3,3,FILL_BLACK);
+    
+}
+
+void rect::update(Direction d,float mag)
+{
+    
+    rect_speed = int(mag*10.0f);  // scale is arbitrary, could be changed in future
+    
+    bullet_x+=2;
+    wait(0.02);
+    bullet_y = rect_y;
+    
+    if (bullet_x >84){
+        bullet_x = rect_x;
+        }
+    
+    
+    // update y value depending on direction of movement
+    // North is decrement as origin is at the top-left so decreasing moves up
+    if (d == N) {
+        rect_y-=rect_speed;
+    } else if (d == S) {
+        rect_y+=rect_speed;
+    }
+     if (d == W) {
+        rect_x-=rect_speed;
+    } else if (d == E) {
+        rect_x+=rect_speed;
+    }
+
+    // check the y origin to ensure that the paddle doesn't go off screen
+    if (rect_y < 1) {
+        rect_y = 1;
+    }
+    if (rect_y > 44) {
+        rect_y = 44;
+    }
+
+    if (rect_x < 1) {
+        rect_x = 1;
+    }
+    if (rect_x > 80) {
+        rect_x = 80;
+    }
+}
+
+
+Vector2D rect::get_pos() {
+    Vector2D p = {rect_x,rect_y};
+    return p;    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rec/rect.h	Thu Apr 12 22:46:30 2018 +0000
@@ -0,0 +1,30 @@
+#ifndef RECT_H
+#define RECT_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h" 
+class rect{
+
+public:
+   rect();
+   ~rect();
+   void init(int x);
+   void draw(N5110 &lcd);
+   void update(Direction d,float mag);
+   Vector2D get_pos();
+
+private:
+    int rect_x;
+    int rect_y;
+    int rect_speed;
+    
+    int bullet_x;
+    int bullet_y;
+    int bullet_speed;
+   
+    
+};
+    
+    
+#endif
\ No newline at end of file