tao lao

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Revision:
3:33bf11645fe1
Parent:
2:4947d6a82971
--- a/main.cpp	Fri Oct 23 16:30:18 2020 -0400
+++ b/main.cpp	Tue Nov 24 05:05:10 2020 +0000
@@ -14,13 +14,18 @@
 #include "map.h"
 #include "graphics.h"
 #include "snake.h"
-
+#include "mbed.h"
+//#include "wave_player.h"
+#include "SDFileSystem.h"
 #include <math.h>
 #include<stdio.h>
 
 #define CITY_HIT_MARGIN 1
 #define CITY_UPPER_BOUND (SIZE_Y-(LANDSCAPE_HEIGHT+MAX_BUILDING_HEIGHT))
-
+int go_right(int x, int y);
+int go_left(int x, int y);
+int go_up(int x, int y);
+int go_down(int x, int y);
 // Helper function declarations
 void playSound(char* wav);
 
@@ -51,12 +56,28 @@
 #define GO_DOWN 6
 #define GAME_OVER 7
 #define FULL_DRAW 8
+#define WON 9
+#define FOODN 10
+#define FOODS 11
+#define FOODW 12
+#define FOODE 13
+
 // Get Actions from User (push buttons & accelerometer)
 // Based on push button and accelerometer inputs, determine which action
 // needs to be performed (may be no action).
 int get_action(GameInputs inputs)
 {
-    return 0;
+    MapItem* N=get_north(snake.head_x,snake.head_y);
+    MapItem* S=get_south(snake.head_x,snake.head_y);
+    MapItem* W=get_east(snake.head_x, snake.head_y);
+    MapItem* E=get_west(snake.head_x, snake.head_y);
+
+    if (E->type==WALL || N->type==WALL || S->type==WALL ||W->type==WALL)   return GAME_OVER;
+    if (inputs.ay >= 0.20) return GO_UP;
+    if (inputs.ay < -0.20) return GO_DOWN;
+    if (inputs.ax < -0.20) return GO_LEFT;
+    if (inputs.ax >= 0.20) return GO_RIGHT;
+    else return NO_ACTION;
 }
 /**
  * Update the game state based on the user action. For example, if the user
@@ -69,7 +90,96 @@
  */
 int update_game(int action)
 {
-    return 0;
+     
+   snake.head_px = snake.head_x;
+   snake.head_py = snake.head_y;   
+    
+    switch(action) {
+        case GO_UP:
+                 snake.head_y -= 1;               
+                 
+               for (int i = 1; i < snake.length-1; i++){   
+                   int oldx = snake.head_px;
+                 int oldy = snake.head_py;          
+                int temx = snake.locations[i].x;
+                int temy = snake.locations[i].y;
+                 snake.locations[i].x = oldx;
+               snake.locations[i].y = oldy;
+                 oldx = temx;
+                 oldy = temy;}
+           if (get_north(snake.head_x,snake.head_y)->type == GOODIE)
+                {map_erase(snake.head_x,snake.head_y-1);
+                 return FULL_DRAW;}
+            break;
+        case GO_LEFT:
+                snake.head_x -= 1;                                
+               for (int i = 1; i < snake.length-1; i++){   
+                   int oldx = snake.head_px;
+                 int oldy = snake.head_py;          
+                int temx = snake.locations[i].x;
+                int temy = snake.locations[i].y;
+                 snake.locations[i].x = oldx;
+               snake.locations[i].y = oldy;
+                 oldx = temx;
+                 oldy = temy;}
+           if (get_west(snake.head_x-1,snake.head_y)->type == GOODIE)
+                {map_erase(snake.head_x,snake.head_y);
+                 return FULL_DRAW;}
+            break;
+                
+ 
+        case GO_DOWN:
+                snake.head_y += 1;
+                                
+               for (int i = 1; i < snake.length-1; i++){   
+                   int oldx = snake.head_px;
+                 int oldy = snake.head_py;          
+                int temx = snake.locations[i].x;
+                int temy = snake.locations[i].y;
+                 snake.locations[i].x = oldx;
+               snake.locations[i].y = oldy;
+                 oldx = temx;
+                 oldy = temy;}
+           if (get_south(snake.head_x,snake.head_y+1)->type == GOODIE)
+                {map_erase(snake.head_x,snake.head_y);
+                 return FULL_DRAW;}
+            break;
+        case GO_RIGHT:
+                snake.head_x += 1;
+                                 
+               for (int i = 1; i < snake.length-1; i++){   
+                   int oldx = snake.head_px;
+                 int oldy = snake.head_py;          
+                int temx = snake.locations[i].x;
+                int temy = snake.locations[i].y;
+                 snake.locations[i].x = oldx;
+               snake.locations[i].y = oldy;
+                 oldx = temx;
+                 oldy = temy;}
+           if (get_here(snake.head_x,snake.head_y)->type == GOODIE)
+                {add_nothing(snake.head_x,snake.head_y);
+                 draw_snake_head(snake.head_x,snake.head_y);
+                snake.score= snake.score + 1;}
+            
+            break;
+           
+        case GAME_OVER:
+         
+            {uLCD.color(RED);
+            uLCD.cls();
+            uLCD.text_width(3);
+            uLCD.text_height(3);
+            uLCD.printf("GAME\nOVER");
+            uLCD.color(GREEN);
+            uLCD.text_width(1);
+            uLCD.text_height(1);
+            uLCD.printf("\n\n\n\n\nScore %d", snake.score);
+            uLCD.color(GREEN);}
+            while(1 == 1);
+            
+            }
+    return NO_RESULT;
+    
 }
 
 /**
@@ -78,6 +188,7 @@
 void draw_upper_status()
 {
     uLCD.line(0, 9, 127, 9, GREEN);
+
 }
 
 /**
@@ -112,7 +223,7 @@
     {
         draw_border();
         int u = 58;
-        int v = 56;
+        int v = 59;
         draw_snake_head(u, v);
         draw_snake_body(u-11, v);
         draw_snake_tail(u-22, v);
@@ -170,6 +281,10 @@
  */
 void init_main_map()
 {
+    uLCD.text_width(1);
+    uLCD.text_height(1);
+    uLCD.printf("Score %d", snake.score);
+    uLCD.color(GREEN);
     // "Random" plants
     Map* map = set_active_map(0);
     for(int i = map_width() + 3; i < map_area(); i += 39) {
@@ -194,7 +309,7 @@
     add_wall(39, 0, VERTICAL, 10);
     pc.printf("Added!\r\n");
 
-
+    
     // Add stairs to chamber (map 1)
     //add_stairs(15, 5, 1, 5, 5);
 
@@ -217,10 +332,11 @@
     // 0. Initialize the maps -- implement this function:
     maps_init();
     init_main_map();
+     playSound("/sd/wavfiles/track.wav");
 
     // Initialize game state
     set_active_map(0);
-    snake.head_x = snake.head_y = 5;
+    snake.head_x = snake.head_y = 5 ;
     // Initial drawing
     draw_game(FULL_DRAW);
     // Main game loop
@@ -257,5 +373,5 @@
 // Plays a wavfile
 void playSound(char* wav)
 {
-    
+   
 }
\ No newline at end of file