فهرست منبع

sake turns now. and has speed decoupled from direction

tarfeef101 6 سال پیش
والد
کامیت
55615f8a99
2فایلهای تغییر یافته به همراه112 افزوده شده و 14 حذف شده
  1. BIN
      assignments/a1/snake
  2. 112 14
      assignments/a1/snake.cc

BIN
assignments/a1/snake


+ 112 - 14
assignments/a1/snake.cc

@@ -32,8 +32,8 @@ using namespace std;
  */
 const int Border = 1;
 const int BufferSize = 10;
-const int FPS = 30;
-const int width = 800;
+const int FPS = 60;
+const int width = 600;
 const int height = 600;
 
 /*
@@ -74,17 +74,56 @@ class Snake : public Displayable
 	public:
 		virtual void paint(XInfo & xinfo)
     {
-			XFillRectangle(xinfo.display, xinfo.window, xinfo.gc[0], x, y, 25, blockSize);
+      switch(direction)
+      {
+        case 1:
+          XFillRectangle(xinfo.display, xinfo.window, xinfo.gc[0], x, y, blockSize, 25);
+          break;
+
+        case 2:
+          XFillRectangle(xinfo.display, xinfo.window, xinfo.gc[0], x, y, 25, blockSize);
+          break;
+
+        case 3:
+          XFillRectangle(xinfo.display, xinfo.window, xinfo.gc[0], x, y, blockSize, 25);
+          break;
+
+        case 4:
+          XFillRectangle(xinfo.display, xinfo.window, xinfo.gc[0], x, y, 25, blockSize);
+          break;
+      }
 		}
 
 		void move(XInfo &xinfo)
     {
-			x = x + direction;
+      switch(direction)
+      {
+        case 1:
+          y -= speed;
+          break;
+
+        case 2:
+          x += speed;
+          break;
+
+        case 3:
+          y += speed;
+          break;
+
+        case 4:
+          x -= speed;
+          break;
+      }
 
-			if (x < 0 || x > width)
+      if (x > width || y < 0)
       {
-				direction = -direction;
-			}
+        direction += 2;
+      }
+
+      if (x < 0 || y > height)
+      {
+        direction -= 2;
+      }
 
             // ** ADD YOUR LOGIC **
             // Here, you will be performing collision detection between the snake,
@@ -116,20 +155,55 @@ class Snake : public Displayable
 
         }
 
-        void turnLeft()
+        void moveup()
         {
+          if (direction == 4 || direction == 2) direction = 1;
+        }
 
+        void moveright()
+        {
+          if (direction == 1 || direction == 3) direction = 2;
         }
 
-        void turnRight()
+        void movedown()
         {
+          if (direction == 4 || direction == 2) direction = 3;
+        }
 
+        void moveleft()
+        {
+          if (direction == 1 || direction == 3) direction = 4;
+        }
+
+        void turnLeft()
+        {
+          if (direction != 1)
+          {
+            --direction;
+          }
+          else
+          {
+            direction = 4;
+          }
+        }
+
+        void turnRight()
+        {
+          if (direction != 4)
+          {
+            ++direction;
+          }
+          else
+          {
+            direction = 1;
+          }
         }
 
 		Snake(int x, int y): x(x), y(y)
     {
-			direction = 5;
+			direction = 2;
       blockSize = 10;
+      speed = 5;
 		}
 
 	private:
@@ -137,6 +211,7 @@ class Snake : public Displayable
 		int y;
 		int blockSize;
 		int direction;
+    int speed;
 };
 
 class Fruit : public Displayable
@@ -199,7 +274,7 @@ void initX(int argc, char * argv[], XInfo & xInfo)
 
 	hints.x = 100;
 	hints.y = 100;
-	hints.width = 800;
+	hints.width = 600;
 	hints.height = 600;
 	hints.flags = PPosition | PSize;
 
@@ -299,11 +374,34 @@ void handleKeyPress(XInfo & xinfo, XEvent & event)
 	if (i == 1)
   {
 		printf("Got key press -- %c\n", text[0]);
-		if (text[0] == 'q')
+
+    switch(text[0])
     {
-			error("Terminating normally.");
-		}
+      case 'q':
+        error("Terminating normally.");
+        break;
+    }
 	}
+
+  if (key == XK_Up)
+  {
+    snake.moveup();
+  }
+
+  if (key == XK_Right)
+  {
+    snake.moveright();
+  }
+
+  if (key == XK_Down)
+  {
+    snake.movedown();
+  }
+
+  if (key == XK_Left)
+  {
+    snake.moveleft();
+  }
 }
 
 void handleAnimation(XInfo & xinfo, int inside)