?? snake.c
字號:
/**********************************************************/
/* 本程序在Win-TC 2.0下編譯通過 */
/* */
/* 文件名: GreedySnake.c */
/**********************************************************/
/**********************************************************/
/* 程序中用到的庫函數所在頭文件應用 #include 命令包含進來 */
#include<graphics.h>
#include<bios.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
#include<conio.h>
#include<ctype.h>
/**********************************************************/
/* 定義符號常量 */
#define len sizeof(struct snake)
#define Q(*que)
/*文件保存路徑*/
#define FILENAME "c:\\tc.dat"
/**********************************************************/
/* 定義數據結構*/
/*蛇節點*/
struct snake_base
{
int x ;
int y ;
}
;
/*隊列節點 */
struct snake
{
struct snake_base data ;
struct snake*next ;
}
;
/*隊列 */
struct queue
{
struct snake*front,*rear ;
}
;
/*排行榜結構*/
struct person
{
char name[20];
int score ;
}
;
/**********************************************************/
/* 定義全局變量 */
struct person per[5];
/**********************************************************/
/************************************************************************************/
/*自定義函數原型說明 */
void inques(struct queue*que,int x,int y);
/*入隊函數*/
void creatque(struct queue Q,int m);
/*創建隊列*/
struct snake_base delete(struct queue*que);
/*出隊函數,返回值為一個蛇節點*/
void*picture(int type);
/*繪制方塊函數*/
void draw(int x,int y,void*p);
/*從圖形緩存中讀出數據,并顯示在屏幕上*/
int search(int x,int y,struct snake*tmpHead);
/*判斷蛇頭是否與蛇身相遇函數*/
void creat_menu();
/*產生主界面*/
void ReadFiles();
/*從文件中讀取記錄信息*/
void WriteFiles(struct person*);
/*將記錄寫入文件中*/
void CompareScore(struct person);
/*排行榜按分數進行排序*/
void pain_gameover();
/*繪制游戲結束時的圖形*/
/***********************************************************************************/
/**********************************************************/
/*主函數*/
void main()
{
int gm=VGAHI,gd=VGA ;
int mark,level,m,key,hi,lo,h ;
int s1,m1,i,j,k,xn,yn,flag,bl ;
long time ;
unsigned s2 ;
void*p1,*p2 ;
char ch,string[3];
struct person CurPerson ;
struct queue*que ;
struct snake*q ;
struct snake_base x1 ;
struct time t[1];
do
{
flag=1 ;
bl=0 ;
que=NULL ;
/*下面兩行為初始化游戲變量*/
time=30000 ;
mark=0 ;
level=1 ;
m=0 ;
gettime(t);
/*初始化隨機數*/
s1=t[0].ti_sec ;
m1=t[0].ti_min ;
s2=s1*m1 ;
srand(s2);
initgraph(&gd,&gm,"");
p1=picture(1);
p2=picture(0);
creatque(que,3);
creat_menu();
itoa(mark,string,10);
setcolor(15);
outtextxy(100,440,string);
itoa(level,string,10);
outtextxy(270,440,string);
q=Q.front->next ;
/*遍歷隊列,繪制初始狀態的蛇身*/
for(k=0;k<=2;k++)
{
draw(q->data.x,q->data.y,p1);
if(k==2)
{
/*隊尾節點為蛇頭,取出賦值給i,j*/
i=q->data.x ;
j=q->data.y ;
}
q=q->next ;
}
do
{
xn=((unsigned long)rand())%44+1 ;
/*隨即產生兩個坐標,作為新節點*/
yn=((unsigned long)rand())%29+1 ;
}
while(xn<1||xn>44||yn<1||yn>29||search(xn,yn,que->front));
/*采用條件來約束隨機點的范圍*/
draw(xn,yn,p1);
lo=0 ;
hi=72 ;
h=72 ;
while(flag)
{
if(kbhit())
{
/*檢查鍵盤事件*/
key=bioskey(0);
if(kbhit())
getch();
lo=key&0x00ff ;
if(lo==0)
hi=(key&0xff00)>>8 ;
}
switch(hi)
{
case 72 :
if(h!=80)
{
h=72 ;
j--;
}
else
{
hi=80 ;
j++;
}
break ;
case 80 :
if(h!=72)
{
h=80 ;
j++;
}
else
{
hi=72 ;
j--;
}
break ;
case 75 :
if(h!=77)
{
h=75 ;
i--;
}
else
{
hi=77 ;
i++;
}
break ;
case 77 :
if(h!=75)
{
h=77 ;
i++;
}
else
{
hi=75 ;
i--;
}
break ;
}
if((xn==i)&&(yn==j))
{
/*蛇頭與新節點相遇,蛇身變長,同時新節點入隊*/
m++;
/*每吃5顆子,難度增加一級*/
if(m==5)
{
level=level+1 ;
if(time>6000)time=time-2000 ;
itoa(level,string,10);
setcolor(15);
outtextxy(270,440,string);
m=0 ;
}
mark=mark+1 ;
itoa(mark,string,10);
setcolor(15);
outtextxy(100,440,string);
inques(que,xn,yn);
do
{
xn=((unsigned long)rand())%44+1 ;
yn=((unsigned long)rand())%29+1 ;
}
while(xn<1||yn>29||xn>44||yn<1||search(xn,yn,que->front));
draw(xn,yn,p1);
}
else
{
/*判斷是否到達邊界*/
if((i<1)||(i>44)||(j<1)||(j>29)||search(i,j,que->front))
{
if(bl==0)
flag=0 ;
/*游戲結束標志*/
}
else
{
/*如果沒有碰到新節點,則移動蛇身,實現方法: 繪制蛇頭的下一狀態的,并讓下一狀態的坐標
入隊,同時,出隊一個節點(要擦除的節點坐標),用擦除圖形擦除該坐標的蛇身*/
inques(que,i,j);
draw(i,j,p1);
x1=delete(que);
draw(x1.x,x1.y,p2);
}
}
delay(time);
/*延時*/
bl=0 ;
}
ReadFiles();
/*從文件中讀取記錄信息*/
setfillstyle(1,9);
bar(12,12,633,421);
pain_gameover();
outtextxy(150,320,"Do you want to restart the game Now? [y/n]");
/*提示信息*/
while(1)
{
ch=getch();
if(ch=='y'||ch=='Y'||ch=='n'||ch=='N')break ;
}
}
while(ch=='y'||ch=='Y');
/*整個游戲的循環,用于控制重復進行游戲*/
closegraph();
getch();
}
/**********************************************************/
/*入隊函數*/
void inques(struct queue*que,int x,int y)
{
struct snake*s ;
s=(struct snake*)malloc(len);
s->data.x=x ;
s->data.y=y ;
s->next=NULL ;
que->rear->next=s ;
que->rear=s ;
}
/*創建隊列*/
void creatque(struct queue Q,int m)
{
int i,y,x ;
struct snake*h ;
h=(struct snake*)malloc(len);
h->next=NULL ;
Q.front=h ;
Q.rear=h ;
/*往隊列中插入3個初始狀態的節點*/
for(i=0;i<m;i++)
{
y=25+2-i ;
inques(que,20,y);
}
}
/*出隊函數,返回值為一個蛇節點*/
struct snake_base delete(struct queue*que)
{
struct snake_base x ;
struct snake*p ;
p=Q.front->next ;
Q.front->next=p->next ;
x=p->data ;
free(p);
return(x);
}
/*繪制方塊函數*/
void*picture(int type)
{
void*p ;
int size ;
if(type)
{
/*繪制蛇身和節點方塊*/
setfillstyle(1,2);
bar(0,0,12,12);
rectangle(1,1,11,11);
rectangle(3,3,9,9);
rectangle(5,5,7,7);
}
else
{
/*繪制擦除方塊*/
setfillstyle(1,9);
bar(0,0,12,12);
}
size=imagesize(0,0,12,12);
/*創建圖形緩存*/
p=malloc(size);
getimage(0,0,12,12,p);
/*讀入圖片到緩存中*/
cleardevice();
return(p);
}
/*從圖形緩存中讀出數據,并顯示在屏幕上*/
void draw(int x,int y,void*p)
{
putimage(x*14,y*14,p,0);
}
/*判斷蛇頭是否與蛇身相遇函數*/
int search(int x,int y,struct snake*tmpHead)
{
int k ;
tmpHead=tmpHead->next ;
/*跳過蛇頭節點*/
while(tmpHead!=NULL)
{
/*判斷是否與蛇身節點有相同坐標,若有,則相碰*/
if((tmpHead->data.x==x)&&(tmpHead->data.y==y))
return(1);
else
{
tmpHead=tmpHead->next ;
}
}
return(0);
}
/*繪制游戲主界面*/
void creat_menu()
{
int i ;
setbkcolor(2);
setfillstyle(1,9);
bar(12,12,629,421);
setfillstyle(1,2);
setcolor(4);
rectangle(13,13,629,421);
setcolor(15);
rectangle(12,12,628,420);
setfillstyle(1,7);
bar(0,0,640,8);
setcolor(1);
setlinestyle(4,0x8888,1);
/* 1000100010001000設置線*/
/*畫橫線*/
for(i=1;i<29;i++)line(14,13+i*14,629,13+i*14);
/*畫堅線*/
for(i=1;i<44;i++)line(13+i*14,14,13+i*14,418);
setlinestyle(SOLID_LINE,1,1);
setcolor(8);
setcolor(15);
outtextxy(30,440,"score:");
outtextxy(200,440,"level:");
outtextxy(30,460,"Copyright by HeJinZhuangBei.");
outtextxy(400,460,"GreedySnake Version 1.0");
}
/*繪制游戲結束時的一些圖形*/
void pain_gameover()
{
setfillstyle(1,6);
bar(180,130,460,190);
setfillstyle(1,15);
setcolor(15);
rectangle(181,131,459,189);
rectangle(195,145,446,175);
rectangle(193,143,448,177);
outtextxy(213,158," BAD LUCK! GAME OVER!");
}
/*讀取文件操作函數*/
void ReadFiles()
{
FILE*fpread ;
/*如果文件不存在,則創建一個空文件,否則則打開該文件*/
if((fpread=fopen(FILENAME,"ab+"))==NULL)
{
printf("can't open the file person.dat!");
exit(0);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -