| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1007 人关注过本帖, 1 人收藏
标题:[求助]c语言贪食蛇问题求教,有源程序
只看楼主 加入收藏
xiaogui8023
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-9-22
收藏(1)
 问题点数:0 回复次数:3 
[求助]c语言贪食蛇问题求教,有源程序

我编写了一个程序,还要求得到一定的分数,速度等级会变快,但是我写了蛇会出现吃到一定的数量后,蛇的速度是加快了,可是会忽块忽慢的,请高手帮忙解决.
#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=50000;
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 Init(void);
void Close(void);
void DrawK(void);
void GameOver(void);
void GamePlay(void);
void PrScore(void);

void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}

void DrawK(void)
{

setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
for(i=50;i<=600;i+=10)
{
rectangle(i,40,i+10,49);
rectangle(i,451,i+10,460);
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10);
rectangle(601,i,610,i+10);
}
}

void GamePlay(void)
{
randomize();
food.yes=1;
snake.life=0;
snake.direction=1;
snake.x[0]=100;snake.y[0]=100;
snake.x[1]=110;snake.y[1]=100;
snake.node=2;
PrScore();
while(1)
{
while(!kbhit())
{
if(food.yes==1)
{
food.x=rand()%400+60;
food.y=rand()%350+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<snake.node;i++)
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
GameOver();
snake.life=1;
break;
}
}
if(snake.x[0]<55||snake.x[0]>595||snake.y[0]<55||
snake.y[0]>455)
{
GameOver();
snake.life=1;
}
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;
PrScore();
}
setcolor(4);
for(i=0;i<snake.node;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 str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}

void Close(void)
{
getch();
closegraph();
}
void main(void)
{ Init();
DrawK();
GamePlay();
Close();
}

搜索更多相关主题的帖子: c语言 int define include 
2006-09-22 12:38
cdmalcl
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:4091
专家分:524
注 册:2005-9-23
收藏
得分:0 

int boxtime(int stime)
{
tnew=biostime(0,tnew);
if(abs(tnew-told)>=stime)
{
told=tnew;
return 1;
}

return 0;
}

控制时间用这个吧

2006-09-22 19:14
红梅
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-8-27
收藏
得分:0 

for(i=snake.node-1;i>0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
有了上面那句话下面这句就没有什么意思了,可以删了
snake.x[1]=110;snake.y[1]=100;
我是这么编写食物的
int SiWu()
{
randomize();
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0)
food.x++;
while(food.y%10!=0)
food.y++;

}

然后把
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
放在里层循环,不然蛇走过食物边线时食物边线就看不见了
进GamePlay调用一次SiWu()当蛇吃到食物后再调用一次SiWu




2006-09-22 21:42
dxl5109
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-1-18
收藏
得分:0 
呵呵,楼主很强大~~我在机器上也编写了这个程序,可为什么编译的时候提醒有一个错误,大概意思是说找不到库函数文件graphics.h,希望楼主能帮忙解决,谢谢!
2010-01-24 20:56
快速回复:[求助]c语言贪食蛇问题求教,有源程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022422 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved