请用C语言编写出贪吃蛇的代码
时间: 2023-09-05 17:04:51 浏览: 66
### 回答1:
很高兴为您提供代码示例。以下是一个使用C语言编写的简单程序,它模拟了一只猫捉住一只老鼠的过程:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int cat = 1, mouse = 0;
srand(time(NULL));
while (mouse < 100) {
int move = rand() % 10;
if (cat > mouse) {
mouse += move;
printf("老鼠跑了 %d 步,现在在 %d 的位置。\n", move, mouse);
} else {
cat += move;
printf("猫跑了 %d 步,现在在 %d 的位置。\n", move, cat);
}
}
printf("猫抓住了老鼠!\n");
return 0;
}
```
该程序随机生成每个动物的移动距离,如果猫的位置在老鼠的位置之前,它会移动,否则老鼠会移动。如果老鼠的位置达到100,则程序输出“猫抓住了老鼠!”并退出循环。
### 回答2:
贪吃蛇是经典的游戏之一,可以用C语言编写。以下是一个基本的贪吃蛇游戏代码示例:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
#define WIDTH 30
#define HEIGHT 20
int snake[WIDTH*HEIGHT][2];
int length;
int direction;
int food[2];
int gameover;
void gotoxy(int x, int y) {
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void startup() {
length = 2;
snake[0][0] = WIDTH / 2;
snake[0][1] = HEIGHT / 2;
snake[1][0] = snake[0][0] - 1;
snake[1][1] = snake[0][1];
direction = 'w';
food[0] = rand() % WIDTH;
food[1] = rand() % HEIGHT;
gameover = 0;
}
void show() {
int i;
system("cls");
for (i = 0; i < length; i++) {
gotoxy(snake[i][0], snake[i][1]);
printf("*");
}
gotoxy(food[0], food[1]);
printf("#");
}
void updateWithoutInput() {
int i;
for (i = length - 1; i > 0; i--) {
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
switch (direction) {
case 'w':
snake[0][1]--;
break;
case 's':
snake[0][1]++;
break;
case 'a':
snake[0][0]--;
break;
case 'd':
snake[0][0]++;
break;
}
if (snake[0][0] == food[0] && snake[0][1] == food[1]) {
length++;
food[0] = rand() % WIDTH;
food[1] = rand() % HEIGHT;
}
if (snake[0][0] < 0 || snake[0][0] >= WIDTH || snake[0][1] < 0 || snake[0][1] >= HEIGHT) {
gameover = 1;
}
for (i = 1; i < length; i++) {
if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) {
gameover = 1;
}
}
}
void updateWithInput() {
char input;
if (_kbhit()) {
input = _getch();
switch (input) {
case 'w':
case 's':
case 'a':
case 'd':
direction = input;
break;
case 'q':
gameover = 1;
break;
}
}
}
int main() {
while (!gameover) {
show();
updateWithoutInput();
updateWithInput();
Sleep(100);
}
printf("\nGame Over!\n");
return 0;
}
这段代码是一个基本的贪吃蛇游戏的实现,使用了Windows.h库实现了控制台窗口的定位和延迟。代码中使用了数组来表示蛇和食物的位置,实现了蛇的移动和吃食物的功能,同时也实现了游戏结束的判定。在Windows环境下编译运行该代码可以玩贪吃蛇游戏。
### 回答3:
贪吃蛇是一款经典的游戏,下面是用C语言编写的一个简单版本的贪吃蛇代码:
```c
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#include <windows.h>
#define WIDTH 40
#define HEIGHT 20
int x, y; // 蛇头的坐标
int fruitX, fruitY; // 果实的坐标
int score; // 得分
int tailX[100], tailY[100]; // 蛇身的坐标
int tailLength; // 蛇的长度
enum Direction { STOP = 0, UP, DOWN, LEFT, RIGHT };
enum Direction dir;
void Setup() {
// 初始化游戏状态
dir = STOP;
x = WIDTH / 2;
y = HEIGHT / 2;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
score = 0;
tailLength = 0;
}
void Draw() {
system("cls");
// 绘制游戏界面
for (int i = 0; i < WIDTH + 2; i++) {
printf("#");
}
printf("\n");
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (j == 0) {
printf("#");
}
if (i == y && j == x) {
printf("O");
}
else if (i == fruitY && j == fruitX) {
printf("F");
}
else {
bool isTail = false;
for (int k = 0; k < tailLength; k++) {
if (tailX[k] == j && tailY[k] == i) {
printf("o");
isTail = true;
break;
}
}
if (!isTail) {
printf(" ");
}
}
if (j == WIDTH - 1) {
printf("#");
}
}
printf("\n");
}
for (int i = 0; i < WIDTH + 2; i++) {
printf("#");
}
printf("\n");
printf("Score: %d\n", score);
}
void Input() {
// 获取用户输入
if (_kbhit()) {
switch (_getch()) {
case 'w':
dir = UP;
break;
case 's':
dir = DOWN;
break;
case 'a':
dir = LEFT;
break;
case 'd':
dir = RIGHT;
break;
case 'x':
exit(0);
break;
}
}
}
void Logic() {
// 更新游戏逻辑
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < tailLength; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir) {
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
default:
break;
}
if (x >= WIDTH) x = 0;
else if (x < 0) x = WIDTH - 1;
if (y >= HEIGHT) y = 0;
else if (y < 0) y = HEIGHT - 1;
for (int i = 0; i < tailLength; i++) {
if (tailX[i] == x && tailY[i] == y) {
exit(0); // 游戏结束
}
}
if (x == fruitX && y == fruitY) {
score += 10;
fruitX = rand() % WIDTH;
fruitY = rand() % HEIGHT;
tailLength++;
}
}
int main() {
Setup();
while (true) {
Draw();
Input();
Logic();
Sleep(10); // 控制游戏速度
}
return 0;
}
```
这段代码实现了一个简单的贪吃蛇游戏。主要逻辑在`Draw()`、`Input()`和`Logic()`函数中。其中`Draw()`函数用来绘制游戏界面,`Input()`函数用来获取用户输入,`Logic()`函数用来更新游戏状态。`main()`函数通过不断调用这三个函数来实现游戏的运行。游戏界面使用ASCII字符来表示蛇、果实和边框,并通过`for`循环来打印出整个游戏界面。在`Logic()`函数中,根据用户输入来更新蛇头的坐标,并检测是否吃到了果实或碰到了墙壁或自身,根据不同的情况进行相应的操作。这段代码是一个基本的贪吃蛇游戏框架,你可以根据自己的需求进行修改和扩展。