Home Web Front-end H5 Tutorial Learn while playing with HTML5 (8)-Brick map lattice characters

Learn while playing with HTML5 (8)-Brick map lattice characters

Mar 29, 2017 pm 03:12 PM

I originally thought that in small games such as Tank Battle and Super Mario, the brick characters in the initial screen were static pictures. Now I know that they are all posted with dynamic textures. I will use the drawing function of HTML5 below. Make a starting screen for a tank battle, and study the dot matrix characters by the way.

1. Dot matrix characters

The texture is actually not much different from the dot matrix characters. The only difference is that the dots are replaced by small pictures. That's it, here is a small program for dot matrix characters. You can input Chinese characters or English letters, and then the program will analyze and generate the dot matrix of the text, and then display it. As for how to analyze and generate a dot matrix, the ideas are as follows:

1. Use the ctx.fillText method to draw the text onto a memory canvas, with the foreground color being black and the background color being white

2. Read each pixel of the canvas and replace it with the corresponding symbol to form a string

There is a question here, how big should the memory canvas be? My solution is to make it as large as possible to ensure that no matter what font is, it will not go out of bounds.

During the process of analyzing the pixels, the width and height of the text can be recorded at the same time. After the analysis is completed, a new canvas will be generated again, this time it can be better equal to the size of the text.

Another problem is that when the text is too small, the font is a bit distorted. This should be a resolution problem. The human eye cannot see the small font clearly, and the program cannot analyze it clearly.

So small-sized fonts need to be specially designed, like the fonts on tank battles.

2. Brick characters

After knowing the principle of dot matrix, it is very simple to implement brick characters. Here is a resource In the picture, the bricks are taken from the inside:

The bricks are very small, in the middle of the picture and on the lower right, the # of each level in the tank battle game we played. ##MAP I didn’t expect it to be generated from such a simple picture.

The following is the dot matrix data of the brick characters. There is only a part here, which just constitutes the BATTLE CITY in the game startup screen and the

in the game end screen. 3. Code
Because time is tight, the code is ugly. The code uses a
jsgame.js. This is my own simple implementation of the HTML5 2D function. The packaging imitates some interface styles of pygame. After encapsulation, it can be seen that the drawing-related code is very simple, and the rest are mainly operational logic codes.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

代码

 

 

 

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

 

--><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

 

