C语言贪吃蛇程序怎么运行,贪吃蛇运行程序 C语言编写

#include

#include

#include

#include

#include

#include

int  snake_len=1;//蛇的长度

int  snake_loc[50][2]={31,12};//整条蛇的位置,最长为50

int  snake_head[2]={31,12};//蛇头位置,初始值为11,12;

int  food[2];//食物位置

char snake_direction='s';

int  delay=200; //蛇每delay个时间走一步

int  eat_flag=0;//1表示吃了食物,0表示未吃

int  liv_stat=0;//1表示死了,游戏该结束了;0表示还活着

void gotoxy(int x, int y)//定位光标,x为行坐标,y为列坐标

{

COORD pos = {x,y};

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorPosition(hOut, pos);

}

void hidden()//隐藏光标

{

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_CURSOR_INFO cci;

GetConsoleCursorInfo(hOut,&cci);

cci.bVisible=0;//赋1为显示,赋0为隐藏

SetConsoleCursorInfo(hOut,&cci);

}

void init()//初始化

{

int i;

snake_len=1;//蛇的长度

snake_loc[0][0]=31;//整条蛇的位置

snake_loc[0][1]=12;

snake_head[0]=31;//蛇头位置,初始值为11,12;

snake_head[1]=12;

snake_direction='s';

delay=200;

eat_flag=0;

liv_stat=0;

for(i=1;i<50;i++)

{

snake_loc[i][0]=0;//整条蛇的位置

snake_loc[i][1]=0;

}

}

void create_window()//创建窗口

