c语言贪吃蛇代码
时间: 2023-08-24 10:22:52 浏览: 54
以下是一个简单的C语言贪吃蛇的代码,仅供参考:
```c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_X 20
#define MAX_Y 20
int map[MAX_X][MAX_Y];
int snake[MAX_X * MAX_Y][2];
int length = 3;
int direction = 2;
int score = 0;
int speed = 200;
void initMap();
void initSnake();
void drawMap();
void drawSnake();
void updateMap();
void updateSnake();
void move();
int checkGameOver();
int checkEatFood();
void generateFood();
int main()
{
srand((unsigned)time(NULL));
initMap();
initSnake();
generateFood();
drawMap();
drawSnake();
while (1) {
if (_kbhit()) {
char ch = _getch();
if (ch == 'w' || ch == 'W') {
if (direction != 1) {
direction = 0;
}
}
else if (ch == 's' || ch == 'S') {
if (direction != 0) {
direction = 1;
}
}
else if (ch == 'a' || ch == 'A') {
if (direction != 3) {
direction = 2;
}
}
else if (ch == 'd' || ch == 'D') {
if (direction != 2) {
direction = 3;
}
}
}
move();
if (checkGameOver()) {
break;
}
if (checkEatFood()) {
score++;
length++;
generateFood();
speed -= 10;
}
_sleep(speed);
}
printf("Game Over!\n");
printf("Your score is %d\n", score);
return 0;
}
void initMap()
{
for (int i = 0; i < MAX_X; i++) {
for (int j = 0; j < MAX_Y; j++) {
if (i == 0 || i == MAX_X - 1 || j == 0 || j == MAX_Y - 1) {
map[i][j] = -1;
}
else {
map[i][j] = 0;
}
}
}
}
void initSnake()
{
for (int i = 0; i < length; i++) {
snake[i][0] = 3 + i;
snake[i][1] = 3;
map[snake[i][0]][snake[i][1]] = 1;
}
}
void drawMap()
{
for (int i = 0; i < MAX_X; i++) {
for (int j = 0; j < MAX_Y; j++) {
if (map[i][j] == -1) {
printf("#");
}
else if (map[i][j] == 0) {
printf(" ");
}
else if (map[i][j] == 1) {
printf("o");
}
else if (map[i][j] == 2) {
printf("*");
}
}
printf("\n");
}
}
void drawSnake()
{
for (int i = 0; i < length; i++) {
printf("%d, %d\n", snake[i][0], snake[i][1]);
}
}
void updateMap()
{
for (int i = 0; i < MAX_X; i++) {
for (int j = 0; j < MAX_Y; j++) {
if (map[i][j] == 1) {
map[i][j] = 0;
}
}
}
for (int i = 0; i < length; i++) {
map[snake[i][0]][snake[i][1]] = 1;
}
map[snake[length - 1][0]][snake[length - 1][1]] = 0;
}
void updateSnake()
{
int tail[2];
tail[0] = snake[length - 1][0];
tail[1] = snake[length - 1][1];
for (int i = length - 1; i > 0; i--) {
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
if (direction == 0) {
snake[0][1] -= 1;
}
else if (direction == 1) {
snake[0][1] += 1;
}
else if (direction == 2) {
snake[0][0] -= 1;
}
else if (direction == 3) {
snake[0][0] += 1;
}
snake[length - 1][0] = tail[0];
snake[length - 1][1] = tail[1];
}
void move()
{
updateMap();
updateSnake();
system("cls");
drawMap();
}
int checkGameOver()
{
int head_x = snake[0][0];
int head_y = snake[0][1];
if (head_x == 0 || head_x == MAX_X - 1 || head_y == 0 || head_y == MAX_Y - 1) {
return 1;
}
for (int i = 1; i < length; i++) {
if (snake[i][0] == head_x && snake[i][1] == head_y) {
return 1;
}
}
return 0;
}
int checkEatFood()
{
int head_x = snake[0][0];
int head_y = snake[0][1];
if (map[head_x][head_y] == 2) {
return 1;
}
return 0;
}
void generateFood()
{
int x, y;
do {
x = rand() % (MAX_X - 2) + 1;
y = rand() % (MAX_Y - 2) + 1;
} while (map[x][y] != 0);
map[x][y] = 2;
}
```
该代码使用了Windows API的一些函数,因此只能在Windows环境下编译运行。如果你想在其他操作系统下运行,需要将这些函数替换为相应的函数。另外,该代码只是一个简单的示例,还有很多改进的空间。