Advertisement
Guest User

Phaser3 code

a guest
Dec 29th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.00 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser-arcade-physics.min.js"></script>
  5. </head>
  6. <body>
  7.  
  8.     <script>
  9.     var config = {
  10.         type: Phaser.AUTO,
  11.         width: 1600,
  12.         height: 800,
  13.         physics: {
  14.             default: 'arcade',
  15.             arcade: {
  16.                 gravity: { y: 0 }
  17.             }
  18.         },
  19.         scene: {
  20.             preload: preload,
  21.             create: create,
  22.             update: update
  23.         }
  24.     };
  25.  
  26.     var game = new Phaser.Game(config);
  27.  
  28.     function preload ()
  29.     {
  30.         this.load.image('free_inactive', 'assets/freeField_inactive.jpg');
  31.         this.load.image('occupied_inactive', 'assets/occupiedField_inactive.jpg');
  32.         this.load.image('player', 'assets/player.jpg');
  33.     }
  34.  
  35.  
  36. //Bug happens here
  37.  
  38.     function create ()
  39.     {
  40.  
  41.         //Creation of staticGroups blocks, which will be used for tiles in the game
  42.         blocks = this.physics.add.staticGroup();
  43.        
  44.         //declaring of vars for randomly generating the game-field
  45.  
  46.         //blocktype will be the the name for the asset, depending on wether it is a occupied field or not
  47.         var blocktype;
  48.  
  49.         //gameArr is a two-dimensional array for storing information, which blocks are occupied
  50.         //disclaimer: I tried to determine this by trying to access the image name from the staticGroup children, but I couldn't do it
  51.         //so I created an 2-dimensional array where the indexes indicated the coordinates [x][y]
  52.         //I know, it's pretty "quick-and-dirty" and I'm open for advice for this :)
  53.         var gameArr = new Array(64);
  54.         for(var i = 0; i < 64; i++)
  55.         {
  56.             gameArr[i] = new Array(32);
  57.         }
  58.        
  59.         //Here, the random generation takes place - two "for"-loops for iterating through all coordinates
  60.         for(var i=0; i < 1600; i+=25)
  61.         {
  62.             for(var j=0; j < 800; j+=25)
  63.             {
  64.                 //I decided, that about 1/3 of the blocks should be occupied - based on this rnd, the values are assinged accordingly
  65.                 if(Phaser.Math.Between(1,100) > 33)
  66.                 {
  67.                     //free space - mark in array
  68.                     blocktype="free_inactive";
  69.                     gameArr[(i/25)][(j/25)] = 0;
  70.                 }
  71.                 else
  72.                 {
  73.                     //occupied space - mark in array
  74.                     blocktype="occupied_inactive";
  75.                     gameArr[(i/25)][(j/25)] = 1;
  76.                 }
  77.                
  78.                 //create block according to the values, stored in the variables
  79.                 blocks.create(i+12.5, j+12.5, blocktype).setScale(0.25);
  80.             }
  81.         }
  82.        
  83.         //Display the values of mines on the free spaces
  84.         //Iterates through the 2-dimensional array, where the values were stored, which field is occupied and which field isn't
  85.         for (var i = 0; i < 64; i++)
  86.         {
  87.             for(var j = 0; j < 32; j++)
  88.             {
  89.                 //If the block is not occupied, then add a text at the specified coordinates
  90.                 //The added values to x and y are just to center the font
  91.                 if(gameArr[i][j] == 0)
  92.                 {
  93.                     var x = i * 25 + 8.25;
  94.                     var y = j * 25 + 6.25;
  95.                     this.add.text(x, y, '0', { fontSize: '12px', fill: '#ffffff' });
  96.                 }
  97.             }
  98.         }
  99.    }
  100.        
  101.     function update ()
  102.     {
  103.     }
  104.    </script>
  105.  
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement