c语言运行出动画代码,c语言代码如何实现贪吃蛇动画

c语言代码实现贪吃蛇动画的方法:首先确定基本思路,蛇每吃一个食物蛇身子就增加一格;然后用UP,DOWN,LEFT,RIGHT控制蛇头的运动,而蛇身子跟着蛇头走;最后每后一格蛇身子下一步走到上一格蛇身子的位置。

c00ad63755aa78898ddb88765cb3a305.png

基本思路:

蛇每吃一个食物蛇身子就增加一格,用UP, DOWN, LEFT, RIGHT控制蛇头的运动,而蛇身子跟着蛇头走,每后一格蛇身子下一步走到上一格蛇身子的位置,以此类推。#include

#include

#include

#define BEG_X 2

#define BEG_Y 1

#define WID 20

#define HEI 20

HANDLE hout;

typedef enum {UP, DOWN, LEFT, RIGHT} DIR;

typedef struct Snake_body

{

COORD pos;//蛇身的位置

struct Snake_body *next;//下一个蛇身

struct Snake_body *prev;//前一个蛇身

}SNAKE, *PSNAKE;

PSNAKE head = NULL;//蛇头

PSNAKE tail = NULL;//蛇尾

//画游戏边框的函数

void DrawBorder()

{

int i, j;

COORD pos = {BEG_X, BEG_Y};

for(i = 0; i < HEI; ++i)

{

SetConsoleCursorPosition(hout, pos);

for(j = 0; j < WID; ++j)

{

if(i == 0)//第一行

{

if(j == 0)

printf("┏");

else if(j == WID - 1)

printf("┓");

else

printf("━");

}

else if(i == HEI - 1)//最后一行

{

if(j == 0)

printf("┗");

else if(j == WID - 1)

printf("┛");

else

printf("━");

}

else if(j == 0 || j == WID - 1)//第一列或最后一列

printf("┃");

else

printf(" ");

}

++pos.Y;

}

}

//添加蛇身的函数

void AddBody(COORD pos)

{

PSNAKE pnew = (PSNAKE)calloc(1, sizeof(SNAKE));

pnew->pos = pos;

if(!head)

{

head = tail = pnew;

}

else

{

pnew->next = head;//新创建蛇身的next指向原先的蛇头

head->prev = pnew;//原先的蛇头的prev指向新创建的蛇身

head = pnew;//把新创建的蛇身作为新的蛇头

}

SetConsoleCursorPosition(hout, head->pos);

printf("◎");

}

//蛇身移动的函数

void MoveBody(DIR dir)

{

PSNAKE ptmp;

COORD pos = head->pos;

switch(dir)

{

case UP:

if(head->pos.Y > BEG_Y + 1)

--pos.Y;

else

return;

break;

case DOWN:

if(head->pos.Y < BEG_Y + HEI - 2)

++pos.Y;

else

return;

break;

case LEFT:

if(head->pos.X > BEG_X + 2)

pos.X -= 2;

else

return;

break;

case RIGHT:

if(head->pos.X < BEG_X + (WID - 2) * 2)

pos.X += 2;

else

return;

break;

}

AddBody(pos);//添加了一个新的蛇头

ptmp = tail;//保存当前的蛇尾

tail = tail->prev;

if(tail)

tail->next = NULL;

SetConsoleCursorPosition(hout, ptmp->pos);

printf(" ");

free(ptmp);

}

int main()

