首先,先說拼接式的地圖,在學迴圈時應該都做過一個題目,利用迴圈輸出等腰三角形圖案,搭配上二維陣列就可做出地圖,首先看一下成品圖。
在開始之前我們要先準備圖檔,才能有東西顯示在地圖上,只是練習所以只做了兩個,草地跟沙漠。
草地 沙漠
程式碼:
物件 Name 屬性
Panel1 Panel1
Image1 Image1 高跟寬自己算吧,看你要放多少個地形再乘32。
Unit1.h
//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #includeUnit1.cpp#include #include #include #include #include //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TPanel *Panel1; TImage *Image1; private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); void __fastcall drawbmp(); //drawbmp()函式 }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
//--------------------------------------------------------------------------- #include#pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; int cs=32,row=11,col=15; //設定地圖變數,cs=32 為圖像的大小32x32,row=11 為地圖為11 行,col=15 為15 列 int map[11][15] = { //設定地圖陣列,1 為沙漠地形,0 為草地地形 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,1,0,0,0,1,0,1,1,1,1,1,0,1}, {1,0,1,0,0,0,1,0,0,0,1,0,0,0,1}, {1,0,1,0,0,0,1,0,0,0,1,0,0,0,1}, {1,0,1,1,1,1,1,0,0,0,1,0,0,0,1}, {1,0,1,0,0,0,1,0,0,0,1,0,0,0,1}, {1,0,1,0,0,0,1,0,0,0,1,0,0,0,1}, {1,0,1,0,0,0,1,0,1,1,1,1,1,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} }; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { Panel1->DoubleBuffered=true; //設定雙倍緩衝,不會有閃爍 drawbmp(); //呼叫drawbmp()函式 } //--------------------------------------------------------------------------- //drawbmp()函式 void __fastcall TForm1::drawbmp() { //此段可另寫一個loadbmp()函式 Graphics::TBitmap *map1; //利用Graphics 類別的TBitmap 方法設定map1 指標 map1 = new Graphics::TBitmap(); //產生map1 物件 map1->LoadFromFile("1.bmp"); //將1.bmp 載入到map1 物件的記憶體當中 Graphics::TBitmap *map2; map2 = new Graphics::TBitmap(); map2->LoadFromFile("2.bmp"); for(int i=0;i Canvas->Draw(j*cs,i*cs,map1); //在Image1 的相對位置畫出map1 的圖形 break; } case 1: //當陣列內容為1 時 { Image1->Canvas->Draw(j*cs,i*cs,map2); break; } } } delete map1,map2; //釋放記憶體中map1 及map2 的內容 } //---------------------------------------------------------------------------
3 則留言:
謝謝您的教學!!
受益良多!!
請問 像這一系列 2維地圖的設置 有哪一些文獻或參考書 範例可以參考的嗎??
謝謝
我的MAIL st580898@hotmail.com
張貼留言