Jahanzeb Khan / Mbed 2 deprecated ELEC2645_Project_el19jak

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
jahanzebkhan
Date:
Fri Jun 05 22:51:49 2020 +0000
Parent:
1:0648f0052827
Commit message:
Final Submission. I have read and agreed with Statement of Academic integrity.

Changed in this revision

body/body.cpp Show annotated file Show diff for this revision Revisions of this file
body/body.h Show annotated file Show diff for this revision Revisions of this file
food/food.cpp Show annotated file Show diff for this revision Revisions of this file
food/food.h Show annotated file Show diff for this revision Revisions of this file
functions/functions.cpp Show annotated file Show diff for this revision Revisions of this file
functions/functions.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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/body/body.cpp	Fri Jun 05 22:51:49 2020 +0000
@@ -0,0 +1,279 @@
+#include "body.h"
+
+Body::Body()
+{
+}
+Body::~Body()
+{
+}
+
+void Body::init()
+{
+    sx0 = 12;
+    sx1 = 13;
+    sx2 = 14;
+    sx3 = 15;
+    sx4 = 16;
+    sx5 = 17;
+    sx6 = 18;
+    sx8 = 19;
+    sx9 = 20;
+    
+    sy0 = 6;
+    sy1 = 6;
+    sy2 = 6;
+    sy3 = 6;
+    sy4 = 6;
+    sy5 = 6;
+    sy6 = 6;
+    sy7 = 6;
+    sy8 = 6;
+    sy9 = 6;
+    
+    score = 0;
+    endgame = false;
+    input_check = Button_right;
+}
+
+void Body::game_end(N5110 &lcd){
+    if (21 < sx0 < 63){/**If snake hits vertical line*/
+        if (sy0 == 24){
+            endgame = true;
+        }
+    }
+    if (12 < sy0 < 36){/**if snake hits horizontal line*/
+        if (sx0 == 42){
+            endgame = true;
+        }
+    }
+    if ((sx0 == sx1 && sy0 == sy1) || (sx0 == sx2 && sy0 == sy2) || (sx0 == sx3 && sy0 == sy3) || (sx0 == sx4 && sy0 == sy4) || (sx0 == sx5 && sy0 == sy5) || (sx0 == sx6 && sy0 == sy6) || (sx0 == sx7 && sy0 == sy7) || (sx0 == sx8 && sy0 == sy8) || (sx0 == sx9 && sy0 == sy9))
+    {
+        endgame = true;
+    }
+}
+
+void Body::visual(N5110 &lcd, Gamepad &pad, Food &food)
+{
+    lcd.clear();
+    
+    char buffer[12];
+    sprintf(buffer,"Score = %2d", score);
+    lcd.printString(buffer,0,1);
+    
+    lcd.setPixel(food.fo_x, food.fo_y, true);
+    
+    lcd.drawLine(21,24,63,24,1);
+    lcd.drawLine(42,12,42,36,1);
+    
+    lcd.setPixel(sx0, sy0, true);
+    lcd.setPixel(sx1, sy1, true);
+    lcd.setPixel(sx2, sy2, true);
+    lcd.setPixel(sx3, sy3, true);
+    lcd.setPixel(sx4, sy4, true);
+    lcd.setPixel(sx5, sy5, true);
+    lcd.setPixel(sx6, sy6, true);
+    lcd.setPixel(sx7, sy7, true);
+    lcd.setPixel(sx8, sy8, true);
+    lcd.setPixel(sx9, sy9, true);
+    
+    if (endgame == true){
+        lcd.clear();
+        pad.tone(1500.0,1.0);
+        
+        lcd.drawRect(10,5,64,38,FILL_TRANSPARENT);
+        lcd.printString("YOU DIED", 21, 2);
+        char buffer2[12];
+        sprintf(buffer2,"you got %2d", score);
+    }
+    
+    lcd.refresh();
+}
+
+void Body::user_input(Gamepad &pad)
+{
+        Input input = input_check;
+        
+        if(input != Button_left){
+            if(pad.A_pressed()){
+                input_check = Button_right;
+            }
+        }
+        if(input != Button_right){
+            if(pad.Y_pressed()){
+                input_check = Button_left;
+            }
+        }
+        if(input != Button_top){
+            if(pad.B_pressed()){
+                input_check = Button_bottom;
+            }
+        }
+        if(input != Button_bottom){
+            if(pad.X_pressed()){
+                input_check = Button_top;
+            }
+        }
+        else{
+            input_check = input_check;
+        }
+}
+
+bool Body::endgame_check()
+{
+    return endgame;
+}
+
+void Body::food_eaten(N5110 &lcd, Gamepad &pad, Food &food)
+{
+    if ((sx0 == food.fo_x) && (sy0 == food.fo_y))
+    {
+        score = score + 1;
+        food.chng_food = true;
+        pad.led(1,1);
+        wait(0.2);
+        pad.led(2,1);
+        wait(0.2);
+        pad.led(3,1);
+        wait(0.2);
+        pad.led(4,1);
+        wait(0.2);
+        pad.led(5,1);
+        wait(0.2);
+        pad.led(6,1);
+        wait(0.2);
+        pad.led(1,0);
+        wait(0.2);
+        pad.led(2,0);
+        wait(0.2);
+        pad.led(3,0);
+        wait(0.2);
+        pad.led(4,0);
+        wait(0.2);
+        pad.led(5,0);
+        wait(0.2);
+        pad.led(6,0);
+    }
+}
+
+int Body::score_display()
+{
+    return score;
+}
+
+void Body::movement(){
+    if (input_check == Button_top){
+        sx9 = sx8;
+        sx8 = sx7;
+        sx7 = sx6;
+        sx6 = sx5;
+        sx5 = sx4;
+        sx4 = sx3;
+        sx3 = sx2;
+        sx2 = sx1;
+        sx1 = sx0;
+        sy9 = sy8;
+        sy8 = sy7;
+        sy7 = sy6;
+        sy6 = sy5;
+        sy5 = sy4;
+        sy4 = sy3;
+        sy2 = sy2;
+        sy2 = sy1;
+        sy1 = sy0;    
+        
+        sx0 = sx0;
+        sy0 = sy0 - 1;
+        
+    }
+    if (input_check == Button_right){
+        sx9 = sx8;
+        sx8 = sx7;
+        sx7 = sx6;
+        sx6 = sx5;
+        sx5 = sx4;
+        sx4 = sx3;
+        sx3 = sx2;
+        sx2 = sx1;
+        sx1 = sx0;
+        sy9 = sy8;
+        sy8 = sy7;
+        sy7 = sy6;
+        sy6 = sy5;
+        sy5 = sy4;
+        sy4 = sy3;
+        sy2 = sy2;
+        sy2 = sy1;
+        sy1 = sy0;    
+        
+        sx0 = sx0 + 1;
+        sy0 = sy0;
+    }
+    if (input_check == Button_bottom){
+        sx9 = sx8;
+        sx8 = sx7;
+        sx7 = sx6;
+        sx6 = sx5;
+        sx5 = sx4;
+        sx4 = sx3;
+        sx3 = sx2;
+        sx2 = sx1;
+        sx1 = sx0;
+        sy9 = sy8;
+        sy8 = sy7;
+        sy7 = sy6;
+        sy6 = sy5;
+        sy5 = sy4;
+        sy4 = sy3;
+        sy2 = sy2;
+        sy2 = sy1;
+        sy1 = sy0;    
+        
+        sx0 = sx0 + 1;
+        sy0 = sy0;
+    }
+    if (input_check == Button_left){
+        sx9 = sx8;
+        sx8 = sx7;
+        sx7 = sx6;
+        sx6 = sx5;
+        sx5 = sx4;
+        sx4 = sx3;
+        sx3 = sx2;
+        sx2 = sx1;
+        sx1 = sx0;
+        sy9 = sy8;
+        sy8 = sy7;
+        sy7 = sy6;
+        sy6 = sy5;
+        sy5 = sy4;
+        sy4 = sy3;
+        sy2 = sy2;
+        sy2 = sy1;
+        sy1 = sy0;    
+        
+        sx0 = sx0 + 1;
+        sy0 = sy0;
+    }
+    if (sy0 == 0)
+    {
+        sy0 = 41;
+    }
+    if (sy0 == 42)
+    {
+        sy0 = 1;
+    }
+    if (sx0 == 0)
+    {
+        sy0 = 83;
+    }
+    if (sx0 == 84)
+    {
+        sy0 = 1;
+    }
+}
+ 
+ void Body::trail_delete(N5110 &lcd)
+ {
+    lcd.setPixel(sx9,sy9,false);
+}   
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/body/body.h	Fri Jun 05 22:51:49 2020 +0000
@@ -0,0 +1,91 @@
+#ifndef BODY_H
+#define BODY_H
+
+#include "mbed.h"
+#include "Gamepad.h"
+#include "N5110.h"
+#include "food.h"
+
+class Body
+{
+    
+public:
+    
+    /** Constructor */
+    Body();
+    
+    /** Destructor */
+    ~Body();
+    
+    /** Initialises body of snake */
+    void init();
+    
+    /** checks if user has lost the game */
+    void game_end(N5110 &lcd);
+    
+    /** removes the historical location of pixels to depict movement */
+    void trail_delete(N5110 &lcd);
+    
+    /** takes input from userfor movement */
+    void user_input(Gamepad &pad);
+    
+    /** alters direction of snake based on input */
+    void movement();
+    
+    /** sets a visual verson of the snake on the pad */
+    void visual(N5110 &lcd, Gamepad &pad, Food &food);
+    
+    /** checks if previous food has been consumed */
+    void food_eaten(N5110 &lcd, Gamepad &pad, Food &food);
+    
+    
+    /** defines button input on gamepad */
+    enum Input {
+        Button_top,
+        Button_bottom,
+        Button_right,
+        Button_left,
+    };
+    
+    int score_display();
+    int score;
+    
+    bool endgame_check();
+    bool endgame;
+    
+    int sx0;
+    int sx1;
+    int sx2;
+    int sx3;
+    int sx4;
+    int sx5;
+    int sx6;
+    int sx7;
+    int sx8;
+    int sx9;
+    int sy0;
+    int sy1;
+    int sy2;
+    int sy3;
+    int sy4;
+    int sy5;
+    int sy6;
+    int sy7;
+    int sy8;
+    int sy9;
+    
+    Input input_check;
+    
+private:
+
+
+};
+
+#endif
+    
+    
+    
+    
+    
+    
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/food/food.cpp	Fri Jun 05 22:51:49 2020 +0000
@@ -0,0 +1,25 @@
+#include "food.h"
+
+Food::Food()
+{
+}
+
+Food::~Food()
+{
+}
+
+void Food::init(){
+    fo_x = 16;
+    fo_y = 38;
+    
+    chng_food = false;
+}
+
+void Food::food_location(N5110 &lcd){
+    if (chng_food == true){
+        chng_food = false;
+        lcd.setPixel(fo_x, fo_y, 0);
+        fo_x = rand()%84;
+        fo_y = rand()%48;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/food/food.h	Fri Jun 05 22:51:49 2020 +0000
@@ -0,0 +1,39 @@
+#ifndef FOOD_H
+#define FOOD_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+
+class Food
+{
+    
+public:
+    /** Constructor */
+    Food();
+    
+    /** Destructor */
+    ~Food();
+    
+    /** Initialises food object */
+    void init();
+    
+    /** sets location of food */
+    void food_location(N5110 &lcd);
+    
+    
+    /** All variables are public as need to be used by Body */
+    
+    /** sets location of food */
+    int fo_x;
+    int fo_y;
+    
+    /** changes location of food */
+    int chng_food;
+    
+private:
+    
+    
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/functions/functions.cpp	Fri Jun 05 22:51:49 2020 +0000
@@ -0,0 +1,16 @@
+#include "functions.h"
+
+Functions::Functions()
+{
+}
+
+Functions::~Functions()
+{
+}
+
+void Functions::output_score(N5110 &lcd, Gamepad &pad, Body &body, Food &food)
+{
+    char buffer2[12];
+    sprintf(buffer2,"score = %2d",body.score);
+    lcd.printString(buffer2,0,1);
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/functions/functions.h	Fri Jun 05 22:51:49 2020 +0000
@@ -0,0 +1,25 @@
+#ifndef FUNCTIONS_H
+#define FUNCTIONS_H
+
+#include "mbed.h"
+#include "Gamepad.h"
+#include "N5110.h"
+#include "body.h"
+#include "food.h"
+
+class Functions
+{
+    
+public:
+    /**constructor*/
+    Functions();
+    
+    /**destructor*/
+    ~Functions();
+    
+    void output_score(N5110 &lcd, Gamepad &pad, Body &body, Food &food);
+    
+};
+
+#endif
+    
\ No newline at end of file
--- a/main.cpp	Sat May 30 21:10:30 2020 +0000
+++ b/main.cpp	Fri Jun 05 22:51:49 2020 +0000
@@ -14,14 +14,75 @@
 #include "mbed.h"
 #include "Gamepad.h"
 #include "N5110.h"
+#include "body.h"
+#include "food.h"
+#include "functions.h"
 
 
 // objects
 Gamepad pad;
 N5110 lcd;
+Food food;
+Body body;
+Functions functions;
+
+void visual();
+void start();
+void init();
+void snake_refresh();
 
 int main()
 {
+    init();
+    start();
     
+    for (;;) 
+    {
+        snake_refresh();
+        body.game_end(lcd);
+        body.food_eaten(lcd, pad, food);
+        food.food_location(lcd);
+        body.visual(lcd, pad, food);
+        functions.output_score(lcd, pad, body, food);
+        
+        wait(0.02);
+        
+    }
+}
+
+void init()
+{
+    lcd.init();
+    pad.init();
+    body.init();
+    food.init();
+        
 }
 
+void snake_refresh()
+{
+    body.user_input(pad);
+    body.trail_delete(lcd);
+    body.movement();
+}
+
+void start()
+{
+    while (pad.A_held() == 0)
+    {
+        lcd.setContrast(0.5);
+        lcd.drawCircle(42,24,22,FILL_TRANSPARENT);
+        lcd.printString("Welcome to Snake",25,2);
+        lcd.printString("Hold A to start",26,4);
+        pad.tone(1500.0,1);
+        wait(0.5);
+        pad.tone(750.0,1);
+        wait(0.5);/**Startup tone*/
+        pad.tone(1500.0,1);
+        wait(0.5);
+        pad.tone(750.0,1);
+        
+        lcd.refresh();
+    }
+}
+