標(biāo)題:
C++初學(xué)推箱子小游戲(代碼有詳細(xì)介紹,通俗易懂)
[打印本頁]
作者:
tanzilong
時(shí)間:
2018-5-23 16:13
標(biāo)題:
C++初學(xué)推箱子小游戲(代碼有詳細(xì)介紹,通俗易懂)
C++初學(xué)者參考學(xué)習(xí),代碼有詳細(xì)介紹。一般都能看懂
#include<stdio.h>
#include<conio.h>// getch頭文件
#include<graphics.h> //圖形庫頭文件
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")//多媒體庫
/*
音樂播放 mciSendString MP3 wav 格式音樂 需要加這個(gè)頭文件及庫
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")//多媒體庫
*/
/*
鼠標(biāo)操作
MouseHit() 判斷是否存在鼠標(biāo)消息
MOUSEMSG 類型 存放鼠標(biāo)消息
GetMouseMsg() 獲取鼠標(biāo)信息的函數(shù)
*/
//人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]) //遍歷數(shù)組
{
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) //循環(huán)找到人的位置
{
x = i;
y = j; //保存人的位置
}
}
}
/*//然后判斷用戶輸入
getch獲取一個(gè)字符來操作*/
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)//箱子上方是空地 推動(dòng)
{
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() //通關(guān)提示
{
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);
//第一個(gè)參數(shù):句柄 作用:讓窗口置前,也可以寫NULL
//第四個(gè)是按鈕
closegraph();//關(guān)閉窗口
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循環(huán)播放音樂
mciSendString("open 素材/bgm.mp3 alias bgm", 0, 0, 0);//打開音樂 alias bgm給這個(gè)取個(gè)別名bgm
mciSendString("play bgm",0,0,0);//播放音樂 只播放一次
/*
關(guān)閉音樂 stop
暫停 pause
*/
#endif
initgraph(640, 640);//創(chuàng)建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();//獲取鼠標(biāo)消息
switch (msg.uMsg)
{
case WM_LBUTTONDOWN://左鍵按下
if (msg.x > 200 && msg.x<400 && msg.y>200 && msg.y < 400)
{
//點(diǎn)擊的區(qū)域在方框之內(nèi),可以退出循環(huán)
flag = 0;
}
break;
default:
break;
}
}
drawMap();
while (1)
{
play();
drawMap();
gameOver();
}
getchar();
closegraph();//關(guān)閉窗口
return 0;
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1