c语言字符蛇代码,贪吃蛇游戏c语言源贪吃蛇代码代码学习

1########蛇的状态,U:上;D:下;L:左R:右蛇身的一个节点12{

13intx;

14inty;

15structSNAKE*next;

16}snake;

1718//全局变量//

19intscore=0,add=10;//总得分与每次吃食物得分。20intstatus,sleeptime=200;//每次运行的时间间隔21snake*head,*food;//蛇头指针,食物指针22snake*q;//遍历蛇的时候用到的指针23intendGamestatus=0;//游戏结束的情况,1:撞到墙;2:咬到自己;3:主动退出游戏。

2425//声明全部函数//

26voidPos();

27voidcreatMap();

28voidinitSnake();

29intbiteSelf();

30voidcreateFood();

31voidcantCrossWall();

32voidsnakeMove();

33voidpause();

34voidrunGame();

35voidinitGame();

36voidendGame();

37voidgameStart();

3839voidPos(intx,inty)//设置光标位置40{

41COORDpos;

42HANDLEhOutput;

43pos.X=x;

44pos.Y=y;

45hOutput=GetStdHandle(STD_OUTPUT_HANDLE);//返回标准的输入、输出或错误的设备的句柄,也就是获得输入、输出/错误的屏幕缓冲区的句柄46SetConsoleCursorPosition(hOutput,pos);

47}

4849voidcreatMap()//创建地图50{

51inti;

52for(i=0;ii+=2)//打印上下边框53{

54Pos(i,0);

55printf("■");//一个方块占两个位置56Pos(i,26);

57printf("■");

58}

59for(i=1;ii++)//打印左右边框60{

61Pos(0,i);

62printf("■");

63Pos(56,i);

64printf("■");

65}

66}

6768voidinitSnake()//初始化蛇身69{

70snake*tail;

71inti;

72tail=(snake*)malloc(sizeof(snake));//从蛇尾开始,头插法,以x,y设定开始的位置//

73tail-x=24;

74tail-y=5;

75tail-next=NULL;

76for(i=1;i=4;i++)//初始长度为477{

78head=(snake*)malloc(sizeof(snake));

79head-next=tail;

80head-x=24+2*i;

81head-y=5;

82tail=head;

83}

84while(tail!=NULL)//从头到为,输出蛇身85{

86Pos(tail-x,tail-

87printf("■");

88tail=tail-next;

89}

90}

91//??92intbiteSelf()//判断是否咬到了自己93{

94snake*self;

95self=head-next;

96while(self!=NULL)

97{

98if(self-x==head-xself-y==head-y)

99{

100return1;

101}

102self=self-next;

103}

104return0;

105}

106107voidcreateFood()//随机出现食物108{

109snake*food_1;

110srand((unsigned)time(NULL));//为了防止每次产生的随机数相同,种子设置为time111food_1=(snake*)malloc(sizeof(snake));

112while((food_1-x%2)!=0)//保证其为偶数,使得食物能与蛇头对其113{

114food_1-x=rand()%52+2;

115}

116food_1-y=rand()%24+1;

117q=head;

118while(q-next==NULL)

119{

120if(q-x==food_1-xq-y==food_1-y)//判断蛇身是否与食物重合121{

122free(food_1);

123createFood();

124}

125q=q-next;

126}

127Pos(food_1-x,food_1-

128food=food_1;

129printf("■");

130}

131132voidcantCrossWall()//不能穿墙133{

134if(head-x==0||head-x==56||head-y==0||head-y==26)

135{

136endGamestatus=1;

137endGame();

138}

139}

140141voidsnakeMove()//蛇前进,上U,下D,左L,右R142{

143snake*nexthead;

144cantCrossWall();

145146nexthead=(snake*)malloc(sizeof(snake));

147if(status==U)

