使用c++编写贪吃蛇游戏
时间: 2024-03-03 09:54:01 浏览: 75
C++编写的贪吃蛇游戏
好的,以下是使用c++编写的贪吃蛇游戏的代码:
```c++
#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std;
bool gameover;
const int width = 20;
const int height = 20;
int x, y, fruitx, fruity, score;
int tailx[100], taily[100], ntail;
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;
void Setup()
{
gameover = false;
dir = STOP;
x = width / 2;
y = height / 2;
fruitx = rand() % width;
fruity = rand() % height;
score = 0;
}
void Draw()
{
system("cls"); // 清屏
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O";
else if (i == fruity && j == fruitx)
cout << "F";
else
{
bool print = false;
for (int k = 0; k < ntail; k++)
{
if (tailx[k] == j && taily[k] == i)
{
cout << "o";
print = true;
}
}
if (!print)
cout << " ";
}
if (j == width - 1)
cout << "#";
}
cout << endl;
}
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
cout << "Score:" << score << endl;
}
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'x':
gameover = true;
break;
}
}
}
void Logic()
{
int prevx = tailx[0];
int prevy = taily[0];
int prev2x, prev2y;
tailx[0] = x;
taily[0] = y;
for (int i = 1; i < ntail; i++)
{
prev2x = tailx[i];
prev2y = taily[i];
tailx[i] = prevx;
taily[i] = prevy;
prevx = prev2x;
prevy = prev2y;
}
switch (dir)
{
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
if (x < 0 || x >= width || y < 0 || y >= height)
gameover = true;
for (int i = 0; i < ntail; i++)
{
if (tailx[i] == x && taily[i] == y)
gameover = true;
}
if (x == fruitx && y == fruity)
{
score += 10;
fruitx = rand() % width;
fruity = rand() % height;
ntail++;
}
}
int main()
{
Setup();
while (!gameover)
{
Draw();
Input();
Logic();
Sleep(100); // 控制游戏速度
}
return 0;
}
```
这个贪吃蛇游戏的实现使用了Windows.h库,所以只能在Windows系统中运行。如果想在其他操作系统中运行,需要对部分代码进行修改。
阅读全文