project for 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Files at this revision

API Documentation at this revision

Comitter:
kblake9
Date:
Wed Nov 25 04:48:22 2020 +0000
Parent:
21:fe15f6311822
Commit message:
final

Changed in this revision

hardware.cpp 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
snake.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r fe15f6311822 -r 33f1a0dff46c hardware.cpp
--- a/hardware.cpp	Wed Nov 25 00:57:48 2020 +0000
+++ b/hardware.cpp	Wed Nov 25 04:48:22 2020 +0000
@@ -57,5 +57,6 @@
     in.b1 = button1.read();
     in.b2 = button2.read();
     in.b3 = button3.read();
+    acc.readXYZGravity(&in.ax,&in.ay,&in.az);
     return in;
 }
diff -r fe15f6311822 -r 33f1a0dff46c main.cpp
--- a/main.cpp	Wed Nov 25 00:57:48 2020 +0000
+++ b/main.cpp	Wed Nov 25 04:48:22 2020 +0000
@@ -14,7 +14,7 @@
 #include "map.h"
 #include "graphics.h"
 #include "snake.h"
-
+#include <string>
 #include <math.h>
 #include<stdio.h>
 
@@ -56,7 +56,28 @@
 // needs to be performed (may be no action).
 int get_action(GameInputs inputs)
 {
-    return 0;
+    if (inputs.b1 == 1) {
+            return ACTION_BUTTON;
+        } else if (inputs.b2 == 1) {
+            return MENU_BUTTON;
+        }
+    if(abs(inputs.ax) > abs(inputs.ay)) {
+        if (inputs.ax > 0) {
+            pc.printf("going right\r\n");
+            return GO_RIGHT;
+        } else {
+            pc.printf("going left\r\n");
+            return GO_LEFT;
+            }
+        } else {
+            if (inputs.ay > 0) {
+                pc.printf("going up\r\n");
+                return GO_UP;
+            } else {
+                pc.printf("going down\r\n");
+                return GO_DOWN;
+            }
+        } 
 }
 /**
  * Update the game state based on the user action. For example, if the user
@@ -69,23 +90,92 @@
  */
 int update_game(int action)
 {
-    return 0;
+    switch(action) {
+        case GO_LEFT:
+            MapItem* collideLeft = get_west(snake.head_x, snake.head_y);
+            if (collideLeft != NULL) {
+            } else {
+                snake.head_px = snake.head_x;
+                snake.head_x -=1;
+                for (int i = 0; i < snake.score + 2; i++) {
+                    if (i == 0) {
+                        snake.locations[i].x -= 1;
+                    } else {
+                        snake.locations[i] = snake.locations[i-1];
+                    }
+                }
+            }
+            break;
+        case GO_RIGHT:
+            MapItem* collideRight = get_east(snake.head_x, snake.head_y);
+            if (collideRight != NULL) {
+            } else {
+                snake.head_px = snake.head_x;
+                snake.head_x +=1;
+                for (int i = 0; i < snake.score + 2; i++) {
+                    if (i == 0) {
+                        snake.locations[i].x += 1;
+                    } else {
+                        snake.locations[i] = snake.locations[i-1];
+                    }
+                }
+            }
+            break;
+        case GO_UP:
+            MapItem* collideUp = get_north(snake.head_x, snake.head_y);
+            if (collideUp != NULL) {
+            } else {
+                snake.head_py = snake.head_y;
+                snake.head_y +=1;
+                for (int i = 0; i < snake.score + 2; i++) {
+                    if (i == 0) {
+                        snake.locations[i].y += 1;
+                    } else {
+                        snake.locations[i] = snake.locations[i-1];
+                    }
+                }
+            }
+            break;
+        case GO_DOWN:
+            MapItem* collideDown = get_south(snake.head_x, snake.head_y);
+            if (collideDown != NULL) {
+            } else {
+                snake.head_py = snake.head_y;
+                snake.head_y -= 1;
+                for (int i = 0; i < snake.score + 2; i++) {
+                    if (i == 0) {
+                        snake.locations[i].y -= 1;
+                    } else {
+                        snake.locations[i] = snake.locations[i-1];
+                    }
+                }
+            }
+            break;
+        case ACTION_BUTTON:
+            break;
+        case MENU_BUTTON:
+            break;
+        default:
+            return 0;
+        }
+        return 8;
 }
 
 /**
  * Draw the upper status bar.
  */
-void draw_upper_status()
-{
-    uLCD.line(0, 9, 127, 9, GREEN);
-}
+void draw_upper_status(){   
+    uLCD.locate(0, 0);
+    uLCD.printf("X: %d, Y: %1d", snake.head_x, snake.head_y);
+} 
 
 /**
  * Draw the lower status bar.
  */
 void draw_lower_status()
 {
-    uLCD.line(0, 118, 127, 118, GREEN);
+    uLCD.locate(0, 15);
+    uLCD.printf("score: %d", snake.score);
 }
 
 /**
@@ -111,11 +201,16 @@
     if(draw_option == FULL_DRAW) 
     {
         draw_border();
-        int u = 58;
-        int v = 56;
-        draw_snake_head(u, v);
-        draw_snake_tail(u-11, v);
-        return;
+        int length = snake.score + 2;
+        for (int i = 0; i < length; i++){
+            if (i == 0) {
+                draw_snake_head(snake.locations[0].x, snake.locations[0].y);
+            } else if (i == length - 1) {
+                draw_snake_tail(snake.locations[i].x, snake.locations[i].y);
+            } else {
+                draw_snake_body(snake.locations[i].x, snake.locations[i].y);
+            }
+        }
     }
     // Iterate over all visible map tiles
     for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles
@@ -225,8 +320,10 @@
     snake.head_x = snake.head_y = 5;
     // Initial drawing
     draw_game(FULL_DRAW);
+    pc.printf("Do we ever draw the whole game?\r\n");
     // Main game loop
     while(1) {
+        pc.printf("in loop now\r\n");
         // Timer to measure game update speed
         Timer t;
         t.start();
@@ -236,10 +333,8 @@
         
         // 2. Determine action (move, act, menu, etc.) -- implement this function:
         int action = get_action(inputs);
-        
         // 3. Update game -- implement this function:
         int result = update_game(action);
-        
         // 3b. Check for game over based on result
         // and if so, handle game over -- implement this.
                 
diff -r fe15f6311822 -r 33f1a0dff46c snake.cpp
--- a/snake.cpp	Wed Nov 25 00:57:48 2020 +0000
+++ b/snake.cpp	Wed Nov 25 04:48:22 2020 +0000
@@ -12,6 +12,10 @@
     s->score = 0;
     s->locations[0].x = 50;
     s->locations[0].y = 50;
-    s->locations[1].x = 40;
-    s->locations[1].y = 40;
+    s->locations[1].x = 39;
+    s->locations[1].y = 50;
+    s->head_x = 5;
+    s->head_y = 5;
+    s->head_px = 5;
+    s->head_py = 5;
 }