C++初學者參考學習,代碼有詳細介紹。一般都能看懂- #include<stdio.h>
- #include<conio.h>// getch頭文件
- #include<graphics.h> //圖形庫頭文件
- #include<mmsystem.h>
- #pragma comment(lib,"winmm.lib")//多媒體庫
- /*
- 音樂播放 mciSendString MP3 wav 格式音樂 需要加這個頭文件及庫
- #include<mmsystem.h>
- #pragma comment(lib,"winmm.lib")//多媒體庫
- */
- /*
- 鼠標操作
- MouseHit() 判斷是否存在鼠標消息
- MOUSEMSG 類型 存放鼠標消息
- GetMouseMsg() 獲取鼠標信息的函數
- */
- //人2 箱子3 目的地4 空地0 墻1
- //人+目的地 2+4 6
- int map[8][8] = {
- 0,0,1,1,1,0,0,0,
- 0,0,1,4,1,0,0,0,
- 0,0,1,0,1,1,1,1,
- 1,1,1,3,0,3,4,1,
- 1,4,0,3,2,1,1,1,
- 1,1,1,1,3,1,0,0,
- 0,0,0,1,4,1,0,0,
- 0,0,0,1,1,1,0,0
- };
- //8*8地圖 總大小640*640
- IMAGE img[6];//六張素材
- void init()//加載素材
- {
- //加載圖片
- loadimage(&img[0], "素材/背景.jpg",640,640);
- loadimage(&img[1], "素材/目的地.jpg",80,80);//箱子推到目的地上
- loadimage(&img[2], "素材/墻.jpg", 80, 80);
- loadimage(&img[3], "素材/人物.jpg", 80, 80);
- loadimage(&img[4], "素材/箱子.jpg", 80, 80);
- loadimage(&img[5], "素材/源氏.jpg", 80, 80);//空的目的地
- }
- void drawMap() //畫地圖
- {
- putimage(0, 0, &img[0]);
- for (int i = 0; i < 8; ++i)
- {
- for (int j = 0; j < 8; ++j)
- {
- switch (map[i][j]) //遍歷數組
- {
- case 0:
- break;
- case 1:
- putimage(j * 80, i * 80, &img[2]);//貼圖 墻
- break;
- case 2:
- putimage(j * 80, i * 80, &img[3]);//貼圖 人物
- break;
- case 3:
- putimage(j * 80, i * 80, &img[4]);//貼圖 箱子
- break;
- case 4:
- putimage(j * 80, i * 80, &img[5]);//貼圖 空目的地
- break;
- case 6:
- putimage(j * 80, i * 80, &img[3]);//貼圖 人物
- break;
- case 7:
- putimage(j * 80, i * 80, &img[1]);//貼圖 目的地
- break;
- }
- }
- }
- }
- void play()//操作部分
- {
- int x, y; //找到人物的位置
- for (int i = 0; i < 8; ++i)
- {
- for (int j = 0; j < 8; ++j)
- {
- if (map[i][j] == 2 || map[i][j] == 6) //循環找到人的位置
- {
- x = i;
- y = j; //保存人的位置
- }
- }
- }
- /*//然后判斷用戶輸入
- getch獲取一個字符來操作*/
- switch (getch())
- {
- case 'W':
- case 'w'://往上
- if (map[x - 1][y] == 0 || map[x - 1][y] == 4)//上方是空地或者是空的目的地
- {
- map[x][y] -= 2; //map[x][y]=map[x][y]-2
- map[x - 1][y] += 2;//人往上走
- }
- else if (map[x - 1][y] == 3 || map[x - 1][y] == 7)//人的上方是箱子或箱子+目的地
- {
- if (map[x - 2][y] == 0 || map[x - 2][y] == 4)//箱子上方是空地 推動
- {
- map[x][y] -= 2; //人往上走
- map[x - 1][y] -= 1;//人來了+2 箱子上去了-3
- map[x - 2][y] += 3;//箱子往上推
- }
- }
- break;
- case 'A':
- case'a': //往左
- if (map[x][y - 1] == 0 || map[x][y - 1] == 4)//
- {
- map[x][y] -= 2; //map[x][y]=map[x][y]-2
- map[x][y - 1] += 2;//
- }
- else if (map[x][y - 1] == 3 || map[x][y - 1] == 7)//
- {
- if (map[x][y - 2] == 0 || map[x][y - 2] == 4)//
- {
- map[x][y] -= 2; //人
- map[x][y - 1] -= 1;//
- map[x][y - 2] += 3;//
- }
- }
- break;
- case 'D':
- case 'd': //往右
- if (map[x][y + 1] == 0 || map[x][y + 1] == 4)//
- {
- map[x][y] -= 2; //map[x][y]=map[x][y]-2
- map[x][y + 1] += 2;//
- }
- else if (map[x][y + 1] == 3 || map[x][y + 1] == 7)//
- {
- if (map[x][y + 2] == 0 || map[x][y + 2] == 4)//
- {
- map[x][y] -= 2; //人
- map[x][y + 1] -= 1;//
- map[x][y + 2] += 3;//
- }
- }
- break;
- case 'S':
- case 's': //往下
- if (map[x + 1][y] == 0 || map[x + 1][y] == 4)//
- {
- map[x][y] -= 2; //map[x][y]=map[x][y]-2
- map[x + 1][y] += 2;//
- }
- else if (map[x + 1][y] == 3 || map[x + 1][y] == 7)//
- {
- if (map[x + 2][y] == 0 || map[x + 2][y] == 4)//
- {
- map[x][y] -= 2; //人
- map[x + 1][y] -= 1;//
- map[x + 2][y] += 3;//
- }
- }
- break;
- }
- }
- void gameOver() //通關提示
- {
- int flag=0;
- for (int i = 0; i < 8; ++i)
- {
- for (int j = 0; j < 8; ++j)
- {
- if (map[i][j] == 3)//找到箱子
- {
- ++flag;
- if (map[i - 1][j] == 1 || map[i + 1][j] == 1) //判斷 輸
- {
- if (map[i][j - 1] == 1 || map[i][j - 1] == 1)
- {
- MessageBox(GetHWnd(), "輸", "sad", MB_OK);
- closegraph();
- exit(0);
- }
- }
- }
- }
- }
- if(flag==0)
- {
- MessageBox(GetHWnd(), "贏", "GOOD", MB_OK);
- //第一個參數:句柄 作用:讓窗口置前,也可以寫NULL
- //第四個是按鈕
- closegraph();//關閉窗口
- exit(0);
- }
- }
- int main()
- {
- #if 0 //音樂播放
- //mciSendString("open 素材/bgm.mp3",0,0,0);//打開音樂
- //mciSendString("play 素材/bgm.mp3",0,0,0);//播放音樂 只播放一次
- //mciSendString("play 素材/bgm.mp3 repeat",0,0,0);//repeat循環播放音樂
- mciSendString("open 素材/bgm.mp3 alias bgm", 0, 0, 0);//打開音樂 alias bgm給這個取個別名bgm
- mciSendString("play bgm",0,0,0);//播放音樂 只播放一次
- /*
- 關閉音樂 stop
- 暫停 pause
- */
- #endif
- initgraph(640, 640);//創建640*640窗口
- init();
- //開始界面++++++++++=======================
- putimage(0, 0, &img[0]);//背景圖
- settextstyle(40, 0, "宋體");
- settextcolor(BLACK);
- //setbkmode(TRANSPARENT); //字體背景透明
- setbkmode(1);//字體背景透明
- rectangle(200, 200, 400, 400);//畫矩形
- outtextxy(200, 200, "開始游戲");
- MOUSEMSG msg ;
- int flag = 1;
- while (flag)
- {
- msg = GetMouseMsg();//獲取鼠標消息
- switch (msg.uMsg)
- {
- case WM_LBUTTONDOWN://左鍵按下
- if (msg.x > 200 && msg.x<400 && msg.y>200 && msg.y < 400)
- {
- //點擊的區域在方框之內,可以退出循環
- flag = 0;
- }
- break;
- default:
- break;
- }
- }
-
-
- drawMap();
- while (1)
- {
- play();
- drawMap();
- gameOver();
- }
- getchar();
- closegraph();//關閉窗口
- return 0;
- }
復制代碼
|