没有合适的资源?快使用搜索试试~ 我知道了~
贪吃蛇C C++程序源代码
5星 · 超过95%的资源 需积分: 49 24 下载量 83 浏览量
2011-12-16
12:37:14
上传
评论 1
收藏 75KB DOC 举报
温馨提示
试读
11页
1. //这个是背景的单元格数据结构 2. const length = 40; 3. const width = 20; 4. struct square{ 5. bool blocked; //是否有障碍物 6. bool food; //是否有食物 7. int x; //单元格在背景中的相对横坐标 8. int y; //单元格在背景中的相对纵坐标 9. }bg[length][width]; //直接创建游戏背景 10. 。。。。。。。。 #include <iostream.h> #include <windows.h> #include <stdlib.h> #include <conio.h> #include <time.h> //使用当前时间做种子; enum dir{up,down,left,right}; //枚举类型enum dir; //围墙;
资源推荐
资源详情
资源评论
1. //这个是背景的单元格数据结构
2. const length = 40;
3. const width = 20;
4. struct square{
5. bool blocked; //是否有障碍物
6. bool food; //是否有食物
7. int x; //单元格在背景中的相对横坐标
8. int y; //单元格在背景中的相对纵坐标
9. }bg[length][width]; //直接创建游戏背景
10.
11.//设置背景
12.void setBG(int length, int width){
13. HANDLE hOut;
14. COORD OutChar;
15. OutChar.X = 10;
16. OutChar.Y = 10;
17. int i = 0;
18. int j = 0;
19. for(i = 0; i < width; i++){
20. for(j = 0; j < length; j++){
21. bg[i][j].x = i;
22. bg[i][j].y = j;
23. bg[i][j].blocked = false;
24. bg[i][j].food = false;
25. OutChar.X = j+10;
26. hOut = GetStdHandle(STD_OUTPUT_HANDLE);
27. SetConsoleCursorPosition(hOut,OutChar);
28. cout << col(BG_WHITE,true) << " ";
29. }
30. cout << endl;
31. OutChar.Y = i+10;
32. SetConsoleCursorPosition(hOut,OutChar);
33. }
34.}
35.//构造障碍物
36.void createBlock(int x, int y, unsigned short color){
37. HANDLE hOut;
38. COORD OutChar;
39. OutChar.X = x;
40. OutChar.Y = y;
41. hOut = GetStdHandle(STD_OUTPUT_HANDLE);
42. SetConsoleCursorPosition(hOut,OutChar); //定位光标输入
43. cout << col(color, true) << " "; //一个颜色为 color 的空白
字符
44.}
45.
46.//生成单个障碍物
47.void createWall(int x,int y){
48. createBlock(x+10,y+10,BG_GREEN);
49. bg[x][y].blocked = true;
50.}
51.
52.//判断所指坐标是否被占用
53.bool checkExisted(int x,int y){
54. if(bg[x][y].blocked == true || bg[x][y].food == true){
55. return false;
56. }
57. return true;
58.}
59.
60.//随机生成障碍物
61.void rand_createWall(void){
62. srand((unsigned)time(NULL));
63. int n = rand() % 70+10;
64. int pos_x = 0;
65. int pos_y = 0;
66. int i = 0;
67. for(i = 0; i < n; i++){
68. pos_x = rand() % length;
69. pos_y = rand() % (width-1);
70. if(checkExisted(pos_x,pos_y) == true){ //防止障碍物重
叠
71. createWall(pos_x,pos_y);
72. }else{
73. n++;
74. }
75. //createWall(pos_x,pos_y);
76. }
77.}
78.//创建食物
79.void createFood(int x,int y){
80. createBlock(x+10,y+10,BG_BLUE);
81. bg[x][y].food = true;
82.}
83.
84.//随机创建食物
85.void rand_createFood(void){
86. srand((unsigned)time(NULL));
87. int n = 1;//rand() % 20;
88. int pos_x = 0;
89. int pos_y = 0;
90. int i = 0;
91. for(i = 0; i < n; i++){
92. pos_x = rand() % length;
93. pos_y = rand() % (width-1);
94. if(checkExisted(pos_x,pos_y) == true){ //防止在障碍物
上生成食物
95. createFood(pos_x,pos_y);
96. }else{
97. n++;
98. }
99. }
100. }
101. //物体信息,这是蛇的单元模型
102. const objLen = 5;
103. struct obj{
104. int x;
105. int y;
106. }snake[objLen];
107.
108. //创建蛇
109. LinList<struct obj> newSnake;
110. void createSnake(void){
111. int i = 0;
112. for(i = 0; i < objLen; i++){
113. snake[i].x = i;
114. snake[i].y = 0;
115. newSnake.Insert(snake[i],i);
116. }
117. }
118.
119. //绘制蛇
120. void drawSnake(int len){
121. int i = 0;
122. struct obj t;
123. for(i = 0; i < len; i++){
124. t = newSnake.GetData(i);
125. createBlock(t.x,t.y,BG_RED);
126. }
127. }
128.
129. //增长蛇的身体
剩余10页未读,继续阅读
资源评论
- woodzcz2013-01-02没看清文件类型,下来是个word文档,没法直接用来测试看看,积分要的有点高
- trueman30002013-01-04代码很经典,不过还没怎么看懂。继续研究中。
- panyanqiao2013-07-25东西不错,不过我是菜鸟,不怎么懂
- cheung1892013-11-25很经典的遊戲,代碼很值得參考
- 避马瘟2013-07-08没法直接用,但是可以看出代码中蛇移动的算法、转向以及吃食的算法。代码的结构也还行。
liyan_827
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- tensorflow-2.7.3-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-2.7.2-cp39-cp39-manylinux2010-x86-64.whl
- Python版本快速排序源代码
- Python 语言版的快速排序算法实现
- 450815388207377安卓_base.apk
- 超微主板 X9DRE-TF+ bios 支持 nvme启动
- 基于Python通过下载气象数据和插值拟合离散数据曲线实现对寒潮过程的能量分析
- 健身房系统的设计与实现论文Java项目
- 使用TCP实现的搜索可用服务器
- 使用贪心算法解决会议时间安排问题的 Java 示例代码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功