|
void initgame(){//initlize game
status flag;
snack s;
gameground();
window(2,2,80,25);
initsnack(&s);
layegg(&s);
gamedrive(&s);
// return flag;
}//initgame
/**********MUEN OPERATION*********/
void menuground(){ //type the menu backgrond
int i;
textmode(C80);
textbackground(BLUE);
clrscr();
window(35,8,45,19);
textbackground(GREEN);
clrscr();
textcolor(RED);
gotoxy(4,2);
cprintf("Start");
gotoxy(4,4);
cputs("Speed");
gotoxy(4,6);
cputs("Sort");
gotoxy(4,8);
cputs("Help");
gotoxy(4,10);
cputs("Quit");
textcolor(BLUE);
for(i=1;i<=11;i+=2){
gotoxy(1,i);
cputs("-----------");
}//for
}//menugrond
void outputmenu(int t){//output the need item
switch(t){
case 1: cputs("Start"); break;
case 2: cputs("Speed"); break;
case 3: cputs("Sort"); break;
case 4: cputs("Help"); break;
case 5: cputs("Quit"); break;
default: exit(0);
}//switch
}//outputmenu
int menucontrol(){ //choose a item from the menu
int m,pos=1;
menuground();
textcolor(YELLOW);
gotoxy(4,2);
cputs("Start");
while(1){
while(!bioskey(1));
m=bioskey(0);
m=m>>8;
switch(m){
case 72: if(pos!=1){
textcolor(RED);
gotoxy(4,2*pos);
outputmenu(pos);
textcolor(YELLOW);
pos--;
gotoxy(4,2*pos);
outputmenu(pos);
}//if
break;
case 80: if(pos!=5){
textcolor(RED);
gotoxy(4,2*pos);
outputmenu(pos);
textcolor(YELLOW);
pos++;
gotoxy(4,2*pos);
outputmenu(pos);
}//if
break;
case 28: return pos;
}//switch
}//while
}//menucontrol
/**********SPEED CONTROL***********/
void speedground(){
textmode(C80);
textbackground(BLUE);
clrscr();
window(20,10,50,15);
textbackground(GREEN);
clrscr();
textcolor(RED);
gotoxy(1,1);
cprintf("SPEED LEVEL:\n");
textcolor(YELLOW);
gotoxy(5,3);
cputs("***1***2***3***4***5***\n");
textcolor(RED);
gotoxy(3,4);
cprintf("****************************");
gotoxy(3,5);
cprintf("Easy<------------->Difficult");
textcolor(YELLOW);
gotoxy(2,6);
cprintf("Choose the level...");
}//speedgrond
void outputspeed(int t){//output the need item
switch(t){
case 1: cputs("1"); break;
case 2: cputs("2"); break;
case 3: cputs("3"); break;
case 4: cputs("4"); break;
case 5: cputs("5"); break;
default: exit(0);
}//switch
}//outputmenu
void speedcontrol(){ //choose a item from the menu
int m,pos=1;
speedground();
textcolor(RED);
gotoxy(8,3);
cputs("1");
while(1){
while(!bioskey(1));
m=bioskey(0);
m=m>>8;
switch(m){
case 75: if(pos!=1){
textcolor(YELLOW);
gotoxy(4*(pos+1),3);
outputspeed(pos);
textcolor(RED);
pos--;
gotoxy(4*(pos+1),3);
outputspeed(pos);
}//if
break;
case 77: if(pos!=5){
textcolor(YELLOW);
gotoxy(4*(pos+1),3);
outputspeed(pos);
textcolor(RED);
pos++;
gotoxy(4*(pos+1),3);
outputspeed(pos);
}//if
break;
case 28: speed=pos;
}//switch
if(m==28)
break;
}//while
}//menucontrol
/*****************END OF GAME**********/
void endgame(){
int i,j,k;
textmode(C80);
textbackground(BLUE);
clrscr();
window(15,1,65,25);
textbackground(YELLOW);
clrscr();
textcolor(GREEN);
j=1;
for(i=1;i<=4;i++){
gotoxy(26-i,j);
for(k=1;k<=2*i-1;k++)
cputs("*");
j++;
}//for
for(i=1;i<=6;i++){
gotoxy(26-(i+1),j);
for(k=1;k<=2*i+1;k++)
cputs("*");
j++;
}//for
for(i=1;i<=8;i++){
gotoxy(26-(i+1),j);
for(k=1;k<=2*i+1;k++)
cputs("*");
j++;
}//for
for(i=1;i<=4;i++){
gotoxy(23,j);
cputs("*****");
j++;
}//for
textcolor(YELLOW);
gotoxy(10,j);
cprintf("To my belove: Happy everyday!");
textcolor(RED);
gotoxy(40,j+1);
cputs("Victor");
getch();
exit(0);
}//endgame
/*************SORT FUNCTIONS********/
void sort(){
FILE *fp1,*fp2;
int n,i;
textmode(C80);
textbackground(BLUE);
clrscr();
textbackground(GREEN);
window(20,5,60,20);
clrscr();
textcolor(RED);
gotoxy(2,1);
cprintf("*****NUMBER*******NAME*******SCORE*****");
if(!(fp1=fopen("n_value.lly","rb"))){ //open the file n_value.lly
// printf("Cannot open the file.\n");
getch();
return;
}//if
n=getw(fp1);
fclose(fp1);//close file n_value.lly
if(!(fp2=fopen("record.lly","rb"))){ //open the file record.lly
printf("Can not open record.lly");
getch();
exit(0);
}//if
for(i=0;i<n;i++) //read the records
fread(&record,sizeof(renode),1,fp2);
fclose(fp2);//close file record.lly
for(i=0;i<n;i++){
gotoxy(7,i+3);
cprintf("%d",record.number);
gotoxy(20,i+3);
cprintf("%s",record.name);
gotoxy(31,i+3);
cprintf("%d",record.score);
}//for
textcolor(YELLOW);
gotoxy(2,16);
cprintf("Press any key to return...");
getch();
}//sort
void recordadd(){
textbackground(GREEN);
window(10,10,35,10);
clrscr();
textcolor(YELLOW);
cprintf("Input your name:");
gets(name);
gotoxy(2,2);
}//recordadd
void sortadd(){
int n,i;
FILE *fp1,*fp2;
if(!(fp1=fopen("n_value.lly","rb"))){ //n_value does not exit
recordadd();
if(name[0]=='\0')
return;
if(!(fp1=fopen("n_value.lly","wb"))){
printf("Can not open n_value with w.");
getch();
exit(0);
}//if
putw(1,fp1); //write n=1 to file n_value.lly
fclose(fp1);//close the file n_value.lly
record[0].score=score;
strcpy(record[0].name,name);
record[0].number=1;
if(!(fp2=fopen("record.lly","wb"))){ //open file record.lly
printf("Can not open record.lly with w");
getch();
exit(0);
}//if
fwrite(&record[0],sizeof(renode),1,fp2);//input new record
fclose(fp2);//close record.lly
}//if
else{//n_value.lly has exited.
n=getw(fp1);//get the record nember
fclose(fp1);
if(n<10){ //the record is not full
recordadd();
if(name[0]=='\0'){ //no name input
fclose(fp1);
return;
}//if
n++;
if(!(fp1=fopen("n_value.lly","wb"))){//open file n_value.lly
printf("Can not open n_value with w.");
getch();
exit(0);
}//if
putw(n,fp1);//updata the file n_value
fclose(fp1); //close file n_value
if(!(fp2=fopen("record.lly","rb"))){//open record.llly to add record
printf("Can not open record.lly");
getch();
exit(0);
}//if
for(i=0;i<n-1;i++)//read out the record
fread(&record,sizeof(renode),1,fp2);
for(i=n-2;i>=0;i--){ //choose the position to insert
if(record.score<score){
strcpy(record[i+1].name,record.name);
record[i+1].score=record.score;
record[i+1].number=record.number+1;
}//if
else
break;
}//for
record[i+1].score=score; //insert the new record
strcpy(record[i+1].name,name);
record[i+1].number=i+2;
fclose(fp2);//close file record.lly
if(!(fp2=fopen("record.lly","wb"))){//open record.llly to add record
printf("Can not open record.lly");
getch();
exit(0);
}//if
for(i=0;i<n;i++) //write the records
fwrite(&record,sizeof(renode),1,fp2);
fflush(fp2);
fclose(fp2); //close the file record
}//if
else{ // the record has full:n=10
if(!(fp2=fopen("record.lly","rb"))){//open record.llly to add record
printf("Can not open record.lly");
getch();
exit(0);
}//if
for(i=0;i<10;i++) //read out all the record
fread(&record,sizeof(renode),1,fp2);
fclose(fp2);//close file record.lly
for(i=0;i<10;i++){ //updata the records
if(record.score<score){
recordadd();
if(name[0]=='\0')
return;
strcpy(record.name,name);
record.score=score;
break;
}//if
}//for
if(i<10){//a new record is inserted
if(!(fp2=fopen("record.lly","wb"))){//open record.llly to add record
printf("Can not open record.lly");
getch();
exit(0);
}//if
for(i=0;i<10;i++)//write the records to file
fwrite(&record,sizeof(renode),1,fp2);
fflush(fp2); //clear the output path
fclose(fp2);//close the file reord.lly
}//if
}// else
}//else
}//sortadd
void main(){
int t,flag=1;
while(flag){
t=menucontrol();
switch(t){
case 1: initgame(); break;
case 2: speedcontrol(); break;
case 3: sort(); break;
case 4: help(); break;
case 5: endgame();
}//switch
flag=menuflag;
if(flag==0)
endgame();
din=right;//init the data
score=0;
}//while
}//main |
|