作者在 2008-08-28 12:46:54 发布以下内容
#include <graphics.h>
#include <bios.h>
#include <stdlib.h>
#include <bios.h>
#include <stdlib.h>
#define ESC 0x011b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
struct Snake{int x,y;} snak[500];
struct Food {int x,y;} food;
int float_esc,float_up,float_down,float_left,float_right,float_eat=0;
int score,newscore,num,speed,chackx,chacky;
int key,begin=0;
struct Food {int x,y;} food;
int float_esc,float_up,float_down,float_left,float_right,float_eat=0;
int score,newscore,num,speed,chackx,chacky;
int key,begin=0;
void drawmap();
void headmv();
void setfloat(int,int,int,int);
void casekey();
void startgame();
void intsnak();
void intfood();
void gameover();
void botmv();
void botadd();
void pscore();
void drawgame(int,int,int);
void headmv();
void setfloat(int,int,int,int);
void casekey();
void startgame();
void intsnak();
void intfood();
void gameover();
void botmv();
void botadd();
void pscore();
void drawgame(int,int,int);
void drawmpa(){
line( 3, 3, 3,420);
line( 3, 3,630, 3);
line(630, 3,630,420);
line( 3,420,630,420);
line( 3, 3, 3,420);
line( 3, 3,630, 3);
line(630, 3,630,420);
line( 3,420,630,420);
line( 3,425, 3,475);
line( 3,425,630,425);
line(630,425,630,475);
line( 3,475,630,475);
}
line( 3,425,630,425);
line(630,425,630,475);
line( 3,475,630,475);
}
void setfloat(int a,int b,int c,int d){
float_up = a;
float_down = b;
float_left = c;
float_right = d;
}
float_up = a;
float_down = b;
float_left = c;
float_right = d;
}
void casekey(){
if(bioskey(1)) key=bioskey(0);
if(begin==0) {key=LEFT;begin++;}
switch(key){
case UP:
setfloat(1,0,0,0);
break;
case DOWN:
setfloat(0,1,0,0);
break;
case LEFT:
setfloat(0,0,1,0);
break;
case RIGHT:
setfloat(0,0,0,1);
break;
case ESC:
exit (0);
default:setfloat(0,0,0,0);
}
}
if(bioskey(1)) key=bioskey(0);
if(begin==0) {key=LEFT;begin++;}
switch(key){
case UP:
setfloat(1,0,0,0);
break;
case DOWN:
setfloat(0,1,0,0);
break;
case LEFT:
setfloat(0,0,1,0);
break;
case RIGHT:
setfloat(0,0,0,1);
break;
case ESC:
exit (0);
default:setfloat(0,0,0,0);
}
}
void intsnak(){
randomize();
snak[0].x=random(630);
if(snak[0].x<3) snak[0].x+=10;
snak[0].x=snak[0].x-snak[0].x%20+10;
snak[0].y=random(420);
if(snak[0].y<3) snak[0].y+=10;
snak[0].y=snak[0].y-snak[0].y%20+10;
snak[1].x=snak[0].x+10;
snak[1].y=snak[0].y;
}
randomize();
snak[0].x=random(630);
if(snak[0].x<3) snak[0].x+=10;
snak[0].x=snak[0].x-snak[0].x%20+10;
snak[0].y=random(420);
if(snak[0].y<3) snak[0].y+=10;
snak[0].y=snak[0].y-snak[0].y%20+10;
snak[1].x=snak[0].x+10;
snak[1].y=snak[0].y;
}
void intfood(){
randomize();
food.x=random(630);
food.x=food.x-food.x%20-10;
if(food.x<3) food.x+=20;
food.y=random(420);
food.y=food.y-food.y%20-10;
if(food.y<3) food.y+=20;
drawgame(food.x,food.y,3);
}
randomize();
food.x=random(630);
food.x=food.x-food.x%20-10;
if(food.x<3) food.x+=20;
food.y=random(420);
food.y=food.y-food.y%20-10;
if(food.y<3) food.y+=20;
drawgame(food.x,food.y,3);
}
void startgame(){
intsnak();
intfood();
score=0;
newscore=0;
num=2;
speed=50000;
}
intsnak();
intfood();
score=0;
newscore=0;
num=2;
speed=50000;
}
void headmv(){
chackx=snak[0].x;
chacky=snak[0].y;
if(float_up){
drawgame(snak[0].x,snak[0].y,0);
snak[0].y-=10;
drawgame(snak[0].x,snak[0].y,15);
}
if(float_down){
drawgame(snak[0].x,snak[0].y,0);
snak[0].y+=10;
drawgame(snak[0].x,snak[0].y,15);
}
if(float_left){
drawgame(snak[0].x,snak[0].y,0);
snak[0].x-=10;
drawgame(snak[0].x,snak[0].y,15);
}
if(float_right){
drawgame(snak[0].x,snak[0].y,0);
snak[0].x+=10;
drawgame(snak[0].x,snak[0].y,15);
}
}
chackx=snak[0].x;
chacky=snak[0].y;
if(float_up){
drawgame(snak[0].x,snak[0].y,0);
snak[0].y-=10;
drawgame(snak[0].x,snak[0].y,15);
}
if(float_down){
drawgame(snak[0].x,snak[0].y,0);
snak[0].y+=10;
drawgame(snak[0].x,snak[0].y,15);
}
if(float_left){
drawgame(snak[0].x,snak[0].y,0);
snak[0].x-=10;
drawgame(snak[0].x,snak[0].y,15);
}
if(float_right){
drawgame(snak[0].x,snak[0].y,0);
snak[0].x+=10;
drawgame(snak[0].x,snak[0].y,15);
}
}
void botmv(){
int i;
int tempx,tempy;
for(i=1;i<num;i++) {
drawgame(snak[i].x,snak[i].y,0);
drawgame(chackx,chacky,15);
tempx=snak[i].x;
tempy=snak[i].y;
snak[i].x=chackx;
snak[i].y=chacky;
chackx=tempx;
chacky=tempy;
}
}
int i;
int tempx,tempy;
for(i=1;i<num;i++) {
drawgame(snak[i].x,snak[i].y,0);
drawgame(chackx,chacky,15);
tempx=snak[i].x;
tempy=snak[i].y;
snak[i].x=chackx;
snak[i].y=chacky;
chackx=tempx;
chacky=tempy;
}
}
void gameover(){
char str[30];
int i;
setcolor(3);
if(snak[0].x<=3||snak[0].x>=630||snak[0].y<=3||snak[0].y>=420){
sprintf(str,"Game Over Your score %d",newscore);
settextstyle(1,0,2);
outtextxy(200,200,str);
if(getch()) exit(0);
}
for(i=3;i<num;i++)
if(snak[0].x==snak[i].x && snak[0].y==snak[i].y){
sprintf(str,"Game Over Your score %d",newscore);
settextstyle(1,0,2);
outtextxy(200,200,str);
if(getch()) exit(0);
}
}
char str[30];
int i;
setcolor(3);
if(snak[0].x<=3||snak[0].x>=630||snak[0].y<=3||snak[0].y>=420){
sprintf(str,"Game Over Your score %d",newscore);
settextstyle(1,0,2);
outtextxy(200,200,str);
if(getch()) exit(0);
}
for(i=3;i<num;i++)
if(snak[0].x==snak[i].x && snak[0].y==snak[i].y){
sprintf(str,"Game Over Your score %d",newscore);
settextstyle(1,0,2);
outtextxy(200,200,str);
if(getch()) exit(0);
}
}
void botadd(){
if(snak[0].x==food.x && snak[0].y==food.y){
newscore++;
speed-=100;
float_eat=1;
if(snak[num-1].x>snak[num-2].x){
num++;
snak[num-1].x=snak[num-2].x+10;
snak[num-1].y=snak[num-2].y;
}
else if(snak[num-1].x<snak[num-2].x){
num++;
snak[num-1].x=snak[num-2].x-10;
snak[num-1].y=snak[num-2].y;
}
else if(snak[num-1].y<snak[num-2].y){
num++;
snak[num-1].y=snak[num-2].y-10;
snak[num-1].x=snak[num-2].x;
}
else{
num++;
snak[num-1].y=snak[num-2].y+10;
snak[num-1].x=snak[num-2].x;
}
}
}
if(snak[0].x==food.x && snak[0].y==food.y){
newscore++;
speed-=100;
float_eat=1;
if(snak[num-1].x>snak[num-2].x){
num++;
snak[num-1].x=snak[num-2].x+10;
snak[num-1].y=snak[num-2].y;
}
else if(snak[num-1].x<snak[num-2].x){
num++;
snak[num-1].x=snak[num-2].x-10;
snak[num-1].y=snak[num-2].y;
}
else if(snak[num-1].y<snak[num-2].y){
num++;
snak[num-1].y=snak[num-2].y-10;
snak[num-1].x=snak[num-2].x;
}
else{
num++;
snak[num-1].y=snak[num-2].y+10;
snak[num-1].x=snak[num-2].x;
}
}
}
void drawgame(int x,int y,int c){
setcolor(c);
circle(x,y,5);
}
setcolor(c);
circle(x,y,5);
}
void pscore(){
char str[30];
char news[30];
sprintf(str,"Your score %d",score);
sprintf(news,"Your score %d",newscore);
settextstyle(1,0,2);
if(newscore==score) outtextxy(5,430,news);
else{
setcolor(0);
outtextxy(5,430,str);
setcolor(3);
outtextxy(5,430,news);
score=newscore;
}
}
char str[30];
char news[30];
sprintf(str,"Your score %d",score);
sprintf(news,"Your score %d",newscore);
settextstyle(1,0,2);
if(newscore==score) outtextxy(5,430,news);
else{
setcolor(0);
outtextxy(5,430,str);
setcolor(3);
outtextxy(5,430,news);
score=newscore;
}
}
void main(){
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"E:\bianyiqi\TURBOC2");
startgame();
for(;;){
float_eat=0;
drawmpa();
pscore();
casekey();
headmv();
botmv();
gameover();
botadd();
if(float_eat){for(;;){
int i;
intfood();
for(i=0;;i++) {
if(food.x==snak[i].x&&food.y==snak[i].y) intfood();
if(i==num) break;
}
break;
}
}
else drawgame(food.x,food.y,3);
delay(speed);
}
closegraph();
}
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"E:\bianyiqi\TURBOC2");
startgame();
for(;;){
float_eat=0;
drawmpa();
pscore();
casekey();
headmv();
botmv();
gameover();
botadd();
if(float_eat){for(;;){
int i;
intfood();
for(i=0;;i++) {
if(food.x==snak[i].x&&food.y==snak[i].y) intfood();
if(i==num) break;
}
break;
}
}
else drawgame(food.x,food.y,3);
delay(speed);
}
closegraph();
}