fork download
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <time.h>
  6. using namespace std ;
  7.  
  8. const int N = 23;
  9.  
  10. struct Point {
  11. int x,y;
  12. void set(int X, int Y) {x=X; y=Y;}
  13. };
  14.  
  15. HANDLE hIn, hOut; //I/O 控制器
  16. Point body[32], cookie, v = {-2,0}; //體塊, 食物, 移動向量,
  17. char room[N][N*2+4]; //房間
  18. int delay_time = 100; //延遲時間
  19. int x1=2,y1=1,x2=(N-1)*2,y2=N-1, len=2; //房間邊界, 身長
  20. bool bExit = false; //是否持續遊戲
  21.  
  22. void gotoxy (int x, int y)
  23. {
  24. static COORD c;
  25. c.X = x; c.Y = y;
  26. SetConsoleCursorPosition (hOut, c);
  27. }
  28.  
  29. void draw (int x, int y, char* s)
  30. {
  31. gotoxy (x,y);
  32. cout << s;
  33. }
  34.  
  35. void drawList (char* a, char* b, char* c, int w, int h=1)
  36. {
  37. static char* p = room[0];
  38. for (int i; h--; *p++=*c, *p++=c[1], *p++=0, p++)
  39. for (*p++='\n', *p++=*a, *p++=a[1], *p++=a[2],
  40. i=w-2; i--;) *p++=*b, *p++=b[1];
  41. }
  42. void put_cookie ()
  43. {
  44. cookie.x = 3 + rand()%(N-2) * 2;
  45. cookie.y = 2 + rand()%(N-3);
  46.  
  47. }
  48.  
  49. void init()
  50. {
  51. srand (time(0));
  52. hOut = GetStdHandle (STD_OUTPUT_HANDLE);
  53. hIn = GetStdHandle (STD_INPUT_HANDLE);
  54. HANDLE err = INVALID_HANDLE_VALUE;
  55. if (hIn == err || hOut == err) {
  56. puts ("handle failed");
  57. getch ();
  58. exit (1);
  59. }
  60. drawList (" ┌","─","┐", N);
  61. drawList (" │"," ","│", N, N-2);
  62. drawList (" └","─","┘", N);
  63.  
  64. gotoxy (0,0);
  65. for (int i=0; i<N; i++) cout << room[i];
  66. put_cookie ();
  67.  
  68. body[0].set ((x2-x1)/2, (y2-y1)/2);
  69. body[1].set (body[0].x+1, body[0].y);
  70. }
  71.  
  72. void key_control()
  73. {
  74. static DWORD count;
  75. static INPUT_RECORD ir;
  76.  
  77. ReadConsoleInput (hIn, &ir, 1, &count);
  78. if (!ir.Event.KeyEvent.bKeyDown) return;
  79. switch (ir.Event.KeyEvent.wVirtualKeyCode) {
  80. case VK_UP : if (v.y != 1) v.set(0,-1); break;
  81. case VK_DOWN : if (v.y != -1) v.set(0, 1); break;
  82. case VK_LEFT : if (v.x != 2) v.set(-2,0); break;
  83. case VK_RIGHT: if (v.x != -2) v.set( 2,0); break;
  84. case VK_ESCAPE: bExit = true;
  85. }
  86. }
  87.  
  88. void move_snack()
  89. {
  90. int i;
  91. int& x = body[0].x;
  92. int& y = body[0].y;
  93. for (i=1; i<len; i++) //檢查自身碰撞
  94. if (body[0].x == body[i].x &&
  95. body[0].y == body[i].y ) break;
  96.  
  97. if (i!=len || x<=x1 || x>x2 || y<=y1 || y>y2) {
  98. draw (14,10,"G a m e O v e r");
  99. getch(); bExit = true;
  100. return;
  101. }
  102. if (x == cookie.x && y == cookie.y) { //吃到食物
  103. delay_time -= 2;
  104. if (++len > 30) {
  105. draw (16,10, "Y o u W i n");
  106. getch(); bExit = true;
  107. return;
  108. }put_cookie();
  109. }
  110. else draw (body[len-1].x, body[len-1].y, " ");
  111.  
  112. for (i=len-1; i>0; --i)
  113. body[i] = body[i-1];
  114. x += v.x;
  115. y += v.y;
  116. for (i=0; i<len; ++i)
  117. draw (body[i].x, body[i].y, "█");
  118. }
  119.  
  120.  
  121. int main ()
  122. {
  123. init();
  124. while (!bExit)
  125. {
  126. Sleep (delay_time); if (kbhit()) key_control();
  127. draw (cookie.x, cookie.y, "◎");
  128. move_snack();
  129. Sleep (delay_time); if (kbhit()) key_control();
  130. gotoxy (10,24);
  131. printf ("cookie: (%2d,%2d) head: (%2d,%2d)",
  132. cookie.x, cookie.y, body[0].x, body[0].y);
  133. }
  134. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:21: fatal error: windows.h: No such file or directory
compilation terminated.
stdout
Standard output is empty