Home Web Front-end JS Tutorial Javascript game development: 'Three Kingdoms Cao Cao Biography' component development (4) using map blocks to create a large map_javascript skills

Javascript game development: 'Three Kingdoms Cao Cao Biography' component development (4) using map blocks to create a large map_javascript skills

May 16, 2016 pm 05:42 PM
map

When we were kids, we played jigsaw puzzles and put them together with our own hands. Today we will study how to use javascript to create puzzles. It is also a puzzle, but it is much more troublesome to use js to puzzle than to do it by hand, so I will optimize it into an engine in the future.

1. Foreword

The above is an introduction. I won’t go too far. Players who are familiar with "The Legend of Cao Cao" know that the map of "Three Kingdoms Cao Cao" is made up of small map tiles. To achieve it, it is the same as what the introduction says. : Very troublesome. But even trouble is still a skill, so I share it with you here, I hope you like it.

2. Code explanation

Today I want to change the way of explanation. I won’t give you the code first. Let’s think about the principle first. Now, if you have a picture, cut it into several parts and scramble them. Now if you use js to organize them, how to do it? Let’s not talk about the order of the pictures. First of all, it is difficult to put them together. At this time, I will reduce the difficulty and give you a few options:
A. Use margin to adjust slowly B. Use an array to arrange them C. Give up

In this question, it is unwise to choose A. Choosing C means you are not sure either. It seems that choice B is the best. Since everyone is told to use arrays, let’s start with the code. So as not to dampen everyone's interest.
js code:

Copy code The code is as follows:

/*
*Prompt:
*If you want to add hurdle, find string: "{{Add hurdle above." and "{{After add hurdle, add the hurdle to the vector above." please.
*If you want to add or change type of grid, find string: "{{Add new grid above.".
*If you want to change position of map, please find string: "{{Change map margin above.".
*If the icon of crid is changed, you have to change the size of icon. Find "{{Change icon size above." to change size.
*/

//Map of hurdle or military or resource.
var vView = [];

/*Remarks:
*L: land *S: sea *R: river *W: swamp *A: lawn *B: bridge *H: house *h: hospital *w: warehouse *b: bourse *M: military academy *m: military factories
*r: research Center *P: port *D: dock *s: Shipyard
*/
var mScene = {
'L': ['./land.png', '陆地']
, 'S': ['./sea.png', '河流']
, 'T': ['./tree.png', '树木']
, 'B': ['./bridge.png', '桥']
, 'C': ['./beach.png', '沙滩']
};
//{{Add new grid above.

var mCurrent = {
Margin: {
left: -1
, top: -1
, right: -1
, bottom: -1
}
, Position: {
X: -1
, Y: -1
}
, Type: 'NONE'

};
var mTitle = {};

var sHurdleONE =
'S,S,S,S,S,S,S,S,S,S,S'
';T,L,T,T,T,T,S,S,S,S,T'
';T,L,L,T,S,S,S,S,S,L,T'
';T,L,L,L,C,C,C,S,S,T,S'
';T,L,L,L,C,C,C,B,B,L,T'
';T,L,L,C,C,C,C,S,S,L,T'
';T,L,L,C,C,T,S,S,L,L,T'

//{{Add hurdle above.

var vHurdles = [sHurdleONE];
//{{After add hurdle, add the hurdle to the vector above.

function _createGrid(nWidthBasic, nHeightBasic, nPicWidth, nPicHeight, cType, mMargin)
{
var mCoordMember = {
left: nWidthBasic
, top: nHeightBasic
, right: nWidthBasic nPicWidth
, bottom: nHeightBasic nPicHeight
};
var mPositionMember = {
X: (mCoordMember.left - mMargin.x) / nPicWidth
, Y: (mCoordMember.top - mMargin.y) / nPicHeight
};
var mItem = {
Coord: mCoordMember
, Position: mPositionMember
, Type: cType
};

return mItem;
}

function _loadHurdle(sHurdle)
{
var nBasic = 0;
var nWidthBasic = nBasic; //margin-left.
var nHeightBasic = 0; //margin-top.

//{{Change map margin above.

var nPicWidth = 45; //Picture width is nBasic.
var nPicHeight = 45; //Picturn height is nHeightBasic.
//{{Change icon size above.

var nSub;
var nRow;
var nCol;

var v = sHurdle.split(';');
var vRec = [];

for(nSub = 0; nSub < v.length; nSub ){
var vCrid = v[nSub].split(',');
vRec[vRec.length] = vCrid;
}

for(nRow = 0; nRow < vRec.length; nRow ){
var vCol = vRec[nRow];

for(nCol = 0; nCol < vCol.length; nCol ){
var cType = vCol[nCol];
var mMargin = {x: nBasic, y: nBasic};

vView[vView.length] = _createGrid(nWidthBasic, nHeightBasic, nPicWidth, nPicHeight, cType, mMargin);

nWidthBasic = nPicWidth;
}

nHeightBasic = nPicHeight;
nWidthBasic = nBasic;
}
}



