求贪吃蛇的c语言代码,觉得挺好玩的
答案:1 悬赏:0 手机版
解决时间 2021-03-28 14:39
- 提问者网友:暗中人
- 2021-03-27 20:05
求贪吃蛇的c语言代码,觉得挺好玩的
最佳答案
- 五星知识达人网友:長槍戰八方
- 2021-03-27 20:17
#include
#include
#include
#include
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define N 200
int i,key;
int level;
int score=0;
int gamespeed;
struct Food
{
int x;
int y;
int yes;
}food;
struct Snake
{
int x[N];
int y[N];
int node;
int direction;
int life;
}snake;
void Choicelevle(void);
void Init(void);
void Close(void);
void DRAW(void);
void GameOver(void);
void GamePlay(void);
void PrScore(void);
void main(void)
{
Init();
Choicelevle();
DRAW();
GamePlay();
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"\\turboc2");
cleardevice();
}
void Choicelevle(void)
{char name[20];
setcolor(YELLOW);
settextstyle(0,0,6);
outtextxy(150,150,"Snake");
setcolor(GREEN);
settextstyle(0,0,1);
outtextxy(200,250,"please put in your English name:");
outtextxy(200,270,"Choice levle from 1-9.");
outtextxy(300,320,"name:yangzilong");
outtextxy(300,350,"number:0902060226");
outtextxy(300,380,"class:computer science 0602");
getch();
printf("please putin your name:");
gets(name);
printf("please choice levle:");
scanf("%d",&level);
gamespeed=100000-400*level-300*level*level;
if(level>9||level<1)
{cleardevice();
setcolor(YELLOW);
settextstyle(0,0,2);
outtextxy(150,200,"level input error");
getch();
level=1;
}
}
void DRAW(void)
{cleardevice();
setcolor(2);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
rectangle(45,45,465,325);
}
void GamePlay(void)
{setcolor(5);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
randomize();
food.yes=1;
snake.life=0;
snake.direction=1;
snake.x[0]=320;snake.y[0]=240;
snake.x[1]=330;snake.y[1]=240;
snake.node=3;
PrScore();
while(1)
{
while(!kbhit())
{
if(food.yes==1)
{
food.x=rand()%360+70;
food.y=rand()%250+60;
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;
}
if(food.yes==0)
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch(snake.direction)
{
case 1: snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;i {
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
if(snake.x[0]<50)
{snake.x[0]=450;
snake.y[0]=snake.y[0];
for(i=snake.node-1;i>0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
{
setfillstyle(SOLID_FILL,0);
bar(50,55,455,315);
}
}
else
if(snake.x[0]>450)
{snake.x[0]=50;
snake.y[0]=snake.y[0];
for(i=snake.node-1;i>0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
{
setfillstyle(SOLID_FILL,0);
bar(50,55,455,315);
}
}
else
if(snake.y[0]<60)
{snake.y[0]=320;
snake.x[0]=snake.x[0];
for(i=snake.node-1;i>0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
{
setfillstyle(SOLID_FILL,0);
bar(50,55,455,315);
}
}
else
if(snake.y[0]>320)
{snake.y[0]=60;
snake.x[0]=snake.x[0];
for(i=snake.node-1;i>0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
{
setfillstyle(SOLID_FILL,0);
bar(50,55,455,315);
}
}
if(snake.life==1)
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
snake.node++;
food.yes=1;
score+=10;
if(score%100==0)
{level++;gamespeed=100000-400*level-300*level*level;
PrScore();
setcolor(YELLOW);
settextstyle(0,0,4);
outtextxy(150,200,"LEVEL UP");
if(level==10)
{level=1,gamespeed=100000-400*level-300*level*level;}
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
bar(50,55,455,315);
}
PrScore();
}
setcolor(4);
for(i=0;i rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(snake.life==1)
break;
key=bioskey(0);
if(key==ESC)
break;
else
if(key==UP&&snake.direction!=4)
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}
}
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
void PrScore(void)
{
char str1[20];
setfillstyle(SOLID_FILL,0);
bar(50,15,390,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str1,"score %d level %d",score,level);
outtextxy(55,20,str1);
setcolor(YELLOW);
settextstyle(0,0,2);
outtextxy(250,400,"EXIT=ESC ");
}
void Close(void)
{
closegraph();
}追问蟹蟹(*^ω^*)
#include
#include
#include
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define N 200
int i,key;
int level;
int score=0;
int gamespeed;
struct Food
{
int x;
int y;
int yes;
}food;
struct Snake
{
int x[N];
int y[N];
int node;
int direction;
int life;
}snake;
void Choicelevle(void);
void Init(void);
void Close(void);
void DRAW(void);
void GameOver(void);
void GamePlay(void);
void PrScore(void);
void main(void)
{
Init();
Choicelevle();
DRAW();
GamePlay();
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"\\turboc2");
cleardevice();
}
void Choicelevle(void)
{char name[20];
setcolor(YELLOW);
settextstyle(0,0,6);
outtextxy(150,150,"Snake");
setcolor(GREEN);
settextstyle(0,0,1);
outtextxy(200,250,"please put in your English name:");
outtextxy(200,270,"Choice levle from 1-9.");
outtextxy(300,320,"name:yangzilong");
outtextxy(300,350,"number:0902060226");
outtextxy(300,380,"class:computer science 0602");
getch();
printf("please putin your name:");
gets(name);
printf("please choice levle:");
scanf("%d",&level);
gamespeed=100000-400*level-300*level*level;
if(level>9||level<1)
{cleardevice();
setcolor(YELLOW);
settextstyle(0,0,2);
outtextxy(150,200,"level input error");
getch();
level=1;
}
}
void DRAW(void)
{cleardevice();
setcolor(2);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
rectangle(45,45,465,325);
}
void GamePlay(void)
{setcolor(5);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
randomize();
food.yes=1;
snake.life=0;
snake.direction=1;
snake.x[0]=320;snake.y[0]=240;
snake.x[1]=330;snake.y[1]=240;
snake.node=3;
PrScore();
while(1)
{
while(!kbhit())
{
if(food.yes==1)
{
food.x=rand()%360+70;
food.y=rand()%250+60;
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0;
}
if(food.yes==0)
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i>0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch(snake.direction)
{
case 1: snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=3;i
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
if(snake.x[0]<50)
{snake.x[0]=450;
snake.y[0]=snake.y[0];
for(i=snake.node-1;i>0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
{
setfillstyle(SOLID_FILL,0);
bar(50,55,455,315);
}
}
else
if(snake.x[0]>450)
{snake.x[0]=50;
snake.y[0]=snake.y[0];
for(i=snake.node-1;i>0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
{
setfillstyle(SOLID_FILL,0);
bar(50,55,455,315);
}
}
else
if(snake.y[0]<60)
{snake.y[0]=320;
snake.x[0]=snake.x[0];
for(i=snake.node-1;i>0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
{
setfillstyle(SOLID_FILL,0);
bar(50,55,455,315);
}
}
else
if(snake.y[0]>320)
{snake.y[0]=60;
snake.x[0]=snake.x[0];
for(i=snake.node-1;i>0;i--)
{snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
{
setfillstyle(SOLID_FILL,0);
bar(50,55,455,315);
}
}
if(snake.life==1)
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)
{
setcolor(0);
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
snake.node++;
food.yes=1;
score+=10;
if(score%100==0)
{level++;gamespeed=100000-400*level-300*level*level;
PrScore();
setcolor(YELLOW);
settextstyle(0,0,4);
outtextxy(150,200,"LEVEL UP");
if(level==10)
{level=1,gamespeed=100000-400*level-300*level*level;}
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
delay(6000000);
bar(50,55,455,315);
}
PrScore();
}
setcolor(4);
for(i=0;i
snake.y[i]-10);
delay(gamespeed);
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
}
if(snake.life==1)
break;
key=bioskey(0);
if(key==ESC)
break;
else
if(key==UP&&snake.direction!=4)
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}
}
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
void PrScore(void)
{
char str1[20];
setfillstyle(SOLID_FILL,0);
bar(50,15,390,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str1,"score %d level %d",score,level);
outtextxy(55,20,str1);
setcolor(YELLOW);
settextstyle(0,0,2);
outtextxy(250,400,"EXIT=ESC ");
}
void Close(void)
{
closegraph();
}追问蟹蟹(*^ω^*)
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