自己编了一个贪吃蛇有效,效果类似,以供参考,我这个贪吃蛇有些机器能显示得分,有效不能显示,是乱码,不知为啥,有知道的给我也讲解一下。
#include<io.h>
#include<bios.h>
#include<dos.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<graphics.h>
#define get_char; ch1=getch();if(ch1=='n'||ch1==27) return(0);
#define delay_; for(delay_x=0;delay_x<1000;delay_x++){delay(delay_y);}
int gdriver = DETECT,gmode,errorcode;
char scr[25][40],disp_char[2];
char ch1;
int win_x = 39 , win_y = 24 ;
char snake_top='Q' , snake_middle = 'O' , snake_tail = 'V' ;
char snake_wall = 'X' , snake_other = 'Z' , snake_food = 'F' ;
int direction_old = 1,direction = 1 , direction_change_times=0,snake_length=3;
int snake_life=1,top_offset_x=1,top_offset_y=0;
int score=0,food_status=0;
unsigned int food_times=0,snake_step_times=0;
int snake_x[1200],snake_y[1200];
int text_color=3,text_back=0,text_mode=3;
int delay_x=1000,delay_y=150;
int check_kip=0,check_middle=0;
char far *image_food;
void main()
{
void graph_open();
void graph_close();
void disp_init_scr();
void disp_window();
void seek_snake();
void clear_grid();
void game_first_delay();
void game_delay();
void game_direction();
void game_over();
void disp_snake_step();
void disp_snake_go();
void init_snake_food();
void snake_eat_food();
void outtextxy_text(char *);
void clear_one_line();
void get_image_food( int, int);
void check_create_snake_food();
void game_if_retry();
void game_retry_init();
graph_open();
system("cls");
settextjustify(0,2);
settextstyle(0,0,2);
setbkcolor(9);
setcolor(15);
loop_1:
disp_init_scr(); /*OK,define scr[40][25]*/
seek_snake(); /*OK,random define snake length==3,disp snake*/
init_snake_food(); /*OK,random look for a space define food "F" ,disp*/
game_direction(); /*OK,according to direction define offset x,y*/
disp_snake_go(); /*ok,if life ,delay and step*/
game_retry_init();
game_if_retry();
game_direction();
if(snake_life==1)
goto loop_1;
free(image_food);
graph_close();
system("cls");
}
void game_retry_init()
{
direction_old = 1;
direction = 1 ;
direction_change_times=0;
snake_length=3;
top_offset_x=1;top_offset_y=0;
score=0;
food_status=0;
food_times=0;
snake_step_times=0;
check_kip=0;
check_middle=0;
}
void game_if_retry()
{
int check_key=0;int re_circle=1;
while(re_circle)
{
outtextxy(10*16,18*16,"press r restart,other quit");
{
check_key=0;
check_key=bioskey(0);
switch( check_key )
{
case 0x1372 :
{
snake_life=1;
check_key=0;
re_circle=0;
} break;
case 0x1071 :
{
snake_life=0;
check_key=0;
re_circle=0;
} break;
default :
{
snake_life=0;
re_circle=1;
check_key=0;
} break;
}
}
}
}
void disp_snake_go()
{
game_first_delay();
while(snake_life)
{
{
game_delay();
disp_snake_step();
snake_step_times++;
}
}
}
void game_direction()
{
switch( direction )
{
case 1 : { } break;
case 2 : { top_offset_x= 0 ; top_offset_y=1;} break;
case 3 : { top_offset_x= -1 ; top_offset_y=0;} break;
case 4 : { top_offset_x= 0 ; top_offset_y=-1;} break;
default : {top_offset_x= 1 ; top_offset_y=0;direction=1; } break;
}
}
void game_delay()
{
int check_i,check_k=0;
for(delay_x=0;delay_x<1000;delay_x++)
{
delay(delay_y) ;
if( bioskey(1) )
{
check_k++;
check_kip=( bioskey(0) );
check_middle = check_kip;
switch( check_kip )
{
case 0x4d00 :
{
if(direction_old!=3&&direction_old!=1)
{
direction=1 ;
game_direction();
check_kip=0;
}
} break;
case 0x5000 :
{
if(direction_old!=4&&direction_old!=2)
{
direction=2 ;
game_direction();
check_kip=0;
}
} break;
case 0x4b00 :
{
if(direction_old!=1&&direction_old!=3)
{
direction=3 ;
game_direction();
check_kip=0;
}
} break;
case 0x4800 :
{
if(direction_old!=2&&direction_old!=4)
{
direction=4 ;
game_direction();
check_kip=0;
}
} break;
default :
{
check_kip=0;
snake_life=0;
} break;
}
}
}
game_direction();
if(direction_old!=direction)
{
direction_change_times++;
direction_old=direction;
}
}
void game_first_delay()
{
int check_i,check_k=0,check_key=0,check_circle=1;
if(bioskey(1))
{
check_key=bioskey(0);
check_key=0;
}
else
check_key=bioskey(0);
switch( check_key )
{
case 0x4d00 :
{
direction=1 ;
} break;
case 0x5000 :
{
direction=2;
} break;
case 0x4b00 :
{
direction=3 ;
} break;
case 0x4800 :
{
direction=4 ;
} break;
default :
{
direction=1 ;
} break;
}
snake_life=1;
game_direction();
if(direction_old!=direction)
{
direction_change_times++;
direction_old=direction;
}
}
void game_over()
{
char * char_over;
snake_life=0;
sprintf(char_over,"12!@:'[]{}GameOver,direction=%d",direction_change_times);
outtextxy(1*16,(win_y+1)*16,char_over);
printf("\r123gameoverfood=%d;direction=%d;step=%d",food_times,direction_change_times,snake_step_times);
getch();
}
void outtextxy_text( char *string_text )
{
int i,j;
for(i=0;i<=win_x;i++)
clear_grid(i*16,(win_y+1)*16);
outtextxy(0,(win_y+2)*16,string_text);
free(string_text);
}
void clear_one_line( )
{
int i,j;
for(i=0;i<=win_x;i++)
clear_grid(i*16,(win_y+2)*16);
}
void clear_grid(int x,int y)
{
int grid_size;
char far *point1;
grid_size=imagesize(0,0,15,15);
point1=malloc(grid_size);
if(!point1) return -1;
getimage(x,y,x+15,y+15,point1);
putimage(x,y,point1,XOR_PUT);
free(point1);
}
void copy_grid(int x,int y,int x1,int y1)
{
int grid_size;
char far *point1;
grid_size=imagesize(0,0,15,15);
point1=malloc(grid_size);
if(!point1) return -1;
getimage(x,y,x+15,y+15,point1);
putimage(x1,y1,point1,COPY_PUT);
free(point1);
}
void get_image_food( int food_x,int food_y)
{
int grid_size;
grid_size=imagesize(0,0,15,15);
image_food=malloc(grid_size);
if(!image_food) return -1;
getimage(food_x,food_y,food_x+15,food_y+15,image_food);
}
void copy_image_food(int x,int y)
{
putimage(x,y,image_food,COPY_PUT);
}
void graph_open()
{
initgraph(&gdriver, &gmode,"");
errorcode = graphresult();
system("\cls");
if (errorcode != 0)
{
printf("Graphics error: %s \n", grapherrormsg(errorcode));
printf("press any key to halt:");
getch();
}
else
{
system("\cls");
}
}
void graph_close()
{
closegraph();
}
void disp_init_scr()
{
int i,j;
for(j=0;j<=win_y;j++)
for(i=0;i<=win_x;i++)
{
if( (i<2) || (i>win_x-2) || (j<2) || (j>win_y-2) )
scr[j][i]=snake_wall;
else
scr[j][i]=snake_other;
}
disp_char[0]='x';
disp_char[1]='\0';
for( i = 0 ; i < 1200 ; i++ )
{
snake_x[i] = -1;
snake_y[i] = -1;
}
disp_window();
}
void disp_window()
{
int i,j;
cleardevice();
for(j=0;j<=win_y;j++)
{
for(i=0;i<=win_x;i++)
{
disp_char[0]=scr[j][i];
/*if(disp_char[0]!='Z')*/
outtextxy(i*16,j*16,disp_char);
}
}
}
void seek_snake()
{
int i=win_x,j=win_y,seek_tail=0;
int seek_result=0,seek_random=0;
for(j=0;j<=win_y;j++)
for(i=0;i<=(win_x-6);i++)
{
if(scr[j][i]==snake_other&&scr[j][i+1]==snake_other&&
scr[j][i+2]==snake_other&&scr[j][i+3]==snake_other&&
scr[j][i+4]==snake_other&&scr[j][i+5]==snake_other)
{
seek_result++;
}
}
if(seek_result<=0)
game_over();
srand( (unsigned)time(NULL) );
seek_random = rand() % seek_result ;
for( seek_result = 0 , j = 0 ; j <= win_y ; j++ )
for( i = 0 ; i <= ( win_x - 6 ) ; i++ )
{
if(scr[j][i]==snake_other&&scr[j][i+1]==snake_other&&
scr[j][i+2]==snake_other&&scr[j][i+3]==snake_other&&
scr[j][i+4]==snake_other&&scr[j][i+5]==snake_other)
{
if( seek_result == seek_random )
{
scr[j][i+3]=snake_top;
snake_x[0]=i+3;
snake_y[0]=j;
scr[j][i+2]=snake_middle;
snake_x[1]=i+2;
snake_y[1]=j;
scr[j][i+1]=snake_tail;
snake_x[2]=i+1;
snake_y[2]=j;
disp_window();
return(0);
}
seek_result++;
}
}
}
void snake_eat_food()
{
}
void init_snake_food()
{
int i=win_x,j=win_y,seek_tail=0;
int seek_result=0,seek_random=0;
if(food_status==0)
{
for(j=0;j<=win_y;j++)
for(i=0;i<=win_x;i++)
{
if(scr[j][i]==snake_other)
{
seek_result++;
}
}
if(seek_result <= 0 )
game_over();
srand( (unsigned)time(NULL) );
seek_random = rand() % seek_result ;
for( seek_result = 0 , j = 0 ; j <= win_y ; j++ )
for( i = 0 ; i <= win_x ; i++ )
{
if(scr[j][i]==snake_other)
{
if( seek_result == seek_random )
{
scr[j][i]=snake_food;
food_status = 1 ;
clear_grid(i*16,j*16);
outtextxy(i*16,j*16,"F");
get_image_food(i*16,j*16 );
return(0);
}
seek_result++;
}
}
}
else
return(0);
}
void check_create_snake_food()
{
int i=win_x,j=win_y,seek_tail=0;
int seek_result=0,seek_random=0;
if(food_status==0)
{
for(j=0;j<=win_y;j++)
for(i=0;i<=win_x;i++)
{
if(scr[j][i]==snake_other)
{
seek_result++;
}
}
if(seek_result <= 0 )
game_over();
srand( (unsigned)time(NULL) );
seek_random = rand() % seek_result ;
for( seek_result = 0 , j = 0 ; j <= win_y ; j++ )
for( i = 0 ; i <= win_x ; i++ )
{
if(scr[j][i]==snake_other)
{
if( seek_result == seek_random )
{
scr[j][i]=snake_food;
food_status = 1 ;
clear_grid(i*16,j*16);
copy_image_food(i*16,j*16 );
return(0);
}
seek_result++;
}
}
}
else
return(0);
}
void disp_snake_step()
{
int i,j,k,i_top,j_top,i_tail,j_tail,i_middle,j_middle,i_next,j_next;
i_top = snake_x[0];
j_top = snake_y[0];
i_tail = snake_x[snake_length-1];
j_tail = snake_y[snake_length-1];
j_next = (snake_y[0]) + top_offset_y ;
i_next = (snake_x[0]) + top_offset_x ;
if( (scr[ j_next ][ i_next ] != snake_other) &&(scr[ j_next ][ i_next ] != snake_food) )
{
snake_life = 0;
game_over();
return(0);
}
else
if( scr[ (snake_y[0])+top_offset_y ][( (snake_x[0]) +
top_offset_x ) ] == snake_other )
{
for( k = 0 ; k < snake_length ; k++ )
{
if(k==0)
{
i = snake_x[k];
j = snake_y[k];
i_middle = snake_x[k] + top_offset_x;
j_middle = snake_y[k] + top_offset_y;
copy_grid( i * 16 , j * 16 , i_middle * 16 , j_middle * 16 ) ;
scr[j_middle][i_middle] = snake_top;
}
else
{
i = snake_x[k];
j = snake_y[k];
i_middle = snake_x[k-1];
j_middle = snake_y[k-1];
copy_grid( i * 16 , j * 16 , i_middle * 16 , j_middle * 16 ) ;
if( k < ( snake_length-1 ) )
{
scr[j_middle][i_middle] = snake_middle;
}
if( k == ( snake_length-1 ) )
{
scr[j_middle][i_middle] = snake_tail;
scr[j][i] = snake_other;
clear_grid( i * 16 , j * 16 ) ;
}
}
}
for( k = 0 ; k < snake_length ; k++ )
{
if( k < ( snake_length - 1) )
{
snake_x[snake_length-1-k] = snake_x[snake_length-2-k] ;
snake_y[snake_length-1-k] = snake_y[snake_length-2-k] ;
}
if( k == snake_length - 1 )
{
snake_x[snake_length-1-k] = i_top + top_offset_x ;
snake_y[snake_length-1-k] = j_top + top_offset_y;
}
}
}
else
if( scr[ (snake_y[0])+top_offset_y ][( (snake_x[0]) +
top_offset_x ) ] == snake_food )
{
for( k = 0 ; k < snake_length ; k++ )
{
if(k==0)
{
i = snake_x[k];
j = snake_y[k];
i_middle = snake_x[k] + top_offset_x;
j_middle = snake_y[k] + top_offset_y;
copy_grid( i * 16 , j * 16 , i_middle * 16 , j_middle * 16 ) ;
scr[j_middle][i_middle] = snake_top;
}
else
{
i = snake_x[k];
j = snake_y[k];
i_middle = snake_x[k-1];
j_middle = snake_y[k-1];
if( k < ( snake_length-1 ) )
{
copy_grid( i * 16 , j * 16 , i_middle * 16 , j_middle * 16 ) ;
scr[j_middle][i_middle] = snake_middle;
}
}
}
for( k = 0 ; k <= snake_length ; k++ )
{
if( k < ( snake_length ) )
{
snake_x[snake_length-k] = snake_x[snake_length-1-k] ;
snake_y[snake_length-k] = snake_y[snake_length-1-k] ;
}
if( k == snake_length )
{
snake_x[snake_length-k] = i_top + top_offset_x;
snake_y[snake_length-k] = j_top + top_offset_y;
}
}
snake_length++;
food_times++;
food_status = 0 ;
check_create_snake_food();
}
}