148{

149nexthead-x=head-

150nexthead-y=head-y-1;

151if(nexthead-x==food-xnexthead-y==food-y)//如果下一个有食物//

152{

153nexthead-next=head;

154head=nexthead;

155q=head;

156while(q!=NULL)

157{

158Pos(q-x,q-

159printf("■");

160q=q-next;

161}

162score=score+add;

163createFood();

164}

165else//如果没有食物//

166{

167nexthead-next=head;

168head=nexthead;

169q=head;

170while(q-next-next!=NULL)

171{

172Pos(q-x,q-

173printf("■");

174q=q-next;

175}

176Pos(q-next-x,q-next-

177printf("");

178free(q-next);

179q-next=NULL;

180}

181}

182if(status==D)

183{

184nexthead-x=head-

185nexthead-y=head-y+1;

186if(nexthead-x==food-xnexthead-y==food-y)//有食物187{

188nexthead-next=head;

189head=nexthead;

190q=head;

191while(q!=NULL)

192{

193Pos(q-x,q-

194printf("■");

195q=q-next;

196}

197score=score+add;

198createFood();

199}

200else//没有食物201{

202nexthead-next=head;

203head=nexthead;

204q=head;

205while(q-next-next!=NULL)

206{

207Pos(q-x,q-

208printf("■");

209q=q-next;

210}

211Pos(q-next-x,q-next-

212printf("");

213free(q-next);

214q-next=NULL;

215}

216}

217if(status==L)

218{

219nexthead-x=head-x-2;

220nexthead-y=head-

221if(nexthead-x==food-xnexthead-y==food-y)//有食物222{

223nexthead-next=head;

224head=nexthead;

225q=head;

226while(q!=NULL)

227{

228Pos(q-x,q-

229printf("■");

230q=q-next;

231}

232score=score+add;

233createFood();

234}

235else//没有食物236{

237nexthead-next=head;

238head=nexthead;

239q=head;

240while(q-next-next!=NULL)

241{

242Pos(q-x,q-

243printf("■");

244q=q-next;

245}

246Pos(q-next-x,q-next-

247printf("");

248free(q-next);

249q-next=NULL;

250}

251}

252if(status==R)

253{

254nexthead-x=head-x+2;

255nexthead-y=head-

256if(nexthead-x==food-xnexthead-y==food-y)//有食物257{

258nexthead-next=head;

259head=nexthead;

260q=head;

261while(q!=NULL)

262{

263Pos(q-x,q-

264printf("■");

265q=q-next;

266}

267score=score+add;

268createFood();

269}

270else//没有食物271{

272nexthead-next=head;

273head=nexthead;

274q=head;

275while(q-next-next!=NULL)

276{

277Pos(q-x,q-

278printf("■");

279q=q-next;

280}

281Pos(q-next-x,q-next-

282printf("");

283free(q-next);

284q-next=NULL;

285}

286}

287if(biteSelf()==1)//判断是否会咬到自己288{

289endGamestatus=2;

290endGame();

291}

292}

293294voidpause()//暂停295{

296while(1)

297{

298Sleep(300);

299if(GetAsyncKeyState(VK_SPACE))

300{

301break;

302}

303304}

305}

306307voidrunGame()//控制游戏308{

309310Pos(64,15);

311printf("不能穿墙,不能咬到自己

");

312Pos(64,16);

313printf("用...分别控制蛇的移动.");

314Pos(64,17);

315printf("F1为加速,F2为减速

");

316Pos(64,18);

317printf("ESC:退出游戏.space:暂停游戏.");

318Pos(64,20);

319printf("C语言研究中心www.clang.cc");

320status=R;

321while(1)

322{

323Pos(64,10);

324printf("得分:%d",score);

325Pos(64,11);

326printf("每个食物得分:%d分",add);

327if(GetAsyncKeyState(VK_UP)status!=D)

328{

329status=U;

330}

331elseif(GetAsyncKeyState(VK_DOWN)status!=U)

332{

333status=D;

334}

335elseif(GetAsyncKeyState(VK_LEFT)status!=R)

336{

337status=L;

338}

339elseif(GetAsyncKeyState(VK_RIGHT)status!=L)

340{

341status=R;

342}

343elseif(GetAsyncKeyState(VK_SPACE))

344{

345pause();

346}

347elseif(GetAsyncKeyState(VK_ESCAPE))

348{

349endGamestatus=3;

350break;

351}

352elseif(GetAsyncKeyState(VK_F1))

353{

354if(sleeptime=50)

355{

356sleeptime=sleeptime-30;

357add=add+2;

358if(sleeptime==320)

359{

360add=2;//防止减到1之后再加回来有错361}

362}

363}

364elseif(GetAsyncKeyState(VK_F2))

365{

366if(sleeptime350)

367{

368sleeptime=sleeptime+30;

369add=add-2;

370if(sleeptime==350)

371{

372add=1;//保证最低分为1373}

374}

375}

376Sleep(sleeptime);

377snakeMove();

378}

379}

380381voidinitGame()//开始界面382{

383Pos(40,12);

384385system("titleC语言研究中心www.clang.cc");

386printf("欢迎来到贪食蛇游戏!");

387Pos(40,25);

388printf("C语言研究中心www.clang.cc.

");

389system("pause");

390system("cls");

391Pos(25,12);

392printf("用...分别控制蛇的移动,F1为加速,2为减速

");

393Pos(25,13);

394printf("加速将能得到更高的分数。

");

395system("pause");

396system("cls");

397}

398399voidendGame()//结束游戏400{

401402system("cls");

403Pos(24,12);

404if(endGamestatus==1)

405{

406printf("对不起,您撞到墙了。游戏结束.");

407}

408elseif(endGamestatus==2)

409{

410printf("对不起,您咬到自己了。游戏结束.");

411}

412elseif(endGamestatus==3)

413{

414printf("您的已经结束了游戏。");

415}

416Pos(24,13);

417printf("您的得分是%d

",score);

418while(getchar()!='y')

419{

420printf("close?[y]");

421}

422exit(0);

423}

424425voidgameStart()//游戏初始化426{

427system("modeconcols=100lines=30");

428initGame();

429creatMap();

430initSnake();

431createFood();

432}

433434intmain()

435{

436gameStart();

437runGame();

438endGame();

439return0;

440}

原博客地址:http://www.cnblogs.com/jacklu/p/5214692.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值