{

int ctrl;

DIR dir = RIGHT;//初始蛇的方向是向右的

COORD pos = {BEG_X + 2, BEG_Y + HEI / 2};

system("color 0E");

system("mode con cols=90 lines=30");

hout = GetStdHandle(STD_OUTPUT_HANDLE);

printf(" ------------贪吃蛇的移动------------");

DrawBorder();

//自定义几个蛇的身体

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

pos.X += 2;

AddBody(pos);

//控制蛇的移动

while(ctrl = getch())

{

switch(ctrl)

{

case 'w':

if(dir == DOWN)

continue;

dir = UP;

break;

case 's':

if(dir == UP)

continue;

dir = DOWN;

break;

case 'a':

if(dir == RIGHT)

continue;

dir = LEFT;

break;

case 'd':

if(dir == LEFT)

continue;

dir = RIGHT;

break;

case 'q':

return 0;

}

MoveBody(dir);

}

return 0;

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个简单的贪吃蛇游戏的 C 语言程序代码: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define MAX_LENGTH 100 int width = 20; // 游戏窗口宽度 int height = 20; // 游戏窗口高度 int x, y; // 蛇头坐标 int food_x, food_y; // 食物坐标 int score; // 得分 int tail_x[MAX_LENGTH], tail_y[MAX_LENGTH]; // 身坐标 int tail_length; // 身长度 enum direction {STOP = 0, LEFT, RIGHT, UP, DOWN}; // 方向控制 enum direction dir; void init() { x = width / 2; y = height / 2; food_x = rand() % width; food_y = rand() % height; score = 0; dir = STOP; } 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 == food_y && j == food_x) printf("F"); else { int flag = 0; for (int k = 0; k < tail_length; k++) { if (tail_x[k] == j && tail_y[k] == i) { printf("o"); flag = 1; } } if (!flag) 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 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': dir = STOP; break; case 'q': exit(0); break; } } } void logic() { int prev_x = tail_x[0], prev_y = tail_y[0]; int prev2_x, prev2_y; tail_x[0] = x; tail_y[0] = y; for (int i = 1; i < tail_length; i++) { prev2_x = tail_x[i]; prev2_y = tail_y[i]; tail_x[i] = prev_x; tail_y[i] = prev_y; prev_x = prev2_x; prev_y = prev2_y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; 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 < tail_length; i++) { if (tail_x[i] == x && tail_y[i] == y) { score = 0; tail_length = 0; } } if (x == food_x && y == food_y) { score += 10; food_x = rand() % width; food_y = rand() % height; tail_length++; } } int main() { init(); while (1) { draw(); input(); logic(); Sleep(100); } return 0; } ``` 这个程序使用了 Windows.h 头文件,所以只能在 Windows 系统上运行。如果需要在其他系统上运行,需要将 `system("cls")` 改为相应的清屏命令。 ### 回答2: 贪吃蛇是一款经典的游戏,一般由一个和苹果组成。游戏目标是控制吃苹果,每吃到一个苹果身长度增加一节。如果蛇头撞到身或者撞墙,则游戏结束。 以下是一个简单的贪吃蛇C语言程序代码: ```c #include <stdio.h> #include <conio.h> #include <windows.h> #define WIDTH 20 #define HEIGHT 15 int score; int gameOver; int x, y; // 蛇头的坐标 int fruitX, fruitY; // 苹果的坐标 int tailX[100], tailY[100]; // 身的坐标 int tailLength; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; enum eDirection dir; void Setup() { gameOver = 0; dir = STOP; x = WIDTH / 2; y = HEIGHT / 2; fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; score = 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 { int printTail = 0; for (int k = 0; k < tailLength; k++) { if (tailX[k] == j && tailY[k] == i) { printf("o"); printTail = 1; } } if (!printTail) 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 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = 1; 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 LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; 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) { gameOver = 1; break; } } if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % WIDTH; fruitY = rand() % HEIGHT; tailLength++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(10); // 控制帧率 } return 0; } ``` 这个C语言程序通过控制台绘制了一个贪吃蛇游戏界面。可以使用键盘的ADWS键来控制的移动方向,X键用来退游戏。贪吃蛇移动时,每吃到一个苹果分数增加10分,同时长度也会增加一节。如果蛇头撞到身或者撞墙,则游戏结束。 ### 回答3: 贪吃蛇是一款经典的游戏,在C语言中编写贪吃蛇的程序代码相当有趣。 首先,我们需要定义一些常量,如窗口宽度、高度、身长度等。然后,我们需要定义的结构体,包括蛇头位置、身数组、的长度等成员变量。 接下来,我们需要实现一些基本的功能函数,如初始化蛇头位置、绘制身、处理键盘输入等。在每次游戏循环中,我们需要不断更新的位置,并处理碰撞事件,如蛇头与边界、自身以及食物的碰撞。 当蛇头与食物碰撞时,的长度增加,然后随机生成一个新的食物位置。同时,我们还需要实现一个函数来判断游戏是否结束,即蛇头与边界或自身碰撞。 在游戏主循环中,我们通过不断更新的位置,并根据用户的输入来改变的方向。通过不断刷新屏幕来实现动画效果,让在窗口中移动。 最后,在主函数中调用以上函数,并通过一个循环来控制游戏的进行。当游戏结束时,展示最终得分,并询问玩家是否重新开始游戏。 总之,贪吃蛇C语言程序代码实现起来并不复杂,主要涉及的移动、碰撞检测以及界面的绘制等基本操作。有了上述的框架和思路,我们就可以编写一个简单而有趣的贪吃蛇游戏了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值