//Show map with vector 'vView'.
function _showMap(sID)
{
var xDiv=document.getElementById(sID);

var xGrid;
var xImg;


var nTop = 0;

var nSub;
var sIdPrefix = 'ID_IMG_NUM_';
var sIdGrid = 'ID_A_NUM_';
for(nSub = 0; nSub < vView.length; nSub ){
var mGrid = vView[nSub];

if(mGrid){
var xMargin = mGrid.Coord;
var cType = mGrid.Type;
var xProper = mScene[cType];

if(xProper){
xGrid = document.createElement('a');
xImg = document.createElement('img');

xImg.style.position = 'absolute';
xImg.style.marginLeft = xMargin.left;
xImg.style.marginTop = xMargin.top;

xImg.src = xProper[0];

xImg.style.border = '0px solid #000000';
xImg.id = sIdPrefix nSub;

xImg.style.width = 45;
xImg.style.height = 45;

xImg.style.display = 'block';

xGrid.onclick = function(e){
var xCurrentGrid = e.target;
var sId = xCurrentGrid.id;
var nIdAsSub = parseInt(sId.substring(sIdPrefix.length, sId.length));

mCurrent = vView[nIdAsSub];
if(!mCurrent){
alert("Error 0004.");
}
};
xGrid.title = xProper[1] '(' parseInt(mGrid.Position.X) ', ' parseInt(mGrid.Position.Y 2) ')';
xGrid.id = sIdGrid nSub;

xGrid.appendChild(xImg);

xDiv.appendChild(xGrid);
}else{
alert("Error: 0003.");
}
}else{
alert("Error: 0002.");
}
}
}

//Show map of hurdle.
function _showHurdle(nHurdle)
{
if(vHurdles[nHurdle - 1]){
_loadHurdle(vHurdles[nHurdle - 1]);
_showMap('ID_DIV_BATTLEFIELD');
}else{
alert("Error: 0001.");
}
}

Look, this program only uses 195 lines, and this is a map. It seems to be a bit troublesome. It's okay, explain slowly.
First of all, let’s put the material here:


The material is not from "The Legend of Cao Cao", because I didn't sort out the map materials of "The Legend of Cao Cao", so I just found some randomly. But it can still be used. I hope you don't mind.

Troublesome code is the easiest to make a mess, so make a good distinction between style settings and puzzle core at this time.
Where is the core of the puzzle? Here:

Copy the code The code is as follows:

var mScene = {
'L ': ['./land.png', 'Land']
, 'S': ['./sea.png', 'River']
, 'T': ['./tree. png', 'trees']
, 'B': ['./bridge.png', 'bridge']
, 'C': ['./beach.png', 'beach']
};
//{{Add new grid above.

var mCurrent = {
Margin: {
left: -1
, top: -1
, right: -1
, bottom: -1
}
, Position: {
X: -1
, Y: -1
}
, Type: ' NONE'

};
var mTitle = {};

var sHurdleONE =
'S,S,S,S,S,S,S,S,S, S,S'
';T,L,T,T,T,T,S,S,S,S,T'
';T,L,L,T,S,S,S, S,S,L,T'
';T,L,L,L,C,C,C,S,S,T,S'
';T,L,L,L,C, C,C,B,B,L,T'
';T,L,L,C,C,C,C,S,S,L,T'
';T,L,L, C,C,T,S,S,L,L,T'

//{{Add hurdle above.

var vHurdles = [sHurdleONE];
//{{ After add hurdle, add the hurdle to the vector above.

First I define S, T, B, C, L so that S represents the river, T represents the tree, B represents the bridge, and stands for beach and L stands for land. var mCurrent will be useful later, so I won’t explain it yet. Then there is var mTitle, which is specifically used to display the title, so I won’t explain it. The key is below:
Copy code The code is as follows:

var sHurdleONE =
'S ,S,S,S,S,S,S,S,S,S,S'
';T,L,T,T,T,T,S,S,S,S,T'
';T,L,L,T,S,S,S,S,S,L,T'
';T,L,L,L,C,C,C,S,S,T, S'
';T,L,L,L,C,C,C,B,B,L,T'
';T,L,L,C,C,C,C,S, S,L,T'
';T,L,L,C,C,T,S,S,L,L,T'


This code is The defined core of S, T, B, C, and L connected together. Later, you only need to define the width and height of S, T, B, C, and L to connect them together. And the style can be changed just by adjusting their position in the array.
Next, in order to switch maps, we put the first map into the array:
Copy the code The code is as follows:

var vHurdles = [sHurdleONE];
//{{After add hurdle, add the hurdle to the vector above.

