Browse Source

added endgame screen

tarfeef101 6 năm trước cách đây
mục cha
commit
9ad1064313
2 tập tin đã thay đổi với 31 bổ sung19 xóa
  1. BIN
      assignments/a1/snake
  2. 31 19
      assignments/a1/snake.cc

BIN
assignments/a1/snake


+ 31 - 19
assignments/a1/snake.cc

@@ -48,6 +48,7 @@ const int blockSize = 25;
 int velocity = 5;
 bool paused = true;
 bool starting = true;
+bool dead = false;
 
 /*
  * Information to draw on the window.
@@ -284,16 +285,14 @@ class Snake : public Displayable
 
       if (sections.front()->getx() > width || sections.front()->gety() < 0)
       {
-        printf("you're shit\n");
-        delete this;
-        exit(0);
+        dead = true;
+        paused = true;
       }
 
       if (sections.front()->getx() < 0 || sections.front()->gety() > height)
       {
-        printf("you're shit\n");
-        delete this;
-        exit(0);
+        dead = true;
+        paused = true;
       }
 
       if (sections.size() <= 3) goto SKIP;
@@ -311,36 +310,32 @@ class Snake : public Displayable
           case 1:
             if (frontx == eachx && (fronty >= eachy && fronty <= (eachy + blockSize * eachlen)))
             {
-              printf("you're shit\n");
-              delete this;
-              exit(0);
+              dead = true;
+              paused = true;
             }
             break;
 
           case 2:
             if ((frontx >= (eachx - eachlen * blockSize) && frontx <= eachx) && fronty == eachy)
             {
-              printf("you're shit\n");
-              delete this;
-              exit(0);
+              dead = true;
+              paused = true;
             }
             break;
 
           case 3:
             if (frontx == eachx && (fronty <= eachy && fronty >= (eachy - blockSize * eachlen)))
             {
-              printf("you're shit\n");
-              delete this;
-              exit(0);
+              dead = true;
+              paused = true;
             }
             break;
 
           case 4:
             if ((frontx <= (eachx - eachlen * blockSize) && frontx >= eachx) && fronty == eachy)
             {
-              printf("you're shit\n");
-              delete this;
-              exit(0);
+              dead = true;
+              paused = true;
             }
             break;
         }
@@ -511,7 +506,6 @@ void initX(int argc, char * argv[], XInfo & xInfo)
 
 void splash(XInfo & xinfo)
 {
-  //char * title = {'S', 'n', 'a', 'k', 'e', '\0'};
   changecolour(xinfo, white);
   XFillRectangle(xinfo.display, xinfo.buffer, xinfo.gc[0], 100, 100, 600, 400);
   changecolour(xinfo, black);
@@ -523,6 +517,19 @@ void splash(XInfo & xinfo)
   XDrawString(xinfo.display, xinfo.buffer, xinfo.gc[0], 265, 320, "Use arrow keys to turn", 22);
   XDrawString(xinfo.display, xinfo.buffer, xinfo.gc[0], 200, 360, "Collect fruit to grow your snake", 32);
 }
+
+void endgame(XInfo & xinfo)
+{
+  changecolour(xinfo, white);
+  XFillRectangle(xinfo.display, xinfo.buffer, xinfo.gc[0], 100, 100, 600, 400);
+  changecolour(xinfo, black);
+  XFontStruct * font = XLoadQueryFont(xinfo.display, "12x24");
+  XSetFont(xinfo.display, xinfo.gc[0], font->fid);
+  XDrawString(xinfo.display, xinfo.buffer, xinfo.gc[0], 347, 200, "Game Over!", 10);
+  XDrawString(xinfo.display, xinfo.buffer, xinfo.gc[0], 340, 240, "Press r to restart", 18);
+  XDrawString(xinfo.display, xinfo.buffer, xinfo.gc[0], 342, 280, "Press q to quit", 15);
+}
+
 /*
  * Function to repaint a display list
  */
@@ -549,6 +556,10 @@ void repaint(XInfo & xinfo)
 	}
 
   if (starting) splash(xinfo);
+  if (dead)
+  {
+    endgame(xinfo);
+  }
 }
 
 void handleKeyPress(XInfo & xinfo, XEvent & event)
@@ -583,6 +594,7 @@ void handleKeyPress(XInfo & xinfo, XEvent & event)
         snake.reset(velocity);
         paused = true;
         starting = true;
+        dead = false;
         break;
     }
 	}