贪吃蛇c++语言代码
时间: 2023-11-12 11:57:23 浏览: 115
以下是一个简单的 C++ 代码示例,实现了一个控制台版的贪吃蛇游戏:
```cpp
#include <iostream>
#include <conio.h>
#include <windows.h>
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int 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++)
std::cout << "#";
std::cout << std::endl;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (j == 0)
std::cout << "#";
if (i == y && j == x)
std::cout << "O";
else if (i == fruitY && j == fruitX)
std::cout << "F";
else
{
bool printTail = false;
for (int k = 0; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
std::cout << "o";
printTail = true;
}
}
if (!printTail)
std::cout << " ";
}
if (j == width - 1)
std::cout << "#";
}
std::cout << std::endl;
}
for (int i = 0; i < width + 2; i++)
std::cout << "#";
std::cout << std::endl;
std::cout <<
阅读全文