Advertisement
Calistio

game

Nov 11th, 2022 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var config = {
  2.     type: Phaser.AUTO,
  3.     width: 1024,
  4.     height: 768,
  5.     backgroundColor: 0x000000,
  6.     physics: {
  7.         default: 'arcade',
  8.         arcade: {
  9.             gravity: { y: 300 },
  10.             debug: true
  11.         }
  12.     },
  13.     scene: {
  14.         preload: preload,
  15.         create: create,
  16.         update: update
  17.     }
  18. }
  19.  
  20. var layoutSize = { 'width': 1024, 'height': 768 };
  21. var X_POSITION;
  22. var Y_POSITION;
  23.  
  24. var player_control;
  25. var player2_control;
  26.  
  27. var player;
  28. var player2;
  29.  
  30. const moveVelocity = 500;
  31. const jumpVelocity = 1050;
  32. const gravityY = 3000;
  33.  
  34. var platforms_normal;
  35.  
  36. function preload() {
  37.     this.load.image('Img_bg', 'assets/Gamelab_Map.png');
  38.     this.load.spritesheet('charaBase_jump', 'assets/Player_Jump.png', { frameWidth: 48, frameHeight: 48 });
  39.     this.load.spritesheet('charaBase_walk', 'assets/Player_Walk.png', { frameWidth: 48, frameHeight: 48 });
  40.     this.load.spritesheet('switch', 'assets/Switch.png', { frameWidth: 48, frameHeight: 48 });
  41.     this.load.image('nPlatform_1x4', 'assets/Platform_1x4.png');
  42.     this.load.image('nPlatform_1x2', 'assets/Platform_1x2.png');
  43.     this.load.image('cPlatformHorizontal_1x4', 'assets/PlatformC_1x4.png');
  44.     this.load.image('cPlatformHorizontal_1x2', 'assets/PlatformC_1x2.png');
  45.     this.load.image('cPlatformVertical_1x4', 'assets/PlatformCVer_1x4.png');
  46.     this.load.image('cPlatformVertical_1x2', 'assets/PlatformCVer_1x2.png');
  47.     this.load.spritesheet('portal', 'assets/Portal_100x100px-sheet.png', { frameWidth: 100, frameHeight: 100 })
  48. }
  49.  
  50. function create() {
  51.     X_POSITION =
  52.     {
  53.         'LEFT': 0,
  54.         'CENTER': game.canvas.width / 2,
  55.         'RIGHT': game.canvas.width
  56.     };
  57.  
  58.     Y_POSITION =
  59.     {
  60.         'TOP': 0,
  61.         'CENTER': game.canvas.height / 2,
  62.         'BOTTOM': game.canvas.height
  63.     };
  64.  
  65.     this.physics.world.setBounds(X_POSITION.CENTER - (1024 / 2), Y_POSITION.TOP, 1024, 768, true, true, true, false);
  66.  
  67.     /* Background */
  68.     let background = this.add.image(X_POSITION.CENTER, Y_POSITION.CENTER, 'Img_bg');
  69.     background.setScale(4);
  70.     background.setDepth(-1);
  71.  
  72.     /* Player */
  73.     player = this.physics.add.sprite(X_POSITION.CENTER - 48, Y_POSITION.CENTER, 'charaBase_jump');
  74.     player.setTint(0x42f5e9);
  75.     player.setDepth(5);
  76.     player.setData('color', 0x42f5e9);
  77.     player.setData('isJumping', false);
  78.  
  79.     player.body.setCollideWorldBounds(true);
  80.     player.setSize(28, 40);
  81.     player.setOffset(10, 8);
  82.     player.body.gravity.y = gravityY;
  83.  
  84.     player2 = this.physics.add.sprite(X_POSITION.CENTER + 48, Y_POSITION.CENTER, 'charaBase_jump');
  85.     player2.setTint(0xf54242);
  86.     player2.setDepth(5);
  87.     player2.setData('color', 0xf54242);
  88.     player2.setData('isJumping', false);
  89.  
  90.     player2.body.setCollideWorldBounds(true);
  91.     player2.setSize(28, 40);
  92.     player2.setOffset(10, 8);
  93.     player2.body.gravity.y = gravityY;
  94.  
  95.     /* Input */
  96.     player_control = this.input.keyboard.addKeys({
  97.         'up': 'up',
  98.         'down': 'down',
  99.         'left': 'left',
  100.         'right': 'right'
  101.     });
  102.  
  103.     player2_control = this.input.keyboard.addKeys({
  104.         'up': Phaser.Input.Keyboard.KeyCodes.W,
  105.         'down': Phaser.Input.Keyboard.KeyCodes.S,
  106.         'left': Phaser.Input.Keyboard.KeyCodes.A,
  107.         'right': Phaser.Input.Keyboard.KeyCodes.D
  108.     });
  109.  
  110.     /* Platform */
  111.     platforms_normal = this.physics.add.staticGroup();
  112.  
  113.     platforms_normal.create(X_POSITION.CENTER - 192, 650, 'nPlatform_1x4').setData('horizontal', true);
  114.     platforms_normal.create(X_POSITION.CENTER, 650, 'nPlatform_1x4');
  115.     platforms_normal.create(X_POSITION.CENTER + 192, 650, 'nPlatform_1x4').setData('horizontal', true);
  116.  
  117.     this.physics.add.collider(player, platforms_normal);
  118.     this.physics.add.collider(player2, platforms_normal);
  119. }
  120.  
  121. function update() {
  122.     if (player.y > layoutSize.height + 192 || player2.y > layoutSize.height + 192) {
  123.         this.scene.restart();
  124.     }
  125.  
  126.     if (player_control == null || player2_control == null) { return; }
  127.  
  128.     /* Player 1 Control */
  129.     if (player_control.left.isDown) {
  130.         player.body.velocity.x = -moveVelocity;
  131.         player.flipX = true;
  132.     }
  133.     else if (player_control.right.isDown) {
  134.         player.body.velocity.x = moveVelocity;
  135.         player.flipX = false;
  136.     }
  137.     else {
  138.         player.body.velocity.x = 0;
  139.     }
  140.  
  141.     if (player_control.up.isDown && player.body.touching.down && !player.getData('isJumping')) {
  142.         player.setData('isJumping', true);
  143.         player.body.velocity.y = -jumpVelocity;
  144.     }
  145.     else if (player_control.up.isUp && player.getData('isJumping')) {
  146.         player.setData('isJumping', false);
  147.     }
  148.  
  149.     /* Player 2 Control */
  150.     if (player2_control.left.isDown) {
  151.         player2.body.velocity.x = -moveVelocity;
  152.         player2.flipX = true;
  153.     }
  154.     else if (player2_control.right.isDown) {
  155.         player2.body.velocity.x = moveVelocity;
  156.         player2.flipX = false;
  157.     }
  158.     else {
  159.         player2.body.velocity.x = 0;
  160.     }
  161.  
  162.     if (player2_control.up.isDown && player2.body.touching.down && !player2.getData('isJumping')) {
  163.         player2.setData('isJumping', true);
  164.         player2.body.velocity.y = -jumpVelocity;
  165.     }
  166.     else if (player2_control.up.isUp && player2.getData('isJumping')) {
  167.         player2.setData('isJumping', false);
  168.     }
  169. }
  170.  
  171. var game = new Phaser.Game(config);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement