| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 529 人关注过本帖
标题:一个有关贪吃蛇的c程序 可是在tc20上运行的不好 是程序哪里出了问题 ...
只看楼主 加入收藏
lilycat
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-6-30
收藏
 问题点数:0 回复次数:0 
一个有关贪吃蛇的c程序 可是在tc20上运行的不好 是程序哪里出了问题 求教



001 #include <graphics.h>

002 #include <stdlib.h>

003 #include <conio.h>

004 #include <time.h>

005 #include <stdio.h>

006 #include "snake.h"

007

008 int score = 0;

009 int gamespeed = 100;



//蛇运行速度

010

011 static void Init(void);


/*图形驱动*/
012 static void Close(void);


/*图形竣事*/
013 static void Game_interface(void);
/*游戏界面*/
014 static void GameOver(void);

/*竣事游戏*/
015 static void GamePlay(void);

/*游戏过程*/
016 static void PrScore(void);


/*输出成就*/
017

018 /*主函数*/
019 int main(void)

020 {

021

Init();





022

Game_interface();





023

GamePlay();




024

Close();





025

return 0;

026 }

027

028 /*图形驱动*/
029 static void Init(void)

030 {

031
int gd=9,gm=2;

032

033
initgraph(&gd,&gm," ");

034
cleardevice();

035 }

036

037 /* 预备画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙 */
038 static void Game_interface(void)

039 {

040

int i;

041

042

setcolor(LIGHTCYAN);






/*setbkcolor(LIGHTGREEN);*/
043

setlinestyle(PS_SOLID,0,1);




/*设置线型*/
044

for(i=50;i<=600;i+=10)






/*画边框*/
045

{

046


rectangle(i,40,i+10,49);




/*上边框*/
047


rectangle(i,451,i+10,460);



/*下边框*/
048

}

049

for(i=40;i<=450;i+=10)

050

{

051


rectangle(50,i,59,i+10);




/*左边框*/
052


rectangle(601,i,610,i+10);



/*右边框*/
053

}

054 }

055

056 /* 游戏主函数 */
057 static void GamePlay(void)

