欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
貪吃蛇運(yùn)行程序 C語(yǔ)言編寫(xiě)
[打印本頁(yè)]
作者:
穿街過(guò)河
時(shí)間:
2017-5-6 21:03
標(biāo)題:
貪吃蛇運(yùn)行程序 C語(yǔ)言編寫(xiě)
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int snake_len=1;//蛇的長(zhǎng)度
int snake_loc[50][2]={31,12};//整條蛇的位置,最長(zhǎng)為50
int snake_head[2]={31,12};//蛇頭位置,初始值為11,12;
int food[2];//食物位置
char snake_direction='s';
int delay=200; //蛇每delay個(gè)時(shí)間走一步
int eat_flag=0;//1表示吃了食物,0表示未吃
int liv_stat=0;//1表示死了,游戲該結(jié)束了;0表示還活著
void gotoxy(int x, int y)//定位光標(biāo),x為行坐標(biāo),y為列坐標(biāo)
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
void hidden()//隱藏光標(biāo)
{
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;//蛇的長(zhǎng)度
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()//創(chuàng)建窗口
{
gotoxy(0,0);
printf("********************************************************************************");
printf("* * *");
printf("* * *");
printf("* * 分?jǐn)?shù):1 *");
printf("* * 按鍵說(shuō)明: *");
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()//更新分?jǐn)?shù)
{
gotoxy(73,3);
printf("%2d",snake_len);
}
void create_food()//產(chǎn)生食物的位置
{
time_t t;
srand(time(&t));
while(1)
{
food[0]=rand()%62+1;//生成1~62之間的隨機(jī)數(shù),其中random函數(shù)生成0~77之間的隨機(jī)數(shù)
food[1]=rand()%22+1;//生成1~22之間的隨機(jī)數(shù),其中random函數(shù)生成0~17之間的隨機(jī)數(shù)
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()//判定蛇死沒(méi)死
{
if(snake_head[0]<1||snake_head[0]>62||snake_head[1]<1||snake_head[1]>22)liv_stat=1;
}
void eat()//判定蛇吃沒(méi)吃上,并對(duì)根據(jù)方向?qū)ι哳^位置進(jìn)行更新
{
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()//更新存儲(chǔ)蛇位置的數(shù)組
{
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<snake_len;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("繼續(xù)請(qǐng)按y,退出請(qǐng)按n");
while(1)
{
i=getch();
if(i=='y')goto LOOP;
else if(i=='n')break;
}
break;
}
}
}
復(fù)制代碼
作者:
348652560
時(shí)間:
2017-6-15 16:12
非常好
作者:
碩005
時(shí)間:
2017-7-15 12:25
可以運(yùn)行,非常好,,
作者:
tv98
時(shí)間:
2017-11-10 23:10
看不懂,學(xué)習(xí)學(xué)習(xí)
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1