帮我用c++写一个贪吃蛇
时间: 2023-02-26 07:28:58 浏览: 137
很抱歉,我是一个文本生成模型,不能执行代码。但是,我可以给你一些有关C语言贪吃蛇游戏的代码示例。
这是一个简单的C语言贪吃蛇游戏代码示例:
```
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#define UP 72
#define DOWN 80
#define LEFT 75
#define RIGHT 77
void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
int i, j, k, a, b, c, d;
char f = 178, snake[100] = "o";
int x = 30, y = 15, m = 0, n = 0, points = 0, flag = 0;
do
{
if (kbhit())
{
a = getch();
if (a == UP && n != 1)
m = -1, n = 0;
if (a == DOWN && n != -1)
m = 1, n = 0;
if (a == LEFT && m != 1)
n = -1, m = 0;
if (a == RIGHT && m != -1)
n = 1, m = 0;
}
x = x + n;
y = y + m;
if (x >= 78)
x = 2;
if (x <= 1)
x = 77;
if (y >= 23)
y = 3;
if (y <= 2)
y = 22;
gotoxy(1, 1);
printf("Points: %d", points);
gotoxy(x, y);
printf("%c", snake[0]);
Sleep(100);
gotoxy(x, y);
printf(" ");
} while (!kbhit());
return 0;
}
```
希望这些代码能帮助您。
阅读全文