呓语 | 杨英明的个人博客

专注于c++、Python,欢迎交流

By

第三个小项目 - 走迷宫

同样使用 graphics.h 图形库,相比openGL要低级,适合初学者玩玩的。

加入了背景音乐。

问题:如何将黑色的背景色从开始就改为白色?

 

  1 //走迷宫2
  2 
  3 #include <graphics.h>
  4 #include <conio.h>
  5 #include <stdio.h>
  6 #include <mmsystem.h>        //导入音乐的两个文件
  7 #pragma comment(lib,"Winmm.lib")
  8 
  9 void ShowPicture();
 10 void PlayGame();
 11 
 12 int maze[8][8]={
 13         1,1,1,1,1,1,2,1,
 14         3,0,0,1,0,0,0,1,
 15         1,1,0,0,0,1,1,1,
 16         1,0,0,1,0,0,0,1,
 17         1,1,1,1,0,1,1,1,
 18         1,0,0,0,0,0,0,1,
 19         1,0,1,0,1,0,0,1,
 20         1,1,1,1,1,1,1,1
 21 };
 22 char FileName[8];
 23 IMAGE img;
 24 int dir1=1,index=1;
 25 
 26 void main()
 27 {
 28     initgraph(744,800);
 29     setbkcolor(WHITE);    //设置绘图背景颜色
 30     ShowPicture();
 31     PlayGame();
 32     getch();
 33 }
 34 
 35 void ShowPicture()
 36 {
 37     mciSendString("play 顾峰、斯琴高丽-犯错.mp3",0,0,0);
 38     for(int i=0;i<8;i++){
 39         for(int j=0;j<8;j++){
 40             if(maze[i][j]==1){
 41                 getimage(&img,"wall.bmp");
 42                 putimage(j*93,i*100,&img);
 43             }
 44             if(maze[i][j]==2){
 45                 sprintf(FileName,"%d.%d.bmp",dir1,index);    //格式化打印
 46                 getimage(&img,FileName);
 47                 putimage(j*93,i*100,&img);
 48             }
 49         }
 50     }
 51 }
 52 
 53 void PlayGame()
 54 {
 55     while(1){
 56         for(int i=0;i<8;i++){
 57             for(int j=0;j<8;j++){
 58                 if(maze[i][j]==2){
 59                     switch(getch()){    //捕捉键盘输入
 60                     case 's':
 61                         dir1=1;
 62                         index++;
 63                         if(index==5){
 64                             index=1;
 65                         }
 66                         if(maze[i+1][j]==0){
 67                             maze[i+1][j]=2;
 68                             maze[i][j]=0;
 69                             cleardevice();    //清屏
 70                             ShowPicture();    //显示当前
 71                         }
 72                         if(maze[i+1][j]==3){
 73                             maze[i+1][j]=3;
 74                             maze[i][j]=0;
 75                             cleardevice();    //清屏
 76                             getimage(&img,"end.bmp");
 77                             putimage(0,0,&img);
 78                         }
 79                         break;
 80                     case 'a':
 81                         dir1=2;
 82                         index++;
 83                         if(index==5){
 84                             index=1;
 85                         }
 86                         if(maze[i][j-1]==0){
 87                             maze[i][j-1]=2;
 88                             maze[i][j]=0;
 89                             cleardevice();    //清屏
 90                             ShowPicture();    //显示当前
 91                         }
 92                         if(maze[i][j-1]==3){
 93                             maze[i][j-1]=3;
 94                             maze[i][j]=0;
 95                             cleardevice();    //清屏
 96                             getimage(&img,"end.bmp");
 97                             putimage(0,0,&img);
 98                         }
 99                         break;
100                     case 'd':
101                         dir1=3;
102                         index++;
103                         if(index==5){
104                             index=1;
105                         }
106                         if(maze[i][j+1]==0){
107                             maze[i][j+1]=2;
108                             maze[i][j]=0;
109                             cleardevice();    //清屏
110                             ShowPicture();    //显示当前
111                         }
112                         if(maze[i][j+1]==3){
113                             maze[i][j+1]=3;
114                             maze[i][j]=0;
115                             cleardevice();    //清屏
116                             getimage(&img,"end.bmp");
117                             putimage(0,0,&img);
118                         }
119                         break;
120                     case 'w':
121                         dir1=4;
122                         index++;
123                         if(index==5){
124                             index=1;
125                         }
126                         if(maze[i-1][j]==0){
127                             maze[i-1][j]=2;
128                             maze[i][j]=0;
129                             cleardevice();    //清屏
130                             ShowPicture();    //显示当前
131                         }
132                         if(maze[i-1][j]==3){
133                             maze[i-1][j]=3;
134                             maze[i][j]=0;
135                             cleardevice();    //清屏
136                             getimage(&img,"end.bmp");
137                             putimage(0,0,&img);
138                         }
139                         break;
140                     }
141                 }
142             }
143         }
144     }
145 }

资源:

http://pan.baidu.com/share/link?shareid=662002&uk=1779322541

##原创声明 **转载请注明:[呓语](http://www.yangyingming.com) » [第三个小项目 - 走迷宫](http://www.yangyingming.com/article/343)**