主题:带音乐背景的"接龙"游戏的C源程序
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
#define NULL 0
#define len sizeof(struct liner)
struct liner {int x; int y; struct liner *next; };
int n; struct liner *creat(int m)
{ int i; struct liner *head,*p1,*p2;
n=0;
p1=(struct liner *)malloc(len);
p1->x=30;p1->y=20;
head=NULL;
p2=p1;
for(i=0;i<=m-1;i++)
{ n++;
if (n==1) head=p1;
else p2->next=p1;
p2=p1; p1=(struct liner *)malloc(len);
p1->x=30;p1->y=20+2-i;
}
p2->next=head; return(head); }
struct liner *locate(struct liner *head) { return(head->next); }
void *picture(int a) { void *p; int size; if(a) {setfillstyle(1,4);
bar(0,0,10,10);
rectangle(1,1,9,9);
rectangle(4,4,6,6);}
size=imagesize(0,0,10,10);
p=malloc(size);
getimage(0,0,10,10,p);
cleardevice();
return(p); }
void draw(int x,int y,void *p)
{putimage(x*10,y*10,p,COPY_PUT);}
struct liner *insert(struct liner *head,int x,int y)
{ struct liner *p; p=(struct liner *)malloc(len);
p->x=x;p->y=y;
p->next=head->next;
head->next=p;
head=p;
n++;
return(head); }
int search(int x,int y,struct liner *head)
{ int k; struct liner *p; p=head;
for(k=0;k<=n-1;k++) {if ((p->x==x) && (p->y==y)) return(1);
else p=locate(p); } return(0); }
main()
{
int gm=2,gd=VGA;
int h; int time=300,mark=0;
int key,hi,lo;
void *p1,*p2; int i,j;
int x,y;
int flag=1,bl=0,musicflag=0;
struct liner *head,*p;
clrscr();
gotoxy(34,10); printf("JieLong");
gotoxy(27,11); printf("CopyRight 1995 by ljh");
gotoxy(29,17); printf("MUISC=ON|OFF[y/n]n");
gotoxy(46,17);
if(tolower(getchar())=='y') musicflag=1;
else musicflag=0;
initgraph(&gd,&gm,"c:\\tc"); p2=picture(0);
p1=picture(1); head=creat(3);
p=head; setfillstyle(1,1); bar(0,0,630,349);
rectangle(0,0,639,349);
for(i=0;i<=2;i++)
{ draw(p->x,p->y,p1); p=locate(p); } x=rand()%60+1; y=rand()%15+1; draw(x,y,p1);
lo=0; p=head; hi=72; h=72;
while(flag) { if(musicflag)
{ sound(400); delay(10); nosound(); }
if(kbhit()) {key=bioskey(0);
if(kbhit()) getch(); lo=key & 0x00ff;
if(lo==0) hi=(key & 0xff00)>>8; } i=p->x; j=p->y;
p=locate(p); draw(p->x,p->y,p2);
{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((x==i)&&(y==j)) { if(musicflag) { sound(1000); delay(100); nosound(); }
p=insert(p,x,y);
x=((unsigned int)rand())%50+1;
y=((unsigned int)rand())%25+1;
draw(x,y,p1); gotoxy(40,25);
printf("%d",++mark*100); }
else {if ((i<1)||(i>63)||(j<1)||(j>34)||search(i,j,p))
{if(bl==0) flag=0;}
else { p->x=i;p->y=j; draw(i,j,p1);} }
delay(time--);
if (time<=40) time=100; bl=0; } }
if(musicflag) { sound(1000); delay(100); sound(700); delay(50); sound(1090); delay(100); nosound(); } closegraph(); gotoxy(30,10); printf("GAME OVER"); getch(); }
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dos.h>
#define NULL 0
#define len sizeof(struct liner)
struct liner {int x; int y; struct liner *next; };
int n; struct liner *creat(int m)
{ int i; struct liner *head,*p1,*p2;
n=0;
p1=(struct liner *)malloc(len);
p1->x=30;p1->y=20;
head=NULL;
p2=p1;
for(i=0;i<=m-1;i++)
{ n++;
if (n==1) head=p1;
else p2->next=p1;
p2=p1; p1=(struct liner *)malloc(len);
p1->x=30;p1->y=20+2-i;
}
p2->next=head; return(head); }
struct liner *locate(struct liner *head) { return(head->next); }
void *picture(int a) { void *p; int size; if(a) {setfillstyle(1,4);
bar(0,0,10,10);
rectangle(1,1,9,9);
rectangle(4,4,6,6);}
size=imagesize(0,0,10,10);
p=malloc(size);
getimage(0,0,10,10,p);
cleardevice();
return(p); }
void draw(int x,int y,void *p)
{putimage(x*10,y*10,p,COPY_PUT);}
struct liner *insert(struct liner *head,int x,int y)
{ struct liner *p; p=(struct liner *)malloc(len);
p->x=x;p->y=y;
p->next=head->next;
head->next=p;
head=p;
n++;
return(head); }
int search(int x,int y,struct liner *head)
{ int k; struct liner *p; p=head;
for(k=0;k<=n-1;k++) {if ((p->x==x) && (p->y==y)) return(1);
else p=locate(p); } return(0); }
main()
{
int gm=2,gd=VGA;
int h; int time=300,mark=0;
int key,hi,lo;
void *p1,*p2; int i,j;
int x,y;
int flag=1,bl=0,musicflag=0;
struct liner *head,*p;
clrscr();
gotoxy(34,10); printf("JieLong");
gotoxy(27,11); printf("CopyRight 1995 by ljh");
gotoxy(29,17); printf("MUISC=ON|OFF[y/n]n");
gotoxy(46,17);
if(tolower(getchar())=='y') musicflag=1;
else musicflag=0;
initgraph(&gd,&gm,"c:\\tc"); p2=picture(0);
p1=picture(1); head=creat(3);
p=head; setfillstyle(1,1); bar(0,0,630,349);
rectangle(0,0,639,349);
for(i=0;i<=2;i++)
{ draw(p->x,p->y,p1); p=locate(p); } x=rand()%60+1; y=rand()%15+1; draw(x,y,p1);
lo=0; p=head; hi=72; h=72;
while(flag) { if(musicflag)
{ sound(400); delay(10); nosound(); }
if(kbhit()) {key=bioskey(0);
if(kbhit()) getch(); lo=key & 0x00ff;
if(lo==0) hi=(key & 0xff00)>>8; } i=p->x; j=p->y;
p=locate(p); draw(p->x,p->y,p2);
{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((x==i)&&(y==j)) { if(musicflag) { sound(1000); delay(100); nosound(); }
p=insert(p,x,y);
x=((unsigned int)rand())%50+1;
y=((unsigned int)rand())%25+1;
draw(x,y,p1); gotoxy(40,25);
printf("%d",++mark*100); }
else {if ((i<1)||(i>63)||(j<1)||(j>34)||search(i,j,p))
{if(bl==0) flag=0;}
else { p->x=i;p->y=j; draw(i,j,p1);} }
delay(time--);
if (time<=40) time=100; bl=0; } }
if(musicflag) { sound(1000); delay(100); sound(700); delay(50); sound(1090); delay(100); nosound(); } closegraph(); gotoxy(30,10); printf("GAME OVER"); getch(); }