{

gotoxy(0,0);

printf("********************************************************************************");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *   分数:1     *");

printf("*                                                              *   按键说明:  *");

printf("*                                                              *   上:w       *");

printf("*                                                              *   下:s       *");

printf("*                                                              *   左:a       *");

printf("*                                                              *   右:d       *");

printf("*                                                              *   暂停:空格  *");

printf("*                                                              *   退出:Esc键 *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("********************************************************************************");

}

void update_score()//更新分数

{

gotoxy(73,3);

printf("%2d",snake_len);

}

void create_food()//产生食物的位置

{

time_t t;

srand(time(&t));

while(1)

{

food[0]=rand()%62+1;//生成1~62之间的随机数,其中random函数生成0~77之间的随机数

food[1]=rand()%22+1;//生成1~22之间的随机数,其中random函数生成0~17之间的随机数

if(food[0]!=snake_head[0]&&food[1]!=snake_head[1])break;

}

gotoxy(food[0],food[1]);

printf("*");

}

void direction()

{

char keyhit=0,i;

while(kbhit()!=0)keyhit=getch();

if( ((keyhit=='a') || (keyhit=='d') || (keyhit=='w') || (keyhit=='s')) && (abs(snake_direction/16-keyhit/16)==1) )snake_direction=keyhit;

else if(keyhit==' ')

{

gotoxy(30,24);

system("pause");

gotoxy(30,24);

for(i=0;i<19;i++)printf(" ");

}

else if(keyhit==27)exit(0);

}

void state()//判定蛇死没死

{

if(snake_head[0]<1||snake_head[0]>62||snake_head[1]<1||snake_head[1]>22)liv_stat=1;

}

void eat()//判定蛇吃没吃上,并对根据方向对蛇头位置进行更新

{

switch(snake_direction)

{

case 'w': snake_head[1]--;break;

case 's': snake_head[1]++;break;

case 'a': snake_head[0]--;break;

case 'd': snake_head[0]++;break;

}

if((food[0]==snake_head[0]) && (food[1]==snake_head[1]) )

{

eat_flag=1;

switch(snake_direction)

{

case 'w': snake_head[1]--;break;

case 's': snake_head[1]++;break;

case 'a': snake_head[0]--;break;

case 'd': snake_head[0]++;break;

}

}

}

void show_snake()//更新蛇在屏幕中的位置

{

gotoxy(snake_head[0],snake_head[1]);

printf("*");

gotoxy(snake_loc[snake_len-1][0],snake_loc[snake_len-1][1]);

printf(" ");

}

void update_maxtrix()//更新存储蛇位置的数组

{

int i;

if(eat_flag!=1)

{

for(i=snake_len-1;i>0;i--)

{

snake_loc[i][0]=snake_loc[i-1][0];

snake_loc[i][1]=snake_loc[i-1][1];

}

}

else

{

snake_len++;

if(snake_len>3 && delay>100)delay-=30;

for(i=snake_len-1;i>1;i--)

{

snake_loc[i][0]=snake_loc[i-2][0];

snake_loc[i][1]=snake_loc[i-2][1];

}

snake_loc[1][0]=food[0];

snake_loc[1][1]=food[1];

eat_flag=0;

create_food();

update_score();

}

snake_loc[0][0]=snake_head[0];

snake_loc[0][1]=snake_head[1];

}

void main()

{

LOOP:

system("cls");

init();

create_window();

hidden();

create_food();

while(1)

{

int i;

Sleep(delay);

direction();

eat();

show_snake();

update_maxtrix();

state();

if(liv_stat==1)

{

for(i=1;i

{

gotoxy(snake_loc[i][0],snake_loc[i][1]);

printf(" ");

}

gotoxy(food[0],food[1]);

printf(" ");

gotoxy(30,12);

printf("Game over!\n");

gotoxy(25,13);

printf("继续请按y,退出请按n");

while(1)

{

i=getch();

if(i=='y')goto LOOP;

else if(i=='n')break;

}

break;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
贪吃蛇是一个经典的小游戏,其实现需要使用图形界面或者游戏引擎。如果您希望了解C语言实现的简化版贪吃蛇,可以参考下面的代码: ``` #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define WIDTH 20 // 地图宽度 #define HEIGHT 20 // 地图高度 #define MAX_LENGTH (WIDTH * HEIGHT) // 蛇的最大长度 int snakeX[MAX_LENGTH]; // 蛇的x坐标 int snakeY[MAX_LENGTH]; // 蛇的y坐标 int length = 3; // 蛇的初始长度 int foodX, foodY; // 食物的坐标 int score = 0; // 得分 int direction; // 蛇头的方向:1为上,2为下,3为左,4为右 int speed = 200; // 蛇的速度 void initMap(); // 初始化地图 void initSnake(); // 初始化蛇 void generateFood(); // 生成食物 void move(); // 移动蛇 void updateDirection(); // 更新方向 void gameOver(); // 游戏结束 int main() { initMap(); initSnake(); generateFood(); while (1) { move(); Sleep(speed); } return 0; } void initMap() { system("cls"); for (int i = 0; i < WIDTH + 2; i++) { printf("#"); } printf("\n"); for (int i = 0; i < HEIGHT; i++) { printf("#"); for (int j = 0; j < WIDTH; j++) { printf(" "); } printf("#\n"); } for (int i = 0; i < WIDTH + 2; i++) { printf("#"); } printf("\n"); printf("Score: %d\n", score); } void initSnake() { snakeX = WIDTH / 2; snakeY = HEIGHT / 2; snakeX = snakeX; snakeY = snakeY + 1; snakeX = snakeX; snakeY = snakeY + 1; for (int i = 0; i < length; i++) { printf("@"); gotoxy(snakeX[i] + 1, snakeY[i] + 1); } } void generateFood() { srand((unsigned)time(NULL)); do { foodX = rand() % WIDTH; foodY = rand() % HEIGHT; } while (foodX == snakeX && foodY == snakeY); gotoxy(foodX + 1, foodY + 1); printf("*"); } void move() { if (_kbhit()) { updateDirection(); } int tailX = snakeX[length - 1]; int tailY = snakeY[length - 1]; gotoxy(tailX + 1, tailY + 1); printf(" "); for (int i = length - 1; i > 0; i--) { snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; } switch (direction) { case 1: snakeY--; break; case 2: snakeY++; break; case 3: snakeX--; break; case 4: snakeX++; break; } if (snakeX == foodX && snakeY == foodY) { score++; length++; generateFood(); if (speed > 50) { speed -= 10; } } if (snakeX < 0 || snakeX >= WIDTH || snakeY < 0 || snakeY >= HEIGHT) { gameOver(); return; } for (int i = 1; i < length; i++) { if (snakeX[i] == snakeX && snakeY[i] == snakeY) { gameOver(); return; } } gotoxy(snakeX[0] + 1, snakeY + 1); printf("@"); gotoxy(WIDTH + 4, 2); printf("Score: %d", score); } void updateDirection() { char ch = _getch(); switch (ch) { case 'w': if (direction != 2) { direction = 1; } break; case 's': if (direction != 1) { direction = 2; } break; case 'a': if (direction != 4) { direction = 3; } break; case 'd': if (direction != 3) { direction = 4; } break; } } void gameOver() { system("cls"); printf("Game Over!\n"); printf("Score: %d\n", score); system("pause"); exit(0); } ``` 以上是简化版贪吃蛇C语言代码。在这个代码中,我们使用了Windows API中的`gotoxy()`函数,来实现在控制台上输出蛇和食物。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值