JSDM

HTML

 
1
!

CSS

xxxxxxxxxx
7
 
1
html, body {
2
  height: 100%;
3
}
4
body {
5
  overflow-x: hidden;
6
  background: black;
7
}
!
? ?
? ?
必须是有效的URL
+ 添加另一个资源

JS

xxxxxxxxxx
237
 
1
/* 
2
SANTA ON THE RUN
3
Uses phaser.js http://phaser.io
4
5
*/
6
7
// FULL mode is best for phones http://codepen.io/natewiley/full/gbwWMX
8
9
(function(){
10
11
var width = window.innerWidth;
12
var height = window.innerHeight > 480 ? 480 : window.innerHeight;
13
var gameScore = 0;
14
15
var SantaGame = {
16
17
  init: function(){
18
19
    this.game = new Phaser.Game(width, height, Phaser.CANVAS, '');
20
    this.game.state.add("load", this.load);
21
    this.game.state.add("play", this.play);
22
    this.game.state.add("title", this.title);
23
    this.game.state.add("gameOver", this.gameOver);
24
    this.game.state.add("instructions", this.instructions);
25
    this.game.state.start("load");
26
27
  },
28
29
  load: {
30
    preload: function(){
31
      this.game.load.audio('drivin-home', 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/drivin-home-low.mp3');
32
      this.game.load.audio('ho-ho-ho', 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/ho-ho-ho.mp3');
33
      this.game.load.audio('hop', 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/jump-sound.mp3');
34
      this.game.load.image('platform', 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/ground.png');
35
      this.game.load.spritesheet('santa-running', 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/santa-running.png', 37, 52);
36
      this.game.load.image('snow-bg', 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/snow-bg.png');
37
      this.game.load.image('snowflake', 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/snowflake.png');
38
      this.game.load.image("logo", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/game-logo.png");
39
      this.game.load.image("instructions", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/instructions.png");
40
      this.game.load.image("game-over", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/game-over.png");
41
      this.game.load.image("startbtn", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/start-btn.png");
42
      this.game.load.image("playbtn", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/play-btn.png");
43
      this.game.load.image("restartBtn", "https://s3-us-west-2.amazonaws.com/s.cdpn.io/74196/restart-btn.png");
44
    },
45
    create: function(){
46
      this.game.state.start("title");
47
    }
48
  },
49
50
51
  // title screen
52
53
  title: {
54
    
55
    create: function(){
56
      this.bg = this.game.add.tileSprite(0, 0, width, height, 'snow-bg');
57
      this.logo = this.game.add.sprite(this.game.world.width/2 - 158, 20, 'logo');
58
      this.logo.alpha = 0;
59
      this.game.add.tween(this.logo).to({alpha: 1}, 1000, Phaser.Easing.Linear.None, true, 0);
60
      this.startBtn = this.game.add.button(this.game.world.width/2 - 159, this.game.world.height - 120, 'startbtn', this.startClicked);
61
      this.startBtn.alpha = 0;
62
      this.game.add.tween(this.startBtn).to({alpha: 1}, 1000, Phaser.Easing.Linear.None, true, 1000);
63
    },
64
65
    startClicked: function(){
66
      this.game.state.start("instructions");
67
    },
68
69
  },
70
71
// instructions screen
72
  instructions: {
73
    
74
    create: function(){
75
      this.bg = this.game.add.tileSprite(0, 0, width, height, 'snow-bg');
76
      this.instructions = this.game.add.sprite(this.game.world.width/2 - 292, 30, 'instructions');
77
      this.instructions.alpha = 0;
78
      this.game.add.tween(this.instructions).to({alpha: 1}, 800, Phaser.Easing.Linear.None, true, 0);
79
      this.playBtn = this.game.add.button(this.game.world.width/2 - 159, this.game.world.height - 120, 'playbtn', this.playClicked);
80
      this.playBtn.alpha = 0;
81
      this.game.add.tween(this.playBtn).to({alpha: 1}, 800, Phaser.Easing.Linear.None, true, 800);
82
    },
83
84
    playClicked: function(){
85
      this.game.state.start("play");
86
    },
87
  },
88
89
  // playing
90
  play: {
91
92
      create: function(){
93
        
94
        gameScore = 0;
95
        this.currentFrame = 0;
96
        this.particleInterval = 2 * 60;
97
        this.gameSpeed = 580;
98
        this.isGameOver = false;
99
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
100
101
        this.music = this.game.add.audio("drivin-home");
102
        this.music.loop = true;
103
        this.music.play();
104
105
        this.bg = this.game.add.tileSprite(0, 0, width, height, 'snow-bg');
106
        this.bg.fixedToCamera = true;
107
        this.bg.autoScroll(-this.gameSpeed / 6, 0);
108
109
        this.emitter = this.game.add.emitter(this.game.world.centerX, -32, 50);
110
111
        this.platforms = this.game.add.group();
112
        this.platforms.enableBody = true;
113
        this.platforms.createMultiple(5, 'platform', 0, false);
114
        this.platforms.setAll('anchor.x', 0.5);
115
        this.platforms.setAll('anchor.y', 0.5);
116
117
        var plat;
118
119
        for(var i=0; i<5; i++){
120
          plat = this.platforms.getFirstExists(false);
121
          plat.reset(i * 192, this.game.world.height - 24);
122
          plat.width = 192;
123
          plat.height = 24;
124
          this.game.physics.arcade.enable(plat);
125
          plat.body.immovable = true;
126
          plat.body.bounce.set(0);
127
        }
128
129
        this.lastPlatform = plat;
130
131
        this.santa = this.game.add.sprite(100, this.game.world.height - 200, 'santa-running');
132
        this.santa.animations.add("run");
133
        this.santa.animations.play('run', 20, true);
134
135
        this.game.physics.arcade.enable(this.santa);
136
137
        this.santa.body.gravity.y = 1500;
138
        this.santa.body.collideWorldBounds = true;
139
        
140
        this.emitter.makeParticles('snowflake');
141
        this.emitter.maxParticleScale = .02;
142
        this.emitter.minParticleScale = .001;
143
        this.emitter.setYSpeed(100, 200);
144
        this.emitter.gravity = 0;
145
        this.emitter.width = this.game.world.width * 1.5;
146
        this.emitter.minRotation = 0;
147
        this.emitter.maxRotation = 40;
148
149
        this.game.camera.follow(this.santa);
150
        this.cursors = this.game.input.keyboard.createCursorKeys();
151
        
152
        this.spacebar = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
153
        this.emitter.start(false, 0, 0);
154
        this.score = this.game.add.text(20, 20, '', { font: "24px Arial", fill: "white" });
155
          
156
      },
157
158
      update: function(){
159
        var that = this;
160
        if(!this.isGameOver){
161
          gameScore += .5;
162
            this.gameSpeed += .03;
163
          this.score.text = 'Score: ' + Math.floor(gameScore);
164
          this.currentFrame++;
165
          var moveAmount = this.gameSpeed / 100;
166
          this.game.physics.arcade.collide(this.santa, this.platforms);
167
168
          if(this.santa.body.bottom >= this.game.world.bounds.bottom){
169
            this.isGameOver = true;
170
            this.endGame();
171
            
172
          }
173
174
          if(this.cursors.up.isDown && this.santa.body.touching.down || this.spacebar.isDown && this.santa.body.touching.down || this.game.input.mousePointer.isDown && this.santa.body.touching.down || this.game.input.pointer1.isDown && this.santa.body.touching.down){
175
            this.jumpSound = this.game.add.audio("hop");
176
            this.jumpSound.play();
177
            this.santa.body.velocity.y = -500;
178
          }
179
180
181
          if(this.particleInterval === this.currentFrame){
182
            this.emitter.makeParticles('snowflake');
183
            this.currentFrame = 0;
184
          }
185
186
          this.platforms.children.forEach(function(platform) {
187
            platform.body.position.x -= moveAmount;
188
            if(platform.body.right <= 0){
189
              platform.kill();
190
              var plat = that.platforms.getFirstExists(false);
191
              plat.reset(that.lastPlatform.body.right + 192, that.game.world.height - (Math.floor(Math.random() * 50)) - 24);
192
              plat.body.immovable = true;
193
              that.lastPlatform = plat;
194
            }
195
          });
196
197
        }
198
        
199
      },
200
201
202
      endGame: function(){
203
        this.music.stop();
204
        this.music = this.game.add.audio("ho-ho-ho");
205
        this.music.play();
206
        this.game.state.start("gameOver");
207
      }
208
209
210
  },
211
  gameOver: {
212
213
    create: function(){
214
      this.bg = this.game.add.tileSprite(0, 0, width, height, 'snow-bg');
215
      this.msg = this.game.add.sprite(this.game.world.width/2 - 280.5, 50, 'game-over');
216
      this.msg.alpha = 0;
217
      this.game.add.tween(this.msg).to({alpha: 1}, 600, Phaser.Easing.Linear.None, true, 0);
218
219
      this.score = this.game.add.text(this.game.world.width/2 - 100, 200, 'Score: ' + Math.floor(gameScore), { font: "42px Arial", fill: "white" });
220
      this.score.alpha = 0;
221
      this.game.add.tween(this.score).to({alpha: 1}, 600, Phaser.Easing.Linear.None, true, 600);
222
223
      this.restartBtn = this.game.add.button(this.game.world.width/2 - 183.5, 280, 'restartBtn', this.restartClicked);
224
      this.restartBtn.alpha = 0;
225
      this.game.add.tween(this.restartBtn).to({alpha: 1}, 600, Phaser.Easing.Linear.None, true, 1000);
226
    },
227
228
    restartClicked: function(){
229
      this.game.state.start("play");
230
    },
231
  }
232
233
};
234
235
SantaGame.init();
236
  
237
})();
!
必须是有效的URL
+ 添加另一个资源
Close

文件管理 点击文件查看URL

图片

  1. 暂无文件

CSS

  1. 暂无文件

JavaScript

  1. 暂无文件

其他

  1. 暂无文件
拖动文件到上面的区域或者:
加载中 ..................