小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C++基于easyx圖形庫實現(xiàn)推箱子游戲

 C語言與CPP編程 2022-10-31 發(fā)布于安徽

學習編程語言時,看視頻看書的時候不是看看就過去了,編程畢竟是門一門手藝活,孰能生巧。

建議一邊看,一邊將視頻中的示例或者小項目教學自己也實現(xiàn)一下,剛開始不會可以照著敲,比只看不動手強一百倍。

精心為學弟學妹整理了60個適合練手的C語言/C++項目,值得收藏!_程序員編程指南的博客-CSDN博客_適合大學生練手的編程項目

神級編程網(wǎng)站,堪稱程序員的充電站,我給你找好了不能錯過_程序員編程指南的博客-CSDN博客_程序員充電網(wǎng)站

這篇文章主要為大家詳細介紹了C++基于easyx圖形庫實現(xiàn)推箱子游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C++實現(xiàn)推箱子游戲的具體代碼,供大家參考,具體內(nèi)容如下:

頭文件:

#include<stdio.h>
#include<stdlib.h>
//#include<Windows.h>
#include<conio.h>
#include<graphics.h>
#include<stdbool.h>
?
//播放音樂需要的頭文件
#include?<mmsystem.h>
#pragma?comment(lib,"winmm.lib")
?
//格子邊長
#define?SPACE?60
?
//6.3.1?定義變量
IMAGE?png[12];//5張圖,定義5個變量,定義一個數(shù)組
?
#define?MapMany?6
?
//枚舉方向
enum?CH?{
?right?=?1,
?left?=?2,
?up?=?3,
?down?=?4
}ch;
?
//定義一個全局三維數(shù)組
?
//0表示空地“?”??1表示墻“■”??2代表目的地“⊙”??3代表人站在空地上“♀”??4代表箱子“□”
//5箱子與目的地重合“◆”??6,人站在目的地上“♀”
?
//下面定義地圖
int?map[MapMany][10][10]?=?{
?//圖一
?{
??{?1,?1,?1,?1,?1,?1,?1,?1,?1,?1?},
??{?1,?2,?0,?1,?1,?0,?0,?0,?2,?1?},
??{?1,?0,?0,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?=?0;//初始化位第0張地圖
?
void?DrawMap(int?n);
void?initscreen();
void?ManUp();
int?KbHit();
void?ManUp();
void?ManRight();
void?ManLeft();
void?ManDown();
int?GameOver();
void?drawGame();

CPP文件:

#include"SokoBan.h"
?
int?main()
{
?initscreen();
?//播放音樂
?mciSendString(TEXT("open?sun.mp3?alias?mysong"),?NULL,?0,?NULL);
?mciSendString(TEXT("play?mySong"),?NULL,?0,?NULL);
?
?while?(1)
?{
??switch?(KbHit())
??{
??case?4:ManUp();?break;
??case?2:ManLeft();?break;
??case?1:ManRight();?break;
??case?3:ManDown();?break;
??//case?0:continue;
??}
??drawGame();
??if?(GameOver())
??{
???WhatMap++;
???Sleep(1000);
??}
??if?(WhatMap?==?3)break;
?}
?loadimage(&png[7],?_T("6.jpg"),?10*SPACE,?10*SPACE,?true);
?putimage(0,?0,?&png[7]);
?Sleep(10000);
?return?0;

?//system("pause");
}
?
//設置窗口大小
void?initscreen()
{
?initgraph(10?*?SPACE,?10?*?SPACE?);
?
?//6.3.2?加載圖片
?
?//0表示空地“?”??1表示墻“■”??2代表目的地“⊙”??3代表人站在空地上“♀”??4代表箱子“□”
?//5箱子與目的地重合“◆”??6,人站在目的地上“♀”
?loadimage(&png[0],?_T("0.jpg"),?SPACE,?SPACE,?true);
?loadimage(&png[1],?_T("1.jpg"),?SPACE,?SPACE,?true);
?loadimage(&png[2],?_T("2.jpg"),?SPACE,?SPACE,?true);
?loadimage(&png[3],?_T("3.jpg"),?SPACE,?SPACE,?true);
?loadimage(&png[4],?_T("4.jpg"),?SPACE,?SPACE,?true);
?loadimage(&png[5],?_T("5.jpg"),?SPACE,?SPACE,?true);
?loadimage(&png[6],?_T("3.jpg"),?SPACE,?SPACE,?true);
?
?
?//以下是windows窗口
?//system("mode?con?cols=20?lines=10");??//cols?代表列?//lines?代表行
?//???????????//設置標題
?//SetConsoleTitleA("推箱子");????//設置標題
}
//貼圖
void?drawGame()?{
?for?(int?i?=?0;?i?<?10;?i++)?{//顯示一行
??for?(int?j?=?0;?j?<?10;?j++)?{//顯示一個格子
???putimage(j*SPACE,?i*SPACE,?&png[map[WhatMap][i][j]]);
??}
?}
}
?
//畫出地圖
?
//0表示空地“?”??1表示墻“■”??2代表目的地“⊙”??3代表人“♀”??4代表箱子“□”
//5箱子與目的地重合“◆”??6,人站在目的地
void?DrawMap(int?n)
{
?system("cls");
?for?(int?j?=?0;?j?<?10;?j++)
?{
??int?i;
??for?(i?=?0;?i?<?10;?i++)
??{
???if?(map[WhatMap][i][j]?==?0)printf("?");
???if?(map[WhatMap][i][j]?==?1)printf("■");
???if?(map[WhatMap][i][j]?==?2)printf("⊙");
???if?(map[WhatMap][i][j]?==?3)printf("♀");
???if?(map[WhatMap][i][j]?==?4)printf("□");
???if?(map[WhatMap][i][j]?==?5)printf("◆");
???if?(map[WhatMap][i][j]?==?6)printf("♀");
??}
??if(i<9)printf("\n");
?}
}
?
//判斷按鍵操作
int?KbHit()
{
?if?(GetAsyncKeyState(VK_UP)&1)return?up;
?else?if?(GetAsyncKeyState(VK_DOWN)&1)return?down;
?else?if?(GetAsyncKeyState(VK_RIGHT)&1)return?right;
?else?if?(GetAsyncKeyState(VK_LEFT)&1)return?left;
?else?return?0;
}
?
//********************************************人向上移動
void?ManUp()
{
?//先找到人的位置
?int?i,?j;
?for?(i?=?0;?i?<?10;?i++)
?{
??int?flag?=?0;
??for?(j?=?0;?j?<?10;?j++)
???if?(map[WhatMap][i][j]?==?3?||?map[WhatMap][i][j]?==?6)
???{
????flag?=?1;
????break;
???}
??if?(flag)break;
?}
?//人的位置是map[WhatMap][i][j];
?
?//改變?nèi)说姆较??
?//人的位置是空地
?if?(map[WhatMap][i][j]?==?3)
?{
??//***********人的上面是空地
??if?(map[WhatMap][i?+?1][j]?==?0)
??{
???map[WhatMap][i][j]?=?0;
???map[WhatMap][i?+?1][j]?=?3;
??}
??//************人的上面是目的地
??else?if?(map[WhatMap][i?+?1][j]?==?2)
??{
???map[WhatMap][i][j]?=?0;
???map[WhatMap][i?+?1][j]?=?6;
??}
??//*************人的上面是箱子
??else?if?(map[WhatMap][i?+?1][j]?==?4)
??{
???//1,***********箱子上面是目的地
???if?(map[WhatMap][i?+?2][j]?==?2)
???{
????map[WhatMap][i][j]?=?0;
????map[WhatMap][i?+?1][j]?=?3;
????map[WhatMap][i?+?2][j]?=?5;
???}
???//2,************箱子上面是空地
???else?if?(map[WhatMap][i?+?2][j]?==?0)
???{
????map[WhatMap][i][j]?=?0;
????map[WhatMap][i?+?1][j]?=?3;
????map[WhatMap][i?+?2][j]?=?4;
???}
???//3,*************箱子上面是。。。。。
??}
?
?}
?//人的位置是目的地
?else?if?(map[WhatMap][i][j]?==?6)
?{
?
??//***********人的上面是空地
?
??if?(map[WhatMap][i?+?1][j]?==?0)
??{
???map[WhatMap][i][j]?=?2;
???map[WhatMap][i?+?1][j]?=?3;
??}
??//************人的上面是目的地
??else?if?(map[WhatMap][i?+?1][j]?==?2)
??{
???map[WhatMap][i][j]?=?2;
???map[WhatMap][i?+?1][j]?=?6;
??}
??//*************人的上面是箱子
?
??else?if?(map[WhatMap][i?+?1][j]?==?4)
??{
???//1,***********箱子上面是目的地
???if?(map[WhatMap][i?+?2][j]?==?2)
???{
????map[WhatMap][i][j]?=?2;
????map[WhatMap][i?+?1][j]?=?3;
????map[WhatMap][i?+?2][j]?=?5;
???}
???//2,************箱子上面是空地
???else?if?(map[WhatMap][i?+?2][j]?==?0)
???{
????map[WhatMap][i][j]?=?2;
????map[WhatMap][i?+?1][j]?=?3;
????map[WhatMap][i?+?2][j]?=?4;
???}
???//3,*************箱子上面是。。。。。
??}
?}
}
//********************************************人向右移動
void?ManRight()
{
?//先找到人的位置
?int?i,?j;
?for?(i?=?0;?i?<?10;?i++)
?{
??int?flag?=?0;
??for?(j?=?0;?j?<?10;?j++)
???if?(map[WhatMap][i][j]?==?3?||?map[WhatMap][i][j]?==?6)
???{
????flag?=?1;
????break;
???}
??if?(flag)break;
?}
?//人的位置是map[WhatMap][i][j];
?
?//改變?nèi)说姆较??
?//人的位置是空地
?if?(map[WhatMap][i][j]?==?3)
?{
??//***********人的右面是空地
??if?(map[WhatMap][i][j+1]?==?0)
??{
???map[WhatMap][i][j]?=?0;
???map[WhatMap][i][j+1]?=?3;
??}
??//************人的右面是目的地
??else?if?(map[WhatMap][i][j+1]?==?2)
??{
???map[WhatMap][i][j]?=?0;
???map[WhatMap][i][j+1]?=?6;
??}
??//*************人的右面是箱子
??else?if?(map[WhatMap][i][j+1]?==?4)
??{
???//1,***********箱子右面是目的地
???if?(map[WhatMap][i][j+2]?==?2)
???{
????map[WhatMap][i][j]?=?0;
????map[WhatMap][i][j+1]?=?3;
????map[WhatMap][i][j+2]?=?5;
???}
???//2,************箱子右面是空地
???else?if?(map[WhatMap][i][j+2]?==?0)
???{
????map[WhatMap][i][j]?=?0;
????map[WhatMap][i][j+1]?=?3;
????map[WhatMap][i][j+2]?=?4;
???}
???//3,*************箱子右面是。。。。。
??}
?}
?//人的位置是目的地
?else?if?(map[WhatMap][i][j]?==?6)
?{
??//***********人的右面是空地
?
??if?(map[WhatMap][i][j+1]?==?0)
??{
???map[WhatMap][i][j]?=?2;
???map[WhatMap][i][j+1]?=?3;
??}
??//************人的右面是目的地
??else?if?(map[WhatMap][i][j+1]?==?2)
??{
???map[WhatMap][i][j]?=?2;
???map[WhatMap][i][j+1]?=?6;
??}
??//*************人的右面是箱子
?
??else?if?(map[WhatMap][i][j+1]?==?4)
??{
???//1,***********箱子右面是目的地
???if?(map[WhatMap][i][j+2]?==?2)
???{
????map[WhatMap][i][j]?=?2;
????map[WhatMap][i][j+1]?=?3;
????map[WhatMap][i][j+2]?=?5;
???}
???//2,************箱子右面是空地
???else?if?(map[WhatMap][i][j+2]?==?0)
???{
????map[WhatMap][i][j]?=?2;
????map[WhatMap][i][j+1]?=?3;
????map[WhatMap][i][j+2]?=?4;
???}
???//3,*************箱子右面是。。。。。
??}
?}
}
//********************************************人向左移動
void?ManLeft()
{
?//先找到人的位置
?int?i,?j;
?for?(i?=?0;?i?<?10;?i++)
?{
??int?flag?=?0;
??for?(j?=?0;?j?<?10;?j++)
???if?(map[WhatMap][i][j]?==?3?||?map[WhatMap][i][j]?==?6)
???{
????flag?=?1;
????break;
???}
??if?(flag)break;
?}
?//人的位置是map[WhatMap][i][j];
?
?//改變?nèi)说姆较??
?//人的位置是空地
?if?(map[WhatMap][i][j]?==?3)
?{
??//***********人的左面是空地
??if?(map[WhatMap][i][j-1]?==?0)
??{
???map[WhatMap][i][j]?=?0;
???map[WhatMap][i][j-1]?=?3;
??}
??//************人的左面是目的地
??else?if?(map[WhatMap][i][j-1]?==?2)
??{
???map[WhatMap][i][j]?=?0;
???map[WhatMap][i][j-1]?=?6;
??}
??//*************人的左面是箱子
??else?if?(map[WhatMap][i][j-1]?==?4)
??{
???//1,***********箱子左面是目的地
???if?(map[WhatMap][i][j-2]?==?2)
???{
????map[WhatMap][i][j]?=?0;
????map[WhatMap][i][j-1]?=?3;
????map[WhatMap][i][j-2]?=?5;
???}
???//2,************箱子左面是空地
???else?if?(map[WhatMap][i][j-2]?==?0)
???{
????map[WhatMap][i][j]?=?0;
????map[WhatMap][i][j-1]?=?3;
????map[WhatMap][i][j-2]?=?4;
???}
???//3,*************箱子左面是。。。。。
??}
?}
?//人的位置是目的地
?else?if?(map[WhatMap][i][j]?==?6)
?{
??//***********人的上面是空地
?
??if?(map[WhatMap][i][j-1]?==?0)
??{
???map[WhatMap][i][j]?=?2;
???map[WhatMap][i][j-1]?=?3;
??}
??//************人的上面是目的地
??else?if?(map[WhatMap][i][j-1]?==?2)
??{
???map[WhatMap][i][j]?=?2;
???map[WhatMap][i][j-1]?=?6;
??}
??//*************人的上面是箱子
?
??else?if?(map[WhatMap][i][j-1]?==?4)
??{
???//1,***********箱子上面是目的地
???if?(map[WhatMap][i][j-2]?==?2)
???{
????map[WhatMap][i][j]?=?2;
????map[WhatMap][i][j-1]?=?3;
????map[WhatMap][i][j-2]?=?5;
???}
???//2,************箱子上面是空地
???else?if?(map[WhatMap][i][j-2]?==?0)
???{
????map[WhatMap][i][j]?=?2;
????map[WhatMap][i][j-1]?=?3;
????map[WhatMap][i][j-2]?=?4;
???}
???//3,*************箱子上面是。。。。。
??}
?}
}
//********************************************人向下移動
void?ManDown()
{
?//先找到人的位置
?int?i,?j;
?for?(i?=?0;?i?<?10;?i++)
?{
??int?flag?=?0;
??for?(j?=?0;?j?<?10;?j++)
???if?(map[WhatMap][i][j]?==?3?||?map[WhatMap][i][j]?==?6)
???{
????flag?=?1;
????break;
???}
??if?(flag)break;
?}
?//人的位置是map[WhatMap][i][j];
?
?//改變?nèi)说姆较??
?//人的位置是空地
?if?(map[WhatMap][i][j]?==?3)
?{
??//***********人的下面是空地
??if?(map[WhatMap][i?-?1][j]?==?0)
??{
???map[WhatMap][i][j]?=?0;
???map[WhatMap][i?-?1][j]?=?3;
??}
??//************人的上面是目的地
??else?if?(map[WhatMap][i?-?1][j]?==?2)
??{
???map[WhatMap][i][j]?=?0;
???map[WhatMap][i?-?1][j]?=?6;
??}
??//*************人的上面是箱子
??else?if?(map[WhatMap][i?-?1][j]?==?4)
??{
???//1,***********箱子上面是目的地
???if?(map[WhatMap][i?-?2][j]?==?2)
???{
????map[WhatMap][i][j]?=?0;
????map[WhatMap][i?-?1][j]?=?3;
????map[WhatMap][i?-?2][j]?=?5;
???}
???//2,************箱子上面是空地
???else?if?(map[WhatMap][i?-?2][j]?==?0)
???{
????map[WhatMap][i][j]?=?0;
????map[WhatMap][i?-?1][j]?=?3;
????map[WhatMap][i?-?2][j]?=?4;
???}
???//3,*************箱子上面是。。。。。
??}
?}
?//人的位置是目的地
?else?if?(map[WhatMap][i][j]?==?6)
?{
??//***********人的上面是空地
?
??if?(map[WhatMap][i?-?1][j]?==?0)
??{
???map[WhatMap][i][j]?=?2;
???map[WhatMap][i?-?1][j]?=?3;
??}
??//************人的上面是目的地
??else?if?(map[WhatMap][i?-?1][j]?==?2)
??{
???map[WhatMap][i][j]?=?2;
???map[WhatMap][i?-?1][j]?=?6;
??}
??//*************人的上面是箱子
?
??else?if?(map[WhatMap][i?-?1][j]?==?4)
??{
???//1,***********箱子上面是目的地
???if?(map[WhatMap][i?-?2][j]?==?2)
???{
????map[WhatMap][i][j]?=?2;
????map[WhatMap][i?-?1][j]?=?3;
????map[WhatMap][i?-?2][j]?=?5;
???}
???//2,************箱子上面是空地
???else?if?(map[WhatMap][i?-?2][j]?==?0)
???{
????map[WhatMap][i][j]?=?2;
????map[WhatMap][i?-?1][j]?=?3;
????map[WhatMap][i?-?2][j]?=?4;
???}
???//3,*************箱子上面是。。。。。
??}
?}
}
?
?
//判斷游戲是否結(jié)束
int?GameOver()
{
?//統(tǒng)計目的地的個數(shù)
?int?flag?=?0;
?for?(int?i?=?0;?i?<?10;?i++)
?{
??for?(int?j?=?0;?j?<?10;?j++)
??{
???if?(map[WhatMap][i][j]?==?2)flag++;
??}
?}
?
?//箱子到達目的地的個數(shù)
?int?count?=?0;
?for?(int?i?=?0;?i?<?10;?i++)
?{
??for?(int?j?=?0;?j?<?10;?j++)
??{
???if?(map[WhatMap][i][j]?==?4)count++;
??}
?}
?if?(count?==?0&&flag?==?0?)return?1;
?else?return?0;
}

運行效果:

游戲資源:

    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多