vencinachev

Game03

Dec 7th, 2021
903
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2. const Game = new Phaser.Game(1000, 800, Phaser.AUTO, 'game-canvas', { preload, create, update })
  3.  
  4.  
  5. let player;
  6. let speed = 2;
  7. let dir = 'right';
  8.  
  9. let player2;
  10. let speed2 = 3;
  11. let dir2 = 'down';
  12.  
  13. function preload() {
  14.     Game.load.image('bg', 'images/bg.jpg');
  15.     Game.load.spritesheet('playerSH', 'images/playerss.png', 576 / 9, 256/ 4);
  16. }
  17.  
  18. function create() {
  19.     let background = Game.add.sprite(0, 0, 'bg');
  20.     background.width = Game.width;
  21.     background.height = Game.height;
  22.  
  23.     player2 = Game.add.sprite(300, 500, 'playerSH')
  24.     player2.width = 80;
  25.     player2.height = 100;
  26.     player2.animations.add('walk-up', [0, 1, 2, 3, 4, 5, 6, 7, 8], 10, true)
  27.     player2.animations.add('walk-down', [18, 19, 20, 21, 22, 23, 24, 25, 26], 10, true)
  28.     player2.animations.add('walk-left', [9, 10, 11, 12, 13, 14, 15, 16, 17], 10, true)
  29.     player2.animations.add('walk-right', [27, 28, 29, 30, 31, 32, 33, 34, 35], 10, true)
  30.  
  31.  
  32.     player = Game.add.sprite(0, 0, 'playerSH')
  33.     player.width = 80;
  34.     player.height = 100;
  35.     player.animations.add('walk-up', [0, 1, 2, 3, 4, 5, 6, 7, 8], 10, true)
  36.     player.animations.add('walk-down', [18, 19, 20, 21, 22, 23, 24, 25, 26], 10, true)
  37.     player.animations.add('walk-left', [9, 10, 11, 12, 13, 14, 15, 16, 17], 10, true)
  38.     player.animations.add('walk-right', [27, 28, 29, 30, 31, 32, 33, 34, 35], 10, true)
  39. }
  40.  
  41. function update() {
  42.     if (dir == 'right'){
  43.         player.x += speed;
  44.         player.animations.play('walk-right')
  45.         if (player.x > (Game.width - player.width)){
  46.             dir = 'left'
  47.         }
  48.     } else if (dir == 'left'){
  49.         player.x -= speed;
  50.         player.animations.play('walk-left')
  51.         if (player.x < 0){
  52.             dir = 'right'
  53.         }
  54.     }
  55.  
  56.     if (dir2 == 'up'){
  57.         player2.y -= speed2;
  58.         player2.animations.play('walk-up');
  59.         if (player2.y < 0){
  60.             dir2 = 'down';
  61.         }
  62.     } else if (dir2 == 'down'){
  63.         player2.y += speed2;
  64.         player2.animations.play('walk-down');
  65.         if (player2.y > (Game.height - player2.height)){
  66.             dir2 = 'up';
  67.         }
  68.     }
  69. }
RAW Paste Data