C++贪吃蛇游戏

自学C++,勉强实现了初步功能

正在学习中

  1 #include<stdio.h>
  2 #include<graphics.h>
  3 #include<conio.h>
  4 #define WIN_WIDTH  640  //窗口宽度
  5 #define WIN_HEIGHT 480  //窗口高度
  6 #define MAX_SNAKE  100   //蛇的最大长度
  7 bool isGameRunning = true;
  8 bool IsKeyDown(int key)
  9 {
 10     return (GetAsyncKeyState(key) & 0x8000 ? 1 : 0);
 11 }
 12 
 13 
 14 enum Direction    //蛇的方向
 15 {
 16     Up,
 17     Down,
 18     Left,
 19     Right,
 20 };
 21 
 22 
 23 void KeyboardControl()
 24 {
 25     if (IsKeyDown(VK_ESCAPE)) // esc键按下则退出循环结束游戏
 26     {
 27         isGameRunning = false;
 28         return;
 29     }
 30 }
 31 struct Snake_flg   //定义蛇的属性结构
 32 {
 33     int num; //蛇的长度
 34     int dir;  //蛇的方向
 35     int scor;   //分数
 36     int size;
 37     POINT coor[MAX_SNAKE];
 38 }snake;
 39 
 40 struct Food_flg   //定义蛇的食物
 41 {
 42     POINT sit;
 43     int flge;
 44     DWORD color;
 45 }food;
 46 
 47 void GameInit()
 48 {   
 49     srand(GetTickCount());
 50     //初始化蛇
 51     snake.num = 3;
 52     snake.dir = Right;
 53     snake.scor = 0;
 54     snake.size = 10;
 55     snake.coor[2].x = 0;
 56     snake.coor[2].y = 0;
 57     snake.coor[1].x = 0 + snake.size;
 58     snake.coor[1].y = 0;
 59     snake.coor[0].x = 2 * snake.size;;
 60     snake.coor[0].y = 0;
 61     //初始化食物
 62     food.sit.x = rand() % (WIN_WIDTH/10)*10;
 63     food.sit.y = rand() % (WIN_HEIGHT/10)*10;
 64     food.flge = 1;
 65     food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
 66 
 67 }
 68 
 69 void GameDraw()
 70 {
 71     setbkcolor(RGB(192, 192, 192));  //设置背景颜色
 72     cleardevice();
 73     /*画蛇*/
 74     setfillcolor(RGB(255, 128, 0));
 75     for (int i = 0; i < snake.num; i++)
 76     {    
 77         setlinecolor(BLACK);
 78         solidrectangle(snake.coor[i].x, snake.coor[i].y, snake.coor[i].x + snake.size, snake.coor[i].y + snake.size);
 79         printf("%d%d\n", snake.coor[i].x, snake.coor[i].y);
 80     }
 81     /*画食物*/
 82     if (food.flge == 1)
 83     {
 84         setfillcolor(food.color);
 85         fillellipse(food.sit.x, food.sit.y, food.sit.x + 10, food.sit.y + 10);
 86     }
 87     
 88     /*显示分数*/
 89     
 90     char temp[20] = "";
 91     sprintf(temp, "分数:%d", snake.scor);
 92     //setbkmode(TRANSPARENT);
 93     outtextxy(20, 20, temp);
 94     
 95 
 96 }
 97 
 98 void SnakeMOVE()
 99 {
100     for (int i = snake.num - 1; i > 0; i--)  //从最后一节蛇开始,每一节都等于上一节蛇的上一次坐标位置
101     {
102         snake.coor[i].x = snake.coor[i - 1].x;
103         snake.coor[i].y = snake.coor[i - 1].y;
104     }
105 
106     switch (snake.dir)
107     {
108     case Up:
109         snake.coor[0].y -= 10;
110         if (snake.coor[0].y <= 0)
111         {
112             snake.coor[0].y = WIN_HEIGHT;
113         }
114         break;
115     case Down:
116         snake.coor[0].y += 10;
117         if (snake.coor[0].y >= 480)
118         {
119             snake.coor[0].y = 0;
120         }
121         break;
122     case Left:
123         snake.coor[0].x -= 10;
124         if (snake.coor[0].x <= 0)
125         {
126             snake.coor[0].x = WIN_WIDTH;
127         }
128         break;
129     case Right:
130         snake.coor[0].x += 10;
131         if (snake.coor[0].x >= WIN_WIDTH)
132         {
133             snake.coor[0].x = 0;
134         }
135         break;
136     default:
137         break;
138     }
139 }
140 
141 void KeyControl()
142 {
143     /*使用win32的API来获取键盘信息*/
144     if (GetAsyncKeyState(VK_UP) && snake.dir != Down)
145     {
146         snake.dir = Up;
147     }
148     if (GetAsyncKeyState(VK_DOWN) && snake.dir != Up)
149     {
150         snake.dir = Down;
151     }
152     if (GetAsyncKeyState(VK_LEFT) && snake.dir != Right)
153     {
154         snake.dir = Left;
155     }
156     if (GetAsyncKeyState(VK_RIGHT) && snake.dir != Left)
157     {
158         snake.dir = Right; 
159         snake.coor[0].x += 10;
160     }
161 }
162 
163 void EatFood()
164 {
165     if (snake.coor[0].x == food.sit.x && snake.coor[0].y == food.sit.y && food.flge ==1)
166     {
167         snake.num ++;
168         snake.scor += 10;
169         food.flge = 0;
170     }
171 
172     if (food.flge == 0)
173     {
174         food.sit.x = rand() % (WIN_WIDTH / 10) * 10;
175         food.sit.y = rand() % (WIN_HEIGHT / 10) * 10;
176         food.flge = 1;
177         food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
178 
179     }
180 }
181 void Died()
182 {
183     for (int i = 2; i < snake.num; i++)
184     {
185         if (snake.coor[0].x == snake.coor[i].x && snake.coor[0].y == snake.coor[i].y)
186         {
187             outtextxy(320, 240, "Game Over!  Please go to study");
188             _getch();
189             exit(233333333);
190         }
191     }
192 }
193 
194 int main()
195 {
196     initgraph(WIN_WIDTH, WIN_HEIGHT, SHOWCONSOLE);  // 初始化一个图形窗口
197 
198     GameInit();
199     while (isGameRunning)
200     {
201         KeyboardControl(); // 处理键盘操作
202         
203         SnakeMOVE();
204         KeyControl();
205         GameDraw();                  // 处理所有绘制
206         EatFood();
207         Died();
208         Sleep(100);                // 等待30毫秒,控制帧率
209     }
210     closegraph();
211     return 0;
212 }

 

posted @ 2021-06-18 09:39  cn_gzb  阅读(120)  评论(0编辑  收藏  举报