贪吃蛇报告1 贪吃蛇小游戏
1.1系统分析
贪吃蛇是一款经典的小游戏。一条小蛇在屏幕上不停的游走,吃屏幕上的食物。每吃掉一个食物身体就会变长一个单位。碰到屏幕四周或者自己的身体就结束游戏。每个食物根据颜色不同会得到不同的分数。能吃到多少食物和得到多少分数就要看玩家的本事了。
主要功能:贪吃蛇游戏需要具备以下功能:
游戏欢迎界面。
游戏执行界面,包括计算得分。
游戏结束界面。
1.2 设计说明
1.2.1.预处理
#include
#include
#include
#include
1.2.2.数据类型定义
...
1 贪吃蛇小游戏
1.1系统分析
贪吃蛇是一款经典的小游戏。一条小蛇在屏幕上不停的游走,吃屏幕上的食物。每吃掉一个食物身体就会变长一个单位。碰到屏幕四周或者自己的身体就结束游戏。每个食物根据颜色不同会得到不同的分数。能吃到多少食物和得到多少分数就要看玩家的本事了。
主要功能:贪吃蛇游戏需要具备以下功能:
游戏欢迎界面。
游戏执行界面,包括计算得分。
游戏结束界面。
1.2 设计说明
1.2.1.预处理
#include
#include
#include
#include
1.2.2.数据类型定义
int length=1;//蛇的当前长度,初始值为1
int line[100][2];//蛇的走的路线
int head[2]={40,12};//蛇头
int food[2];//食物的位置
char direction;//蛇运动方向
int x_min=1;
int x_max=77;
int y_min=2;
int y_max=23;//设置蛇的运动区域
int tail_before[2]={40,12};//上一个状态的蛇尾
char direction_before='s';//上一个状态蛇的运动方向
int live_death=1;//死活状态,0死,1活
int eat_flag=0;//吃食物与否的状态。0没吃 1吃了
int max=0;
int delay;//移动延迟时间
1.2.3.主函数
main()
{
int x=0,y=0;
int i;
hidden();//隐藏光标
create_window();
initial();
createfood();
for(direction='s',delay=600;;)
{
get_direction();
eat();
update_line();
live_state();//判断生死状态
if(live_death==1)
{
show_snake();
}
else
break;
Sleep(delay);
}
}
1.2.4.主要函数及功能
1.准备程序
void gotoxy(int x, int y)//x为列坐标,y为行坐标
{
COORD pos = {x,y};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
}
2.初始页面
void hidden()//隐藏光标
{
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=1;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut,&cci);
}
3.分数程序
void update_score()
{
gotoxy(2,1);
printf("我的分数:%d",length);
gotoxy(42,1);
printf("最高记录:%d",max);
}
4.游戏窗口
void create_window()
{
gotoxy(0,0);
printf("╔══════════════════╦═══════════════════╗");
printf("║ ║ ║");
printf("╠══════════════════╩═══════════════════╣");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("╚═════════════════ 小蛇贪吃 ════════════════╝");
}
5.吃食物的变化
void update_line()
{
int i;
if(eat_flag==0)//吃了食物就不用记住上一个状态的蛇尾,否则会被消掉
{
tail_before[0]=line[0][0];//记住上一个状态的蛇尾
tail_before[1]=line[0][1];
for(i=0;ix_min)
break;
}//产生一个偶数横坐标
for(;;)
{
food[1]=rand()%(y_max);
if(food[1]>y_min)
break;
}
for(i=0,flag=0;i=x_max || head[1]<=y_min || head[1]>=y_max || flag==1)
{
system("cls");
create_window();
update_score();
gotoxy(35,12);
printf("GAME OVER !\n");
Sleep(500);
live_death=0;
fp=fopen("highest","w");
fprintf(fp,"%d",max);//保存最高分
}
}
11.小蛇吃食物
void eat()
{
if(head[0]==food[0]&&head[1]==food[1])
{
length++;
line[length-1][0]=head[0];//更新蛇头
line[length-1][1]=head[1];
eat_flag=1;
createfood();
if(length>max)
max=length;
update_score();
if(delay>100)
delay-=30;//加速
}
}
2.3 程序运行截图
2.4源程序代码
#include
#include
#include
#include
int length=1;
int line[100][2];
int head[2]={40,12};
int food[2];
char direction;
int x_min=1;
int x_max=77;
int y_min=2;
int y_max=23;
int tail_before[2]={40,12};
char direction_before='s';
int live_death=1;
int eat_flag=0;
int max=0;
int delay;
void gotoxy(int x, int 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=1;
SetConsoleCursorInfo(hOut,&cci);
}
void update_score()
{
gotoxy(2,1);
printf("我的分数:%d",length);
gotoxy(42,1);
printf("最高记录:%d",max);
}
void create_window()
{
gotoxy(0,0);
printf("╔══════════════════╦═══════════════════╗");
printf("║ ║ ║");
printf("╠══════════════════╩═══════════════════╣");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("║ ║");
printf("╚═════════════════ 小蛇贪吃 ════════════════╝");
}
void update_line()
{
int i;
if(eat_flag==0)
{
tail_before[0]=line[0][0];
tail_before[1]=line[0][1];
for(i=0;ix_min)
break;
}
for(;;)
{
food[1]=rand()%(y_max);
if(food[1]>y_min)
break;
}
for(i=0,flag=0;i=x_max || head[1]<=y_min || head[1]>=y_max || flag==1)
{
system("cls");
create_window();
update_score();
gotoxy(35,12);
printf("GAME OVER !\n");
Sleep(500);
live_death=0;
fp=fopen("highest","w");
fprintf(fp,"%d",max);
}
}
void eat()
{
if(head[0]==food[0]&&head[1]==food[1])
{
length++;
line[length-1][0]=head[0];
line[length-1][1]=head[1];
eat_flag=1;
createfood();
if(length>max)
max=length;
update_score();
if(delay>100)
delay-=30;
}
}
main()
{
int x=0,y=0;
int i;
hidden();
create_window();
initial();
createfood();
for(direction='s',delay=600;;)
{ get_direction();
eat();
update_line();
live_state();
if(live_death==1)
{
show_snake();
}
else
break;
Sleep(delay);
}
}
本文档为【贪吃蛇报告】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。