058 {

059

int i;

060


061

srand(time(NULL));






/*随机数发生器*/
062

food.yes = 1;








/*1暗示需要呈现新食物,0暗示已经存在食物*/
063

snake.life = 0;







/*在世*/
064

snake.direction = 1;






/*标的目的往右*/
065

snake.x[0] = 100;

066

snake.y[0] = 100;








067

snake.x[1] = 110;

068

snake.y[1] = 100;

069

snake.node = 2;







/*节数*/
070

071

PrScore();








/*输出得分*/
072

while(1)









/*可以一再玩游戏,压ESC键竣事*/
073

{

074


while( !kbhit() )






/*在没有按键的情形下,蛇自己移动*/
075


{

076



if(food.yes == 1)





/*需要呈现新食物*/
077



{

078




food.x = rand()%400 + 60;

079




food.y = rand()%350 + 60;

080




while(food.x%10 != 0)



/*食物随机呈现后必需让食物能够在整格内,这样才可以让蛇吃到*/
081





food.x++;

082




while(food.y%10 != 0)

083





food.y++;

084




food.yes = 0;





/*画面上有食物了*/
085



}

086



if(food.yes == 0)





/*画面上有食物了就要显示*/
087



{

088




setcolor(GREEN);

089




rectangle(food.x,food.y,food.x + 10,food.y - 10);

090



}

091

092

093



for(i=snake.node-1;i>0;i--)


/*蛇的每个环节往前移动,也就是贪吃蛇的关头算法*/
094



{

095




snake.x = snake.x[i-1];

096




snake.y = snake.y[i-1];

097



}

098



/*1,2,3,4暗示右,左,上,下四个标的目的,经由过程这个判定来移动蛇头*/
099



switch(snake.direction)

100



{

101




case 1:

102





snake.x[0] += 10;

103





break;

104




case 2:

105





snake.x[0] -= 10;

106





break;

107




case 3:

108





snake.y[0] -= 10;

109





break;

110




case 4:

111





snake.y[0] += 10;

112





break;

113



}

114



/* 从蛇的第四节预备判定是否撞到自己 */
115



for(i=3;i<snake.node;i++)

116



{

117




if((snake.x == snake.x[0]) && (snake.y == snake.y[0]))

118




{

119





GameOver();




/*显示失踪败*/
120





snake.life = 1;

121





break;

122




}

123



}

124



if((snake.x[0] < 55) || (snake.x[0] > 595) || (snake.y[0] < 55) || (snake.y[0] > 455))
/*蛇是否撞到墙壁*/
125



{

126

127

128




GameOver();










/*本次游戏竣事*/
129




snake.life = 1;









/*蛇死*/
130



}

131



if(snake.life == 1)









/*以上两种判定往后,若是蛇死就跳出内轮回,从头预备*/
132




break;

133



if((snake.x[0] == food.x) && (snake.y[0] == food.y))

/*吃到食物往后*/
134



{

135




setcolor(BLACK);









/*把画面上的食物工具去失踪*/
136




rectangle(food.x,food.y,food.x+10,food.y-10);

137




snake.x[snake.node] = -20;

138




snake.y[snake.node] = -20;

139

140




/* 新的一节先放在看不见的位置,下次轮回就取前一节的位置 */
141

142




snake.node++;





/*蛇的身体长一节*/
143




food.yes = 1;





/*画面上需要呈现新的食物*/
144




score += 10;

145




PrScore();





/*输出新得分*/
146



}

147



setcolor(RED);





/*画出蛇*/
148

149

150



for(i=0;i<snake.node;i++)

151




rectangle(snake.x,snake.y,snake.x+10,snake.y-10);

152



Sleep(gamespeed);

153



setcolor(BLACK);





/*用黑色去除蛇的的最后一节*/
154



rectangle(snake.x[snake.node-1],snake.y[snake.node-1],

155





snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);

156


}

/*endwhile(!kbhit)*/
157



158


if(snake.life == 1)





/*若是蛇死就跳出轮回*/
159



break;

160



161


key=getch();






/*领受按键*/
162


if (key == ESC) break;




/*按ESC键退出*/
163



164


switch(key)

165


{









166



case UP:

167




if(snake.direction != 4)


/*判定是否往相反的标的目的移动*/
168





snake.direction = 3;

169




break;

170



case RIGHT:

171




if(snake.direction != 2)

172





snake.direction = 1;

173




break;

174



case LEFT:

175




if(snake.direction != 1)

176





snake.direction = 2;

177




break;

178



case DOWN:

179




if(snake.direction != 3)

180





snake.direction = 4;

181




break;

182


}

183

184

}/*endwhile(1)*/
185 }

186

187 /*游戏竣事*/
188 static void GameOver(void)

189 {

190

cleardevice();

191

PrScore();

192

setcolor(RED);

193

setfont(56,0,"黑体");

194

outtextxy(200,200,"GAME OVER");

195

getch();

196 }

197

198 /*输出成就*/
199 static void PrScore(void)

200 {

201

char str[10];

202

203

setfillstyle(YELLOW);

204

bar(50,15,220,35);

205

setcolor(BROWN);

206

setfont(16,0,"宋体");

207

sprintf(str,"score:%d",score);

208

outtextxy(55,16,str);

209 }

210

211 static void Close(void)

212 {

213

closegraph();

214 }
01 #ifndef SNAKE_H

02 #define SNAKE_H

03

04

05 #define LEFT 'a'

06 #define RIGHT 'd'

07 #define DOWN 's'

08 #define UP 'w'

09 #define ESC 27

10

11 #define N 200



/*蛇的最大长度*/

12

13 char key;




/*节制按键*/
14

15

16 struct Food

17 {

18

int x;




/*食物的横坐标*/
19

int y;




/*食物的纵坐标*/
20

int yes;



/*判定是否要呈现食物的变量*/
21 }food;





/*食物的结构体*/
22

23 struct Snake

24 {

25

int x[N];

26

int y[N];

27

int node;



/*蛇的节数*/
28

int direction;


/*蛇移动标的目的*/
29

int life;



/* 蛇的生命,0在世,1衰亡*/
30 }snake;

31

32

33

34 #endif


 
搜索更多相关主题的帖子: void 游戏 include 贪吃蛇 c程序 
2012-06-30 09:53
快速回复:一个有关贪吃蛇的c程序 可是在tc20上运行的不好 是程序哪里出了问 ...
数据加载中...
 
   



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

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