<html>

 

    <head>

 

        <title></title>

 

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

 

        <script type="text/javascript" language="javascript" src="jsgame.js"></script>

 

    </head>

 

    <body>

 

    <canvas id="html5_08_1" width="180" height="180" style=" background-color: black">

 

        你的浏览器不支持 Canvas 标签,请使用 Chrome 浏览器 或者 FireFox 浏览器

 

    </canvas>

 

    文本:<input type="text" id="text" value="博客园" />

 

    字号:<input type="text" id="size" value="16"  size="2" />

 

    字体:<select id="fontName">

 

        <option>宋体</option>

 

        <option>楷体_GB2312</option>

 

        <option>隶书</option>

 

        <option>Kristen ITC</option>

 

        <option>Harrington</option>

 

    </select>

 

    <input type="checkbox" id="ckBold" />黑体

 

    <input type="checkbox" id="ckitalic" />斜体

 

    <input type="button" id="btnStart" disabled value="处理" onclick="draw_pixel_text()" />

 

    <br/><textarea wrap="off" rows="20" cols="120" id="txtResult" ></textarea>

 

 

 

 

 

    <p/>

 

 

 

    <img alt="坦克大战的资源图片" src="http://images.cnblogs.com/cnblogs_com/myqiao/sprites.gif"/><br/>

 

    <canvas id="html5_08_2" width="480" height="200" style=" background-color: black">

 

        你的浏览器不支持 Canvas 标签,请使用 Chrome 浏览器 或者 FireFox 浏览器

 

    </canvas><br/>

 

    <input type="button" id="btn_draw"  value="绘制砖块字" onclick="draw_brick_text()" />

 

 

 

 

 

    <script type="text/javascript">

 

        draw_pixel_text();

 

        function draw_pixel_text(){

 

            var display= Display.attach(document.getElementById("html5_08_1"));

 

            document.getElementById("btnStart").setAttribute("disabled","true");

 

 

 

            document.getElementById("txtResult").value="";

 

            display.clear();

 

 

 

            var text=document.getElementById("text").value

 

            var size=document.getElementById("size").value

 

 

 

            var font=new Font();

 

            font.bold=document.getElementById("ckBold").checked;

 

            font.italic=document.getElementById("ckitalic").checked;

 

            font.name=document.getElementById("fontName").value;

 

 

 

            var sur=font.render(text,size);

 

            display.draw(sur,10,10);

 

 

 

            var str=&#39;&#39;;

 

            for(var y=0;y<sur.height;y++){

 

                for(var x=0;x<sur.width;x++){

 

                    if((sur.get_pixel(x,y)[0]<255))

 

                        str=str+&#39;龍&#39;;

 

                    else

 

                        str=str+&#39; &#39;;

 

                }

 

                str=str+&#39;\n&#39;;

 

            }

 

            document.getElementById("txtResult").value=str;

 

            document.getElementById("btnStart").removeAttribute("disabled");

 

        }

 

 

 

        ///======================================================================

 

        ///下面的代码是绘制砖块字

 

 

 

        //截断字符,每 7 个一组

 

        function chunk(str,len){

 

            var count=0;

 

            var list=[];

 

            var temp=[];

 

            var times=0;

 

            for(var i=0;i<str.length;i++){

 

                if(count<len){

 

                    temp.push(str[i])

 

                    count++;

 

                }else{

 

                    count=0;

 

                    list[times]=temp;

 

                    temp=[];

 

                    temp.push(str[i])

 

                    count++;

 

                    times++;

 

                }

 

            }

 

            list[times]=temp;

 

            return list;

 

        }

 

 

 

        //字母和对应的点阵数据

 

        var keys="abcegilmortvy";

 

        var values=["0011100011011011000111100011111111111000111100011",

 

            "1111110110001111000111111110110001111000111111110",

 

            "0011110011001111000001100000110000001100110011110",

 

            "1111110110000011000001111100110000011000001111110",

 

            "0011111011000011000001100111110001101100110011111",

 

            "1111110001100000110000011000001100000110001111110",

 

            "1100000110000011000001100000110000011000001111110",

 

            "1100011111011111111111111111110101111000111100011",

 

            "0111110110001111000111100011110001111000110111110",

 

            "1111110110001111000111100111111110011011101100111",

 

            "1111110001100000110000011000001100000110000011000",

 

            "1100011110001111000111110111011111000111000001000",

 

            "1100110110011011001100111100001100000110000011000"];

 

 

 

 

 

        var game2=new JsGame();

 

 

 

        //载入图片

 

        var img=new Image();

 

        img.src="data:image/gif;base64,......";//省略四个字节

 

        var bricks=[];

 

        img.onload=function(){

 

            //图片载入后,将砖块的部分从中间截取出来,并分为四小部分

 

            var temp= new Surface(img).subsurface(56,64,8,8)

 

            bricks[0]=temp.subsurface(0,0,4,4)

 

            bricks[1]=temp.subsurface(4,0,4,4)

 

            bricks[2]=temp.subsurface(0,4,4,4)

 

            bricks[3]=temp.subsurface(4,4,4,4)

 

        }

 

 

 

        //检测资源是否装载完毕

 

        game2.is_ready(function(){

 

            return img.complete

 

        });

 

 

 

        //开始绘制

 

        function draw_brick_text(){

 

            //如果正在绘制,则停止

 

            game2.stop()

 

            //绑定画布

 

            var display= new Display.attach(document.getElementById("html5_08_2"));

 

            //清空画布

 

            display.clear();

 

 

 

            //要绘制的字符串

 

            var text=&#39;BATTLECITY&#39;.toLowerCase();

 

 

 

            //将每个字符的点阵数据截成 7 段,即每个字符都是 7*7 的点阵,方便绘制

 

            var alph_bits=[];

 

            for(var i=0;i<text.length;i++)

 

                for(var index=0;index<keys.length;index++)

 

                    if(keys[index]==text[i])

 

                        alph_bits.push(chunk(values[index],7));

 

 

 

            var which=0;

 

            var p_row=0;

 

            var p_col=0;

 

            var surface= new Surface(28,28);

 

             

 

            game2.loop(function(){

 

                if(alph_bits[which][p_row][p_col]==1){

 

                    var temp=null;

 

                    if((p_row%2)==0){

 

                        if((p_col%2)==0)

 

                            temp=bricks[0];

 

                        else

 

                            temp=bricks[1];

 

                    }

 

                    else{

 

                        if((p_col%2)==0)

 

                            temp=bricks[2];

 

                        else

 

                            temp=bricks[3];

 

                    }

 

                    surface.draw(temp,p_col*4,p_row*4)

 

                    display.save()

 

                    display.scale(2,2)

 

                    if(which<6)

 

                        display.draw(temp,which*32+p_col*4+20,p_row*4+20);

 

                    else

 

                        display.draw(temp,(which-5)*32+p_col*4+20,p_row*4+56);

 

                    display.restore()

 

                }

 

                p_col++

 

                if((p_col%7)==0){

 

                    p_col=0

 

                    p_row++

 

                    if((p_row%7)==0){

 

                        p_row=0;

 

                        p_col=0;

 

                        which++;

 

                        if(which==text.length) game2.stop()

 

                    }

 

                }

 

            })

 

        }

 

    </script>

 

</body>

 

</html>

Copy after login


The above is the detailed content of Learn while playing with HTML5 (8)-Brick map lattice characters. For more information, please follow other related articles on the PHP Chinese website!

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1676
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

See all articles