用c语言写一个贪吃蛇
时间: 2023-02-27 15:42:54 浏览: 86
C语言写的贪吃蛇
好的,这是一个用C语言写的简单的贪吃蛇游戏代码:
```
#include <stdio.h>
#include <stdlib.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 x = 10, y = 10;
int fruitx, fruity;
int score = 0;
int gameover;
int flag;
fruitx = rand() % 20 + 1;
fruity = rand() % 20 + 1;
while(1)
{
if (kbhit())
{
switch (getch())
{
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
}
if (x == fruitx && y == fruity)
{
score += 10;
fruitx = rand() % 20 + 1;
fruity = rand() % 20 + 1;
}
gotoxy(0, 0);
printf("Score: %d", score);
gotoxy(x, y);
printf("O");
gotoxy(fruitx, fruity);
printf("F");
Sleep(100);
}
return 0;
}
```
希望这能帮到你!
阅读全文