Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <title>Phaser - Making your first game, part 1</title>
- <script type="text/javascript" src="js/phaser.min.js"></script>
- <style type="text/css">
- body {
- margin: 0;
- }
- </style>
- </head>
- <body>
- <script type="text/javascript">
- var game = new Phaser.Game(320, 480, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
- function preload() {
- game.load.spritesheet('zombie', 'assets/zombies32.png', 29, 35)
- game.load.image('bkg', 'assets/grass.png');
- game.load.image('player', 'assets/diamond.png');
- game.load.image('bullet', 'assets/star.png');
- }
- var seconds = 0;
- var b = 0;
- var can_shoot = true;
- var bullets;
- var zombies;
- var score = 0;
- var txt;
- function create() {
- if (game.device.desktop==false) {
- //scaling start
- game.stage.scale.startFullScreen();
- game.stage.scaleMode = Phaser.StageScaleMode.SHOW_ALL; //resize your window to see the stage resize too
- game.stage.scale.setShowAll();
- game.stage.scale.pageAlignHorizontally = true;
- game.stage.scale.pageAlignVertically = true;
- game.stage.scale.refresh();
- //scaling end
- }
- background = game.add.tileSprite(0, 0, 320, 480, 'bkg');
- player = game.add.sprite(160, 440, 'player');
- txt = game.add.group();
- timer = game.add.text(4, 48, 'Time: 0s', { fontSize: '32px', fill: 'white', stroke: "black", strokeThickness: 5}, txt);
- scoreText = game.add.text(4, 4, 'Score: 0', { fontSize: '32px', fill: 'white', stroke: "black", strokeThickness: 5 }, txt);
- // Enable Input detection. Sprites have this disabled by default,
- // so you have to start it if you want to interact with them.
- player.input.start(0,true);
- // This allows you to drag the sprite. The parameter controls if you drag from the position you touched it (false)
- // or if it will snap to the center (true)
- player.input.enableDrag();
- // This will lock the sprite so it can only be dragged horizontally, not vertically
- player.input.allowVerticalDrag = false;
- player.body.immovable = true;
- player.events.onDragStop.add(dragStop, this);
- player.events.onDragStart.add(dragStart, this);
- zombies = game.add.group();
- bullets = game.add.group();
- }
- function update() {
- game.physics.collide(bullets, zombies, collisionHandler, null, this);
- game.physics.overlap(player, zombies, death_touch, null, this);
- //Calling a different function to update the timer just cleans up the update loop if you have other code.
- updateTimer();
- shoot();
- }
- function render() {
- game.debug.renderSpriteBody(player);
- }
- function shoot(){
- b += 1;
- if(b % 30 == 0 && can_shoot == true){
- var bullet = bullets.create(player.x, player.y, 'bullet');
- player.bringToTop();
- bullet.body.velocity.y = -150;
- bullet.events.onOutOfBounds.add(destroy, this);
- b = 0;
- }
- }
- function updateTimer() {
- seconds += 1;
- timer.content = "Time: " + Math.round(seconds/100) + 's';
- if(seconds/10 % 20 == 0){
- spawn()
- }
- }
- function spawn() {
- for (var i = 0; i < 5; i++)
- {
- // Create a zombie inside of the 'stars' group
- var z = zombies.create(i * 70, i - game.rnd.integerInRange(0, 32), 'zombie');
- var rand = game.rnd.integerInRange(0, 6)
- //console.log("Random: "+rand);
- if (rand == 1){
- z.animations.add('1', [0, 1, 2], 5, true);
- z.animations.play('1')
- }
- else if (rand == 2){
- z.animations.add('2', [3, 4, 5], 5, true);
- z.animations.play('2')
- }
- else if (rand == 3){
- z.animations.add('3', [6, 7, 8], 5, true);
- z.animations.play('3')
- }
- else if (rand == 4){
- z.animations.add('4', [9, 10, 11], 5, true);
- z.animations.play('4')
- }
- z.name = 'zomb' + i;
- z.events.onOutOfBounds.add(destroy, this);
- z.body.velocity.y = 50;
- }
- }
- function dragStart() {
- can_shoot = false;
- }
- function dragStop() {
- can_shoot = true;
- }
- function collisionHandler (bullet, zomb) {
- bullet.kill();
- score+=25;
- scoreText.content = "Score: " + score;
- zomb.kill();
- }
- function death_touch (obj1, obj2) {
- // If the player collides with the chillis then they get eaten :)
- // The chilli frame ID is 17
- console.log('Hit', obj2.name);
- score-=50;
- scoreText.content = "Score: " + score;
- obj2.kill();
- }
- function destroy (obj){
- if (obj.group==zombies){
- score-=10;
- scoreText.content = "Score: " + score;
- }
- obj.kill();
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement