Skip to content

Instantly share code, notes, and snippets.

@darkoverlordofdata
Last active August 29, 2015 14:08
Show Gist options
  • Save darkoverlordofdata/f5b63a196f6a10db4603 to your computer and use it in GitHub Desktop.
Save darkoverlordofdata/f5b63a196f6a10db4603 to your computer and use it in GitHub Desktop.
Dart port of phaser_tutorial_02
import 'package:play_phaser/phaser.dart' as Phaser;
void main() {
new Game();
}
class Game extends Phaser.State {
var platforms;
var player;
var cursors;
var stars;
var score = 0;
var scoreText;
Game() {
game = new Phaser.Game(800, 600, Phaser.AUTO, '', this);
}
preload() {
game.load.image('sky', 'assets/sky.png');
game.load.image('ground', 'assets/platform.png');
game.load.image('star', 'assets/star.png');
game.load.spritesheet('dude', 'assets/dude.png', 32, 48);
}
create() {
// We're going to be using physics, so enable the Arcade Physics system
game.physics.startSystem(Phaser.Physics.ARCADE);
// A simple background for our game
game.add.sprite(0, 0, 'sky');
// The platforms group contains the ground and the 2 ledges we can jump on
platforms = game.add.group();
// We will enable physics for any object that is created in this group
platforms.enableBody = true;
// Here we create the ground.
var ground = platforms.create(0, game.world.height - 64, 'ground');
// Scale it to fit the width of the game (the original sprite is 400x32 in size)
ground.scale.set(2, 2);
// This stops it from falling away when you jump on it
ground.body.immovable = true;
// Now let's create two ledges
var ledge = platforms.create(400, 400, 'ground');
ledge.body.immovable = true;
ledge = platforms.create(-150, 250, 'ground');
ledge.body.immovable = true;
// The player and its settings
player = game.add.sprite(32, game.world.height - 150, 'dude');
// We need to enable physics on the player
game.physics.arcade.enable(player);
// Player physics properties. Give the little guy a slight bounce.
player.body.bounce.y = 0.2;
player.body.gravity.y = 300;
player.body.collideWorldBounds = true;
// Our two animations, walking left and right.
player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);
// Finally some stars to collect
stars = game.add.group();
// We will enable physics for any star that is created in this group
stars.enableBody = true;
// Here we'll create 12 of them evenly spaced apart
for (var i = 0; i < 12; i++)
{
// Create a star inside of the 'stars' group
var star = stars.create(i * 70, 0, 'star');
// Let gravity do its thing
star.body.gravity.y = 300;
// This just gives each star a slightly random bounce value
//star.body.bounce.y = 0.7 + rnd.real() * 0.2;
star.body.bounce.y = 0.7 + rnd.normal() * 0.2;
}
// The score
scoreText = game.add.text(16, 16, 'score: 0', new Phaser.TextStyle(font: 'bold 20pt Arial', fill: '#000' ));
// Our controls.
cursors = game.input.keyboard.createCursorKeys();
}
update() {
// Collide the player and the stars with the platforms
game.physics.arcade.collide(player, platforms);
game.physics.arcade.collide(stars, platforms);
// Checks to see if the player overlaps with any of the stars, if he does call the collectStar function
game.physics.arcade.overlap(player, stars, collectStar, null);
// Reset the players velocity (movement)
player.body.velocity.x = 0;
if (cursors.left.isDown)
{
// Move to the left
player.body.velocity.x = -150;
player.animations.play('left');
}
else if (cursors.right.isDown)
{
// Move to the right
player.body.velocity.x = 150;
player.animations.play('right');
}
else
{
// Stand still
player.animations.stop();
player.frame = 4;
}
// Allow the player to jump if they are touching the ground.
if (cursors.up.isDown && player.body.touching.down)
{
player.body.velocity.y = -350;
}
}
collectStar (player, star) {
// Removes the star from the screen
star.kill();
// Add and update the score
score += 10;
scoreText.text = "Score: $score";
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Orion</title>
<style type="text/css">
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<script type="application/dart" src="phaser_tutorial_02.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
name: phaser_tutorial_02
version: 0.0.1
description: A sample web application
dependencies:
browser: any
play_phaser: any
dev_dependencies:
# unittest: any
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment