2D Playground

class TankGame extends Phaser.Scene { constructor () { super(); this.tank = null; this.cursors = null; this.bullets = null; this.lastFired = 0; } preload () { this.load.image('sky', `https://play.rosebud.ai/assets/sky.png?zxfU`); this.load.image('tank', `https://play.rosebud.ai/assets/logo.png?x1Wx`); this.load.image('bullet', `https://play.rosebud.ai/assets/red.png?0GEl`); } create () { this.add.image(400, 300, 'sky'); this.tank = this.physics.add.image(400, 300, 'tank'); this.tank.setCollideWorldBounds(true); this.cursors = this.input.keyboard.createCursorKeys(); this.bullets = this.physics.add.group({ classType: Phaser.Physics.Arcade.Image, defaultKey: 'bullet', maxSize: 10, runChildUpdate: true }); this.input.keyboard.on('keydown-SPACE', this.fireBullet, this); } update (time, delta) { if (this.cursors.left.isDown) { this.tank.setAngularVelocity(-150); } else if (this.cursors.right.isDown) { this.tank.setAngularVelocity(150); } else { this.tank.setAngularVelocity(0); } if (this.cursors.up.isDown) { this.physics.velocityFromRotation(this.tank.rotation, 200, this.tank.body.velocity); } else { this.tank.setVelocity(0); } } fireBullet (time) { if (time - this.lastFired < 200) return; let bullet = this.bullets.get(this.tank.x, this.tank.y); if (bullet) { bullet.setActive(true); bullet.setVisible(true); bullet.setRotation(this.tank.rotation); this.physics.velocityFromRotation(this.tank.rotation, 400, bullet.body.velocity); this.lastFired = time; } } } const container = document.getElementById('renderDiv'); const config = { type: Phaser.AUTO, parent: 'renderDiv', scale: { mode: Phaser.Scale.FIT, autoCenter: Phaser.Scale.CENTER_BOTH, }, width: 800, height: 600, physics: { default: 'arcade', arcade: { gravity: { y: 0 }, debug: false } }, scene: TankGame }; window.phaserGame = new Phaser.Game(config);

  • Developer:hienoque.vrL6
  • Plays:15
  • Remixes:0
  • Platform:Browser (desktop & mobile)