admin管理员组

文章数量:1530856

2024年6月29日发(作者:)

在这篇文章中,让我们来一起用HTML5制作Flappy Bird。

Flappy Bird是一款非常优秀且容易上手的游戏,可以作为一个很好的HTML5游戏的教程。

在这片教程中,我们使用Phaser框架写一个只有65行js代码的简化版Flappy Bird。

提示:你得会JavaScript

设置

先下载我为教程制作的模板,里面包括:

, 简化了的Phaser框架v1.1.5

, 用来展示游戏的文件

, 我们写代码的地方

asset/, 用来保存小鸟和管子的图片的文件夹(和)

用浏览器打开,用文本编辑器打开

在中可以看到我们之前提到的Phaser工程的基本结构

1 //Initialize Phaser, and creates a 400x490px game

2 var game = new (400, 490, ,'game_div');

3

4 //Creates a new'main'state that will contain the game

5 var main_state = {

6

7 preload:function() {

8 //Function called first to load all the assets

9 },

10

11 create:function() {

12 //Fuction called after'preload'to setup the game

13 },

14

15 update:function() {

16 //Function called 60timesper second

17 },

18 };

19

20 //Add and start the'main'state to start the game

21 ('main', main_state);

22 ('main');

接下来我们一次完成preload(),create()和update()方法,并增加一些新的方法。

小鸟的制作

我们先来看如何添加一个用空格键来控制的小鸟。

首先我们来更新preload(),create()和update()方法。

1 preload:function() {

2 //Change the background color of the game

3 oundColor ='#71c5cf';

4

5 //Load the bird sprite

6 ('bird','assets/');

7 },

8

9 create:function() {

10 //Display the bird on thescreen

11 = (100, 245,'bird');

12

13 //Add gravity to the bird tomakeit fall

14 y.y = 1000;

15

16 //Call the'jump'functionwhen the spacekey is hit

17 var space_key = (AR);

18 space_(, this);

19 },

20

21 update:function() {

22 //If the bird is out of the world (too high or too low), call the'restart_game'fun

本文标签: 游戏小鸟用来