ELEC2645 (2018/19) / Mbed 2 deprecated el17cr

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
el17cr
Date:
Fri Apr 05 16:30:47 2019 +0000
Parent:
1:45c73b6a7468
Child:
3:5edefa83f8f0
Commit message:
functions organised and ball linked to move with joystick

Changed in this revision

Ball/Ball.cpp Show annotated file Show diff for this revision Revisions of this file
Ball/Ball.h Show annotated file Show diff for this revision Revisions of this file
Ball/ball.cpp Show diff for this revision Revisions of this file
Ball/ball.h Show diff for this revision Revisions of this file
Falldown/Falldown.cpp Show annotated file Show diff for this revision Revisions of this file
Falldown/Falldown.h Show annotated file Show diff for this revision Revisions of this file
Ground/Ground.cpp Show annotated file Show diff for this revision Revisions of this file
Ground/Ground.h Show annotated file Show diff for this revision Revisions of this file
MyClass/MyClass.cpp Show diff for this revision Revisions of this file
MyClass/MyClass.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ball/Ball.cpp	Fri Apr 05 16:30:47 2019 +0000
@@ -0,0 +1,59 @@
+#include "Ball.h"
+
+Ball::Ball()
+{
+
+}
+
+Ball::~Ball()
+{
+
+}
+
+
+void Ball::init(int y, int height, int width)
+{
+    
+    
+    _y = y; // y value fixed
+
+    _x = WIDTH/2 -  width/2;
+    _height = height;
+    _width = width;
+    _speed = 1;
+    
+    
+}
+
+void Ball::draw(N5110 &lcd)
+{
+    lcd.drawRect(_x,_y,_width,_height,FILL_TRANSPARENT);
+}
+
+void Ball::update(Direction d,float mag)
+{
+  
+
+    _speed = int(mag*10.0f);  // scale is arbitrary, could be changed in future
+
+    // update y value depending on direction of movement
+    // North is decrement as origin is at the top-left so decreasing moves up
+    if (d == W) {
+        _x-=_speed;
+    } else if (d == E) {
+        _x+=_speed;
+    }
+
+    // check the y origin to ensure that the paddle doesn't go off screen
+    if (_x < 1) {
+        _x = 1;
+    }
+    if (_x > WIDTH - _width - 1) {
+        _x = WIDTH - _width - 1;
+    }
+}
+
+Vector2D Ball::get_pos() {
+    Vector2D p = {_x,_y};
+    return p;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ball/Ball.h	Fri Apr 05 16:30:47 2019 +0000
@@ -0,0 +1,32 @@
+#ifndef BALL_H
+#define BALL_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+
+
+class Ball
+{
+    
+public:
+     Ball();
+    ~Ball();
+    void init(int y,int height,int width);
+    void draw(N5110 &lcd);
+    void update(Direction d,float mag);
+    Vector2D get_pos();
+    
+    
+private:
+
+    int _height;
+    int _width;
+    int _x;
+    int _y;
+    int _speed;
+};
+#endif
+    
+    
+    
\ No newline at end of file
--- a/Ball/ball.cpp	Tue Mar 19 10:53:05 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-#include ball.h
-
-Ball::Ball()
-{
-
-}
-
-Ball::~Ball()
-{
-
-}
-
-
-void Ball::init(int size,int speed)
-{
-    
-    _size = size;
-
-    _x = WIDTH/2 -  _size/2;
-    _y = HEIGHT/2 - _size/2;
-    
-
-
-
-
-//Direction d = joystick.get_direction();
\ No newline at end of file
--- a/Ball/ball.h	Tue Mar 19 10:53:05 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-#ifndef BALL_H
-#define BALL_H
-
-#include "mbed.h"
-#include "N5110.h"
-#include "Gamepad.h"
-#include "Joystick.h"
-
-
-class ball
-{
-    
-public:
-     Ball();
-    ~Ball();
-    void init(int size,int speed);
-    
-private:
-    int _size;
-    int _x;
-    int _y;
-};
-#endif
-    
-    
-    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Falldown/Falldown.cpp	Fri Apr 05 16:30:47 2019 +0000
@@ -0,0 +1,178 @@
+#include "Falldown.h"
+
+Falldown::Falldown()
+{
+
+}
+
+Falldown::~Falldown()
+{
+
+}
+
+void Falldown::init(int ground_width,int ground_height,int ball_width,int ball_height)
+{
+    // initialise the game parameters
+    _ground_width = ground_width;
+    _ground_height = ground_height;
+    _ball_width = ball_width;
+    _ball_height = ball_height;
+    
+    
+    
+
+    // x position on screen - WIDTH is defined in N5110.h
+    //_p1x = GAP;
+    //_p2x = WIDTH - GAP - _Ground_width;
+    _bally = GAP;
+    
+
+    // puts Grounds and ball in middle
+    _ball.init(_bally,_ball_height,_ball_width);
+    
+    _ground.init(_ground_height,_ground_width);
+}
+
+void Falldown::read_input(Gamepad &pad)
+{
+    _d = pad.get_direction();
+    _mag = pad.get_mag();
+}
+
+void Falldown::draw(N5110 &lcd)
+{
+    // draw the elements in the LCD buffer
+    // pitch
+    //lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
+    //lcd.drawLine(WIDTH/2,0,WIDTH/2,HEIGHT-1,2);
+    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
+   
+    // Grounds
+    _ground.draw(lcd);
+    _ball.draw(lcd);
+}
+
+void Falldown::update(Gamepad &pad)
+{
+    //check_goal(pad);
+    // important to update Grounds and ball before checking collisions so can
+    // correct for it before updating the display
+    _ball.update(_d,_mag);
+    
+    //_ground.update();
+
+    //check_wall_collision(pad);
+    //check_Ground_collisions(pad);
+}
+
+/*
+void Falldown::check_wall_collision(Gamepad &pad)
+{
+    // read current ball attributes
+    Vector2D ball_pos = _ball.get_pos();
+    Vector2D ball_velocity = _ball.get_velocity();
+
+    // check if hit top wall
+    if (ball_pos.y <= 1) {  //  1 due to 1 pixel boundary
+        ball_pos.y = 1;  // bounce off ceiling without going off screen
+        ball_velocity.y = -ball_velocity.y;
+        // audio feedback
+        pad.tone(750.0,0.1);
+    }
+    // check if hit bottom wall
+    else if (ball_pos.y + _ball_size >= (HEIGHT-1) ) { // bottom pixel is 47
+        // hit bottom
+        ball_pos.y = (HEIGHT-1) - _ball_size;  // stops ball going off screen
+        ball_velocity.y = -ball_velocity.y;
+        // audio feedback
+        pad.tone(750.0,0.1);
+    }
+
+    // update ball parameters
+    _ball.set_velocity(ball_velocity);
+    _ball.set_pos(ball_pos);
+}
+
+void Falldown::check_Ground_collisions(Gamepad &pad)
+{
+    // read current ball attributes
+    Vector2D ball_pos = _ball.get_pos();
+    Vector2D ball_velocity = _ball.get_velocity();
+
+    // check p1 first
+    Vector2D p1_pos = _p1.get_pos();
+
+    // see if ball has hit the Ground by checking for overlaps
+    if (
+        (ball_pos.y >= p1_pos.y) && //top
+        (ball_pos.y <= p1_pos.y + _Ground_height) && //bottom
+        (ball_pos.x >= _p1x) && //left
+        (ball_pos.x <= _p1x + _Ground_width)  //right
+    ) {
+        // if it has, fix position and reflect x velocity
+        ball_pos.x = _p1x + _Ground_width;
+        ball_velocity.x = -ball_velocity.x;
+        // audio feedback
+        pad.tone(1000.0,0.1);
+    }
+
+    // check p2 next
+    Vector2D p2_pos = _p2.get_pos();
+
+    // see if ball has hit the Ground by checking for overlaps
+    if (
+        (ball_pos.y >= p2_pos.y) && //top
+        (ball_pos.y <= p2_pos.y + _Ground_height) && //bottom
+        (ball_pos.x + _ball_size >= _p2x) && //left
+        (ball_pos.x + _ball_size <= _p2x + _Ground_width)  //right
+    ) {
+        // if it has, fix position and reflect x velocity
+        ball_pos.x = _p2x - _ball_size;
+        ball_velocity.x = -ball_velocity.x;
+        // audio feedback
+        pad.tone(1000.0,0.1);
+    }
+
+    // write new attributes
+    _ball.set_velocity(ball_velocity);
+    _ball.set_pos(ball_pos);
+}
+
+void Falldown::check_goal(Gamepad &pad)
+{
+    Vector2D ball_pos = _ball.get_pos();
+    // P2 has scored
+    if (ball_pos.x + _ball_size < 0) {
+        _p2.add_score();
+        _ball.init(_ball_size,_speed);
+        pad.tone(1500.0,0.5);
+        pad.leds_on();
+        wait(0.5);
+        pad.leds_off();
+    }
+
+    // P1 has scored
+    if (ball_pos.x > WIDTH) {
+        _p1.add_score();
+        _ball.init(_ball_size,_speed);
+        pad.tone(1500.0,0.5);
+        pad.leds_on();
+        wait(0.5);
+        pad.leds_off();
+    }
+}
+
+void Falldown::print_scores(N5110 &lcd)
+{
+    // get scores from Grounds
+    int p1_score = _p1.get_score();
+    int p2_score = _p2.get_score();
+
+    // print to LCD i
+    char buffer1[14];
+    sprintf(buffer1,"%2d",p1_score);
+    lcd.printString(buffer1,WIDTH/2 - 20,1);  // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
+    char buffer2[14];
+    sprintf(buffer2,"%2d",p2_score);
+    lcd.printString(buffer2,WIDTH/2 + 4,1);
+}*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Falldown/Falldown.h	Fri Apr 05 16:30:47 2019 +0000
@@ -0,0 +1,54 @@
+#ifndef FALLDOWN_H
+#define FALLDOWN_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "Ball.h"
+#include "Ground.h"
+
+#define GAP 2
+
+class Falldown
+{
+
+public:
+    Falldown();
+    ~Falldown();
+
+    void init(int ground_width,int ground_height,int ball_width,int ball_height);
+    void read_input(Gamepad &pad);
+    void update(Gamepad &pad);
+    void draw(N5110 &lcd);
+    
+private:
+
+    //void check_wall_collision(Gamepad &pad);
+    //void check_Ground_collisions(Gamepad &pad);
+    //void check_goal(Gamepad &pad);
+    //void print_scores(N5110 &lcd);
+    
+    Ground _ground;
+    Ball _ball;
+    
+    
+     
+    int _ground_width;
+    int _ground_height;
+    int _ball_width;
+    int _ball_height;
+    
+    
+    // x positions of the Grounds
+    
+    
+    
+    
+    int _bally;
+    
+    Direction _d;
+    float _mag;
+
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ground/Ground.cpp	Fri Apr 05 16:30:47 2019 +0000
@@ -0,0 +1,28 @@
+#include "Ground.h"
+
+
+Ground::Ground()
+{
+
+}
+
+Ground::~Ground()
+{
+
+}
+
+void Ground::init(int height, int width)
+{
+_height = height;
+_width = width;
+}
+
+void Ground::draw(N5110 &lcd)
+{
+    lcd.drawRect(0,10,_width,_height,FILL_BLACK);
+    lcd.drawRect(0,20,_width,_height,FILL_BLACK);
+    lcd.drawRect(0,30,_width,_height,FILL_BLACK);
+    lcd.drawRect(0,40,_width,_height,FILL_BLACK);
+    lcd.drawRect(0,50,_width,_height,FILL_BLACK);
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Ground/Ground.h	Fri Apr 05 16:30:47 2019 +0000
@@ -0,0 +1,30 @@
+#ifndef GROUND_H
+#define GROUND_H
+
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+
+
+
+class Ground
+{
+
+public:
+
+Ground();
+~Ground();
+void init(int height, int width);
+void draw(N5110 &lcd);
+
+
+private:
+
+int _height;
+int _width;
+int _x;
+int _y;
+
+};
+#endif
\ No newline at end of file
--- a/main.cpp	Tue Mar 19 10:53:05 2019 +0000
+++ b/main.cpp	Fri Apr 05 16:30:47 2019 +0000
@@ -7,3 +7,61 @@
 Student ID Number:
 Date:
 */
+
+#include "mbed.h"
+#include "Gamepad.h"
+#include "N5110.h"
+#include "Falldown.h"
+
+#define GROUND_WIDTH 70
+#define GROUND_HEIGHT 2
+#define BALL_WIDTH 3
+#define BALL_HEIGHT 3
+
+
+
+struct UserInput {
+    Direction d;
+    float mag;
+};
+
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  // K64F - pwr from 3V3
+Gamepad pad;
+Falldown falldown;
+
+void init();
+void update_game(UserInput input);
+void render();
+
+int main()
+{
+    init();
+    render();
+    wait(0.1);
+    while (1) {
+        falldown.read_input(pad);
+        falldown.update(pad);
+        render();
+        wait(0.1);
+    }
+}
+
+void init()
+{
+    lcd.init();
+    pad.init();
+    
+    falldown.init(GROUND_WIDTH,GROUND_HEIGHT,BALL_WIDTH,BALL_HEIGHT);
+}
+
+void render()
+{
+    
+    lcd.clear();  
+    //lcd.drawRect(2, 2, 80, 44, FILL_TRANSPARENT);
+    //gr.draw(lcd);
+    //ba.draw(lcd);
+    falldown.draw(lcd);
+    lcd.refresh();
+    
+}
\ No newline at end of file
--- a/mbed.bld	Tue Mar 19 10:53:05 2019 +0000
+++ b/mbed.bld	Fri Apr 05 16:30:47 2019 +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/3a7713b1edbc
\ No newline at end of file