本文目录一览:
大专考试,C语言贪吃蛇程序编写,
//DEV C++ 通过
/*
program by wlfryq 71693456@qq.com
*/
#include stdio.h
#include stdlib.h
#include conio.h
#include windows.h
#include time.h
#include direct.h
#include stdbool.h
#define W 80 //屏幕宽度
#define H 37 //屏幕高度
#define SNAKE_ALL_LENGTH 200 //蛇身最长为
void CALLBACK TimerProc(
HWND hwnd,
UINT message,
UINT idTimer,
DWORD dwTime);
void start();
struct MYPOINT
{
int x;
int y;
} s[SNAKE_ALL_LENGTH] , head, end, food;
int max_count=0; //历史最高分,如果countmax_count, 则max_count=count
int old_max_count=0; //历史最高分,不会变动, 用于死亡时判断max_count是否大于old_max_count,如果大于,则写入文件
int count=0; //得分
int len=20; //当前蛇身长度
int direct=0; //方向: 0-向右, 1-向下, 2-向左, 3-向上
int speed=200; //速度:毫秒
bool isfood=false; //食物是否存在
int timerID;
bool stop=false; //暂停
char* ini_path; //数据文件绝对路径
void setxy(int x, int y) //设置CMD窗口光标位置
{
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hide_cursor() //隐藏CMD窗口光标
{
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = sizeof(cci);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, cci);
}
void set_food() //设置食物坐标
{
if(isfood==true)
{
return;
}
int x,y,i;
bool flag=false;
while(1)
{
flag=false;
x=rand()%(W-14)+6;
y=rand()%(H-12)+6;
for(i=0;ilen;i++) //判断食物是否落在蛇身上
{
if(s[i].x==x s[i].y==y)
{
flag=true;
break;
}
}
if(flag==true)
{
continue;
}
else
{
food.x=x;
food.y=y;
break;
}
}
setxy(food.x,food.y);
printf("*");
isfood=true;
}
void check_board() //检测蛇身是否越界和相交
{
int i;
if(s[0].x=W-3 || s[0].x=2 || s[0].y=H-1 || s[0].y=2)
{
setxy(W/2-5,0);
printf("game over\n");
stop=true;
if(old_max_countmax_count)
{
char t[5]={'\0'};
sprintf(t,"%d",max_count);
WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);
}
}
for(i=1;ilen;i++)
{
if(s[i].x==s[0].x s[i].y==s[0].y)
{
setxy(W/2-5,0);
printf("game over\n");
stop=true;
if(old_max_countmax_count)
{
char t[5]={'\0'};
sprintf(t,"%d",max_count);
WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);
}
break;
}
}
if(stop==true)
{
KillTimer(NULL,timerID);
int c;
while(1)
{
fflush(stdin);
c=_getch();
if(c=='n' || c=='N')
{
start();
}
else if(c=='q' || c=='Q')
{
exit(0);
}
else continue;
}
}
}
void printf_body(bool is_first) //打印蛇身
{
if(is_first==true) //如果是第一次打印蛇身
{
int i;
for(i=0;ilen;i++)
{
setxy(s[i].x,s[i].y);
printf("O");
}
}
else //如果不是第一次打印蛇身
{
setxy(end.x,end.y);
printf(" ");
setxy(s[0].x,s[0].y);
printf("O");
}
if(food.x==s[0].x food.y==s[0].y) //如果吃到食物
{
count++;
isfood=false; //重置食物
set_food();
len++;
KillTimer(NULL,timerID);
if(speed100) speed-=10;
else if(speed50) speed-=5;
else if(speed30) speed-=2;
else if(speed16) speed-=1;
else ;
setxy(0,0);
if(max_countcount) max_count=count;
printf(" speed : %d ms score : %d best score:%d ",speed,count,max_count);
timerID=SetTimer(NULL,001,speed,TimerProc);
}
}
void change_body_pos(int x, int y) //改变蛇身的坐标数据
{
end.x=s[len-1].x;
end.y=s[len-1].y;
int i;
for(i=len-1;i0;i--)
{
s[i].x=s[i-1].x;
s[i].y=s[i-1].y;
}
s[0].x=x;
s[0].y=y;
}
void CALLBACK TimerProc(
HWND hwnd,
UINT message,
UINT idTimer,
DWORD dwTime)
{
switch(direct)
{
case 0:
head.x++;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case 1:
head.y++;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case 2:
head.x--;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case 3:
head.y--;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
}
}
void start()
{
int i;
KillTimer(NULL,timerID);
count=0; //得分
len=20; //当前蛇身长度
direct=0; //方向: 0-向右, 1-向下, 2-向左, 3-向上
speed=200; //速度:毫秒
isfood=false; //食物是否存在
stop=false; //停止
system("cls");
setxy(1,4);
printf("┌─────────────────────────────────────┐\n");
for(i=0;i33;i++)
{
printf(" │ │\n");
}
printf(" └─────────────────────────────────────┘");
head.x=len-1+5;
head.y=H/2;
for(i=0;ilen;i++)
{
s[i].x=head.x-i;
s[i].y=head.y;
}
setxy(0,0);
printf(" speed : %d:ms score : %d best score:%d ",speed,count,max_count);
printf_body(true);
set_food();
timerID=SetTimer(NULL,001,speed,TimerProc);
int c;
MSG msg;
while(GetMessage(msg,NULL,0,0))
{
if(stop==true) break;
if(_kbhit()) //如果按下的是方向键或功能键, _getch()要调用两次,第一次返回0XE0或0
{
fflush(stdin);
c=_getch(); //上: 72 下:80 左:75 右:77
if(c==0XE0 || c==0)
{
c=_getch();
if(c==72 direct!=1 direct!=3)
{
direct=3;
}
else if(c==80 direct!=1 direct!=3)
{
direct=1;
}
else if(c==75 direct!=0 direct!=2)
{
direct=2;
}
else if(c==77 direct!=0 direct!=2)
{
direct=0;
}
}
else if(c==' ')
{
setxy(W/2-10,0);
system("pause");
setxy(W/2-10,0);
printf(" ");
}
}
if(msg.message==WM_TIMER)
{
DispatchMessage(msg);
}
}
}
int main()
{
ini_path=(char*)malloc(sizeof(char)*50);
srand((unsigned)time(0));
getcwd(ini_path,50);//取得当前程序绝对路径
ini_path=strcat(ini_path,"snake.dat");
max_count=GetPrivateProfileInt("MAX_COUNT","max_count",0,ini_path);
old_max_count=max_count;
char cmd[50];
sprintf(cmd,"mode con cols=%d lines=%d\0",W,H);
system(cmd);//改变CMD窗口大小
hide_cursor();
start();
return 0;
}
codeblocks 贪吃蛇c语言程序
/* C语言
program by wlfryq 71693456@qq.com
*/
#include stdio.h
#include stdlib.h
#include conio.h
#include windows.h
#include time.h
#include direct.h
#include stdbool.h
#define W 80 //屏幕宽度
#define H 37 //屏幕高度
#define SNAKE_ALL_LENGTH 200 //蛇身最长为
void CALLBACK TimerProc(
HWND hwnd,
UINT message,
UINT idTimer,
DWORD dwTime);
void start();
struct MYPOINT
{
int x;
int y;
} s[SNAKE_ALL_LENGTH] , head, end, food;
int max_count=0; //历史最高分,如果countmax_count, 则max_count=count
int old_max_count=0; //历史最高分,不会变动, 用于死亡时判断max_count是否大于old_max_count,如果大于,则写入文件
int count=0; //得分
int len=20; //当前蛇身长度
int direct=0; //方向: 0-向右, 1-向下, 2-向左, 3-向上
int speed=200; //速度:毫秒
bool isfood=false; //食物是否存在
int timerID;
bool stop=false; //暂停
char* ini_path; //数据文件绝对路径
void setxy(int x, int y) //设置CMD窗口光标位置
{
COORD coord = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void hide_cursor() //隐藏CMD窗口光标
{
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = sizeof(cci);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, cci);
}
void set_food() //设置食物坐标
{
if(isfood==true)
{
return;
}
int x,y,i;
bool flag=false;
while(1)
{
flag=false;
x=rand()%(W-14)+6;
y=rand()%(H-12)+6;
for(i=0;ilen;i++) //判断食物是否落在蛇身上
{
if(s[i].x==x s[i].y==y)
{
flag=true;
break;
}
}
if(flag==true)
{
continue;
}
else
{
food.x=x;
food.y=y;
break;
}
}
setxy(food.x,food.y);
printf("*");
isfood=true;
}
void check_board() //检测蛇身是否越界和相交
{
int i;
if(s[0].x=W-3 || s[0].x=2 || s[0].y=H-1 || s[0].y=2)
{
setxy(W/2-5,0);
printf("game over\n");
stop=true;
if(old_max_countmax_count)
{
char t[5]={'\0'};
sprintf(t,"%d",max_count);
WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);
}
}
for(i=1;ilen;i++)
{
if(s[i].x==s[0].x s[i].y==s[0].y)
{
setxy(W/2-5,0);
printf("game over\n");
stop=true;
if(old_max_countmax_count)
{
char t[5]={'\0'};
sprintf(t,"%d",max_count);
WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);
}
break;
}
}
if(stop==true)
{
KillTimer(NULL,timerID);
int c;
while(1)
{
fflush(stdin);
c=_getch();
if(c=='n' || c=='N')
{
start();
}
else if(c=='q' || c=='Q')
{
exit(0);
}
else continue;
}
}
}
void printf_body(bool is_first) //打印蛇身
{
if(is_first==true) //如果是第一次打印蛇身
{
int i;
for(i=0;ilen;i++)
{
setxy(s[i].x,s[i].y);
printf("O");
}
}
else //如果不是第一次打印蛇身
{
setxy(end.x,end.y);
printf(" ");
setxy(s[0].x,s[0].y);
printf("O");
}
if(food.x==s[0].x food.y==s[0].y) //如果吃到食物
{
count++;
isfood=false; //重置食物
set_food();
len++;
KillTimer(NULL,timerID);
if(speed100) speed-=10;
else if(speed50) speed-=5;
else if(speed30) speed-=2;
else if(speed16) speed-=1;
else ;
setxy(0,0);
if(max_countcount) max_count=count;
printf(" speed : %d ms score : %d best score:%d ",speed,count,max_count);
timerID=SetTimer(NULL,001,speed,TimerProc);
}
}
void change_body_pos(int x, int y) //改变蛇身的坐标数据
{
end.x=s[len-1].x;
end.y=s[len-1].y;
int i;
for(i=len-1;i0;i--)
{
s[i].x=s[i-1].x;
s[i].y=s[i-1].y;
}
s[0].x=x;
s[0].y=y;
}
void CALLBACK TimerProc(
HWND hwnd,
UINT message,
UINT idTimer,
DWORD dwTime)
{
switch(direct)
{
case 0:
head.x++;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case 1:
head.y++;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case 2:
head.x--;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case 3:
head.y--;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
}
}
void start()
{
int i;
KillTimer(NULL,timerID);
count=0; //得分
len=20; //当前蛇身长度
direct=0; //方向: 0-向右, 1-向下, 2-向左, 3-向上
speed=200; //速度:毫秒
isfood=false; //食物是否存在
stop=false; //停止
system("cls");
setxy(1,4);
printf("┌─────────────────────────────────────┐\n");
for(i=0;i33;i++)
{
printf(" │ │\n");
}
printf(" └─────────────────────────────────────┘");
head.x=len-1+5;
head.y=H/2;
for(i=0;ilen;i++)
{
s[i].x=head.x-i;
s[i].y=head.y;
}
setxy(0,0);
printf(" speed : %d:ms score : %d best score:%d ",speed,count,max_count);
printf_body(true);
set_food();
timerID=SetTimer(NULL,001,speed,TimerProc);
int c;
MSG msg;
while(GetMessage(msg,NULL,0,0))
{
if(stop==true) break;
if(_kbhit()) //如果按下的是方向键或功能键, _getch()要调用两次,第一次返回0XE0或0
{
fflush(stdin);
c=_getch(); //上: 72 下:80 左:75 右:77
if(c==0XE0 || c==0)
{
c=_getch();
if(c==72 direct!=1 direct!=3)
{
direct=3;
}
else if(c==80 direct!=1 direct!=3)
{
direct=1;
}
else if(c==75 direct!=0 direct!=2)
{
direct=2;
}
else if(c==77 direct!=0 direct!=2)
{
direct=0;
}
}
else if(c==' ')
{
setxy(W/2-10,0);
system("pause");
setxy(W/2-10,0);
printf(" ");
}
}
if(msg.message==WM_TIMER)
{
DispatchMessage(msg);
}
}
}
int main()
{
ini_path=(char*)malloc(sizeof(char)*50);
srand((unsigned)time(0));
getcwd(ini_path,50);//取得当前程序绝对路径
ini_path=strcat(ini_path,"snake.dat");
max_count=GetPrivateProfileInt("MAX_COUNT","max_count",0,ini_path);
old_max_count=max_count;
char cmd[50];
sprintf(cmd,"mode con cols=%d lines=%d\0",W,H);
system(cmd);//改变CMD窗口大小
hide_cursor();
start();
return 0;
}
求贪吃蛇的C++程序代码
#includegraphics.h
#includestdlib.h
#includedos.h
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /*食物的结构体*/
{
int x; /*食物的横坐标*/
int y; /*食物的纵坐标*/
int yes; /*食物是否出现的变量*/
}food;
struct Snack /*蛇的结构体*/
{
int x[N];
int y[N];
int node; /*蛇的节数*/
int direction; /*蛇的方向*/
int life; /*蛇的生命,0活着,1死亡*/
}snake;
void Init(void); /*图形驱动*/
void Close(void); /*关闭游戏函数*/
void DrawK(void); /*画图函数*/
void GameOver(void);/*输出失败函数*/
void GamePlay(); /*游戏控制函数 主要程序*/
void PrScore(void); /*分数输出函数*/
DELAY(char ch)/*调节游戏速度*/
{
if(ch=='3')
{
delay(gamespeed); /*delay是延迟函数*/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}
Menu()/*游戏开始菜单*/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers..\n");
do
{ch=getch();}
while(ch!='1'ch!='2'ch!='3');
clrscr();
return(ch);
}
/*主函数*/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(gd,gm,"c:\\tc");
cleardevice();
}
void DrawK(void)
{
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i=600;i+=10)
{
rectangle(i,40,i+10,49); /*画出上边框*/
rectangle(i,451,i+10,460); /*画出下边框*/
}
for(i=40;i=450;i+=10)
{
rectangle(50,i,59,i+10); /*画出左边框*/
rectangle(601,i,610,i+10); /*画出右边框*/
}
}
void GamePlay(char ch)
{
randomize(); /*随机数发生器*/
food.yes=1; /*1代表要出现食物,0表示以存在食物*/
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1) /*可以重复游戏*/
{
while(!kbhit()) /*在没有按键的情况下蛇自己移动*/
{
if(food.yes==1) /*需要食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60; /*使用rand函数随机产生食物坐标*/
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++; /*判断食物是否出现在整格里*/
food.yes=0; /*现在有食物了*/
}
if(food.yes==0) /*有食物了就要显示出来*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i0;i--) /*贪吃蛇的移动算法*/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1]; /*贪吃蛇的身体移动算法*/
}
switch(snake.direction) /*贪吃蛇的头部移动算法,以此来控制移动*/
{
case 1:snake.x[0]+=10;break;
case 2:snake.x[0]-=10;break;
case 3:snake.y[0]-=10;break;
case 4:snake.y[0]+=10;break;
}
for(i=3;isnake.node;i++) /*判断是否头部与身体相撞*/
{
if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
/*下面是判断是否撞到墙壁*/
if(snake.x[0]55||snake.x[0]595||snake.y[0]55||snake.y[0]455)
{
GameOver();
snake.life=1;
}
if(snake.life==1) /*如果死亡就退出循环*/
break;
if(snake.x[0]==food.xsnake.y[0]==food.y) /*判断蛇是否吃到食物*/
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10); /*吃的食物后用黑色将食物擦去*/
snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*现把增加的一节放到看不到的地方去*/
snake.node++;
food.yes=1;
score+=10;
PrScore();
}
setcolor(4); /*每次移动后将后面的身体擦去*/
for(i=0;isnake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
DELAY(ch);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(snake.life==1)
break;
key=bioskey(0); /*接受按键*/
if(key==ESC)
break;
else
if(key==UPsnake.direction!=4)/*判断是否改变方向*/
snake.direction=3;
else
if(key==RIGHTsnake.direction!=2)
snake.direction=1;
else
if(key==LEFTsnake.direction!=1)
snake.direction=2;
else
if(key==DOWNsnake.direction!=3)
snake.direction=4;
}
}
void GameOver(void)
{
cleardevice();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}