If a map is added later, Just add the array name to which the map belongs to the vHurdles array, and you can directly write the corresponding subscript when calling it.
The style settings are as follows:
Copy the code The code is as follows:

function _createGrid(nWidthBasic, nHeightBasic, nPicWidth, nPicHeight, cType, mMargin)
{
var mCoordMember = {
left: nWidthBasic
, top: nHeightBasic
, right: nWidthBasic nPicWidth
, bottom: nHeightBasic nPicHeight
};
var mPositionMember = {
X: (mCoordMember.left - mMargin.x) / nPicWidth
, Y: (mCoordMember.top - mMargin.y) / nPicHeight
};
var mItem = {
Coord: mCoordMember
, Position: mPositionMember
, Type: cType
};

return mItem;
}

function _loadHurdle(sHurdle)
{
var nBasic = 0;
var nWidthBasic = nBasic; //margin-left.
var nHeightBasic = 0; //margin-top.

//{{Change map margin above.

var nPicWidth = 45; //Picture width is nBasic.
var nPicHeight = 45; //Picturn height is nHeightBasic.
//{{Change icon size above.

var nSub;
var nRow;
var nCol;

var v = sHurdle.split(';');
var vRec = [];

for(nSub = 0; nSub < v.length; nSub ){
var vCrid = v[nSub].split(',');
vRec[vRec.length] = vCrid;
}

for(nRow = 0; nRow < vRec.length; nRow ){
var vCol = vRec[nRow];

for(nCol = 0; nCol < vCol.length; nCol ){
var cType = vCol[nCol];
var mMargin = {x: nBasic, y: nBasic};

vView[vView.length] = _createGrid(nWidthBasic, nHeightBasic, nPicWidth, nPicHeight, cType, mMargin);

nWidthBasic = nPicWidth;
}

nHeightBasic = nPicHeight;
nWidthBasic = nBasic;
}
}



//Show map with vector 'vView'.
function _showMap(sID)
{
var xDiv=document.getElementById(sID);

var xGrid;
var xImg;


var nTop = 0;

var nSub;
var sIdPrefix = 'ID_IMG_NUM_';
var sIdGrid = 'ID_A_NUM_';
for(nSub = 0; nSub < vView.length; nSub ){
var mGrid = vView[nSub];

if(mGrid){
var xMargin = mGrid.Coord;
var cType = mGrid.Type;
var xProper = mScene[cType];

if(xProper){
xGrid = document.createElement('a');
xImg = document.createElement('img');

xImg.style.position = 'absolute';
xImg.style.marginLeft = xMargin.left;
xImg.style.marginTop = xMargin.top;

xImg.src = xProper[0];

xImg.style.border = '0px solid #000000';
xImg.id = sIdPrefix nSub;

xImg.style.width = 45;
xImg.style.height = 45;

xImg.style.display = 'block';

xGrid.onclick = function(e){
var xCurrentGrid = e.target;
var sId = xCurrentGrid.id;
var nIdAsSub = parseInt(sId.substring(sIdPrefix.length, sId.length));

mCurrent = vView[nIdAsSub];
if(!mCurrent){
alert("Error 0004.");
}
};
xGrid.title = xProper[1] '(' parseInt(mGrid.Position.X) ', ' parseInt(mGrid.Position.Y 2) ')';
xGrid.id = sIdGrid nSub;

xGrid.appendChild(xImg);

xDiv.appendChild(xGrid);
}else{
alert("Error: 0003.");
}
}else{
alert("Error: 0002.");
}
}
}

以上的代码很简单,自己可以看看,提示一下:当你在自己开发的过程中如果弹出一个Error: 0002, Error: 0003, Error: 0001什么之类的,就代表出了错,需要马上去检查。这是为了在麻烦的程序开发中有一点提醒而设计的。值得注意的是:这里的图片全是createElement弄出来的,所以请不要猜疑html代码里有什么蹊跷。
接着看:
复制代码 代码如下:

function _showHurdle(nHurdle)
{
if(vHurdles[nHurdle - 1]){
_loadHurdle(vHurdles[nHurdle - 1]);
_showMap('ID_DIV_BATTLEFIELD');
}else{
alert("Error: 0001.");
}
}

这是在你要弄出地图的调用函数,当你在html代码里写上:几可以把拼的图一下子画出来。nHurdle就是地图在数组vHurdles里的对应下标,最低是1,而不是0,也就是说要用第一张地图,那nHurdle就该赋值为1,调用是写为:。

源代码下载
三、演示效果

演示图在下:


由于是静态的,所以就不给demo了。这种方法虽然很麻烦,而且地图块多了就很慢,但是毕竟是种技术,如果大家有什么好的方法也可以来告诉我。

希望大家多支持。谢谢。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

How to use map and location functions in uniapp How to use map and location functions in uniapp Oct 16, 2023 am 08:01 AM

How to use map and positioning functions in uniapp 1. Background introduction With the popularity of mobile applications and the rapid development of positioning technology, map and positioning functions have become an indispensable part of modern mobile applications. uniapp is a cross-platform application development framework developed based on Vue.js, which can facilitate developers to share code on multiple platforms. This article will introduce how to use maps and positioning functions in uniapp and provide specific code examples. 2. Use the uniapp-amap component to implement the map function

How to add store address to Xiaohongshu map? How to fill in the store address setting? How to add store address to Xiaohongshu map? How to fill in the store address setting? Mar 29, 2024 am 09:41 AM

As Xiaohongshu becomes more and more popular among young people, more and more people choose to open stores on Xiaohongshu. Many novice sellers encounter difficulties when setting up their store address and do not know how to add the store address to the map. 1. How to add the store address to the map in Xiaohongshu? 1. First, make sure your store has a registered account on Xiaohongshu and has successfully opened a store. 2. Log in to your Xiaohongshu account, enter the store backend, and find the "Store Settings" option. 3. On the store settings page, find the "Store Address" column and click "Add Address". 4. In the address adding page that pops up, fill in the detailed address information of the store, including province, city, district, county, street, house number, etc. 5. After filling in, click the "Confirm Add" button. Xiaohongshu will provide you with the address

Master the art of using Apple Maps Guides on iPhone and iPad Master the art of using Apple Maps Guides on iPhone and iPad Aug 30, 2023 am 09:25 AM

In an ever-evolving technological world, the ability to navigate digital maps has become an essential skill. This article provides a comprehensive guide on how to use Apple Maps Guides on iPhone and iPad, a feature that revolutionizes the way users explore their surroundings and plan their journeys. Apple Maps is a built-in application on all Apple devices, and it is constantly updated and improved to provide a seamless navigation experience. One of its most notable features is the Guide feature, which provides a curated list of interesting places to visit in various cities around the world. This feature is not only beneficial for travelers, but also a boon for locals looking to discover new attractions in their city. How to use Apple Maps on iOS guide First, visit the Apple Maps

How to use at-a-glance directions on Google Maps How to use at-a-glance directions on Google Maps Jun 13, 2024 pm 09:40 PM

A year after its launch, Google Maps has launched a new feature. Once you set a route to your destination on the map, it summarizes your travel route. Once your journey begins, you can "Browse" route guidance from your phone's lock screen. You can use Google Maps to see your estimated arrival time and route. Throughout your trip, you can view navigation information on your lock screen, and by unlocking your phone, you can view navigation information without accessing Google Maps. By unlocking your phone, you can view navigation information without accessing Google Maps. By unlocking your phone, you can view navigation information without accessing Google Maps. By unlocking your phone, you can view navigation information without accessing Google Maps. By unlocking your phone, you can view navigation information without accessing Google Maps. By unlocking your phone, you can view navigation information without accessing Google Maps.

Cloud-based and car-based MapNeXt is all done! Construction of next-generation online high-precision maps Cloud-based and car-based MapNeXt is all done! Construction of next-generation online high-precision maps Jan 31, 2024 pm 06:06 PM

Written above & the author’s personal understanding In collaborative, connected and automated mobility (CCAM), the stronger the ability of intelligent driving vehicles to perceive, model and analyze the surrounding environment, the more aware and able they are to understand and make decisions , as well as perform complex driving scenarios safely and efficiently. High-precision (HD) maps represent road environments with centimeter-level accuracy and lane-level semantic information, making them a core component of intelligent mobility systems and a key enabler of CCAM technology. These maps provide automated vehicles with a powerful advantage in understanding their surroundings. HD maps are also considered hidden or virtual sensors because they bring together knowledge from physical sensors (maps), namely lidar, cameras, GPS and IMU, to build a model of the road environment. HD map

How to create a map heat map using Highcharts How to create a map heat map using Highcharts Dec 17, 2023 pm 04:06 PM

How to use Highcharts to create a map heat map requires specific code examples. A heat map is a visual data display method that can represent the data distribution in each area through different color shades. In the field of data visualization, Highcharts is a very popular JavaScript library that provides rich chart types and interactive functions. This article will introduce how to use Highcharts to create a map heat map and provide specific code examples. First, we need to prepare some data

How to create an interactive map using HTML, CSS and jQuery How to create an interactive map using HTML, CSS and jQuery Oct 25, 2023 am 09:40 AM

How to Create an Interactive Map Using HTML, CSS, and jQuery Maps are a common visualization tool that help users understand and navigate geographic locations and related information more easily. By using HTML, CSS and jQuery, we can create an interactive map and add some fun and useful features. This article will guide you on how to use these techniques to create your own interactive map. Create the HTML structure First, we need to create the HTML structure to hold the map. The following is a basic

See all articles