写一个贪吃蛇的c语言代码
时间: 2023-03-09 11:00:34 浏览: 121
我不太擅长写c语言代码,但我可以提供一些参考资料,例如:https://www.dreamincode.net/forums/topic/254465-snake-game-in-c/,https://www.programmingsimplified.com/c-program-snake-ladder-game。
相关问题
写一个贪吃蛇 c语言代码
很高兴回答您的问题。以下是一个简单的贪吃蛇 C 语言代码:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#define width 20
#define height 20
int x, y, fruitX, fruitY, score, gameover;
int tailX[100], tailY[100];
int nTail;
void Setup()
{
gameover = ;
x = width / 2;
y = height / 2;
fruitX = rand() % width;
fruitY = rand() % height;
score = ;
}
void Draw()
{
system("cls");
for (int i = ; i < width + 2; i++)
printf("#");
printf("\n");
for (int i = ; i < height; i++)
{
for (int j = ; j < width; j++)
{
if (j == )
printf("#");
if (i == y && j == x)
printf("O");
else if (i == fruitY && j == fruitX)
printf("F");
else
{
int print = ;
for (int k = ; k < nTail; k++)
{
if (tailX[k] == j && tailY[k] == i)
{
printf("o");
print = 1;
}
}
if (!print)
printf(" ");
}
if (j == width - 1)
printf("#");
}
printf("\n");
}
for (int i = ; i < width + 2; i++)
printf("#");
printf("\n");
printf("Score: %d\n", score);
}
void Input()
{
if (_kbhit())
{
switch (_getch())
{
case 'a':
x--;
break;
case 'd':
x++;
break;
case 'w':
y--;
break;
case 's':
y++;
break;
case 'x':
gameover = 1;
break;
}
}
}
void Logic()
{
int prevX = tailX[];
int prevY = tailY[];
int prev2X, prev2Y;
tailX[] = x;
tailY[] = 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 (_getch())
{
case 'a':
x--;
break;
case 'd':
x++;
break;
case 'w':
y--;
break;
case 's':
y++;
break;
case 'x':
gameover = 1;
break;
}
if (x > width || x < || y > height || y < )
gameover = 1;
for (int i = ; i < nTail; i++)
if (tailX[i] == x && tailY[i] == y)
gameover = 1;
if (x == fruitX && y == fruitY)
{
score += 10;
fruitX = rand() % width;
fruitY = rand() % height;
nTail++;
}
}
int main()
{
Setup();
while (!gameover)
{
Draw();
Input();
Logic();
Sleep(50);
}
return ;
}
发一个贪吃蛇C语言代码
好的,这是一个简单的贪吃蛇C语言代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
#define WIDE 50
#define HEIGHT 20
int score = 0;
int x, y;
int fx, fy;
int map[WIDE][HEIGHT];
void init_map()
{
int i, j;
for(i = 0; i <= WIDE-1; i++)
{
for(j = 0; j <= HEIGHT-1; j++)
{
if(i == 0 || i == WIDE-1 || j == 0 || j == HEIGHT-1)
{
//设置边界
map[i][j] = -1;
}
else
{
//设置食物和蛇的初始位置
map[i][j] = 0;
}
}
}
//设置初始蛇身长度和位置
x = WIDE/2;
y = HEIGHT/2;
map[x][y] = 1;
map[x+1][y] = 2;
map[x+2][y] = 3;
//设置初始食物位置
fx = rand()%(WIDE-2)+1;
fy = rand()%(HEIGHT-2)+1;
map[fx][fy] = 4;
}
void display()
{
int i, j;
for(i = 0; i <= WIDE-1; i++)
{
for(j = 0; j <= HEIGHT-1; j++)
{
if(map[i][j] == -1)
{
printf("#");
}
else if(map[i][j] == 0)
{
printf(" ");
}
else if(map[i][j] > 0 && map[i][j] < 4)
{
printf("*");
}
else if(map[i][j] == 4)
{
printf("$");
}
}
printf("\n");
}
printf("score:%d \n", score);
}
void move()
{
int dir = getch();
switch(dir)
{
case 'w':
x--;
break;
case 's':
x++;
break;
case 'a':
y--;
break;
case 'd':
y++;
break;
}
//判断是否撞墙
if(map[x][y] == -1)
{
printf("Game over\n");
exit(0);
}
//判断是否吃到食物
if(map[x][y] == 4)
{
score++;
//重新设置食物位置
fx = rand()%(WIDE-2)+1;
fy = rand()%(HEIGHT-2)+1;
while(map[fx][fy] != 0)
{
fx = rand()%(WIDE-2)+1;
fy = rand()%(HEIGHT-2)+1;
}
map[fx][fy] = 4;
}
else
{
//没有吃到食物时,设置蛇的位置
int i, j, k;
k = map[x][y] + 1;
map[x][y] = 1;
for(i = 0; i <= WIDE-1; i++)
{
for(j = 0; j <= HEIGHT-1; j++)
{
if(map[i][j] == k)
{
map[i][j] = 0;
}
else if(map[i][j] < k && map[i][j] > 0)
{
map[i][j]++;
}
}
}
}
}
int main()
{
init_map();
while(1)
{
system("cls");
display();
move();
Sleep(100);
}
return 0;
}
```
阅读全文