Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser-arcade-physics.min.js"></script>
- </head>
- <body>
- <script>
- var config = {
- type: Phaser.AUTO,
- width: 1600,
- height: 800,
- physics: {
- default: 'arcade',
- arcade: {
- gravity: { y: 0 }
- }
- },
- scene: {
- preload: preload,
- create: create,
- update: update
- }
- };
- var game = new Phaser.Game(config);
- function preload ()
- {
- this.load.image('free_inactive', 'assets/freeField_inactive.jpg');
- this.load.image('occupied_inactive', 'assets/occupiedField_inactive.jpg');
- this.load.image('player', 'assets/player.jpg');
- }
- //Bug happens here
- function create ()
- {
- //Creation of staticGroups blocks, which will be used for tiles in the game
- blocks = this.physics.add.staticGroup();
- //declaring of vars for randomly generating the game-field
- //blocktype will be the the name for the asset, depending on wether it is a occupied field or not
- var blocktype;
- //gameArr is a two-dimensional array for storing information, which blocks are occupied
- //disclaimer: I tried to determine this by trying to access the image name from the staticGroup children, but I couldn't do it
- //so I created an 2-dimensional array where the indexes indicated the coordinates [x][y]
- //I know, it's pretty "quick-and-dirty" and I'm open for advice for this :)
- var gameArr = new Array(64);
- for(var i = 0; i < 64; i++)
- {
- gameArr[i] = new Array(32);
- }
- //Here, the random generation takes place - two "for"-loops for iterating through all coordinates
- for(var i=0; i < 1600; i+=25)
- {
- for(var j=0; j < 800; j+=25)
- {
- //I decided, that about 1/3 of the blocks should be occupied - based on this rnd, the values are assinged accordingly
- if(Phaser.Math.Between(1,100) > 33)
- {
- //free space - mark in array
- blocktype="free_inactive";
- gameArr[(i/25)][(j/25)] = 0;
- }
- else
- {
- //occupied space - mark in array
- blocktype="occupied_inactive";
- gameArr[(i/25)][(j/25)] = 1;
- }
- //create block according to the values, stored in the variables
- blocks.create(i+12.5, j+12.5, blocktype).setScale(0.25);
- }
- }
- //Display the values of mines on the free spaces
- //Iterates through the 2-dimensional array, where the values were stored, which field is occupied and which field isn't
- for (var i = 0; i < 64; i++)
- {
- for(var j = 0; j < 32; j++)
- {
- //If the block is not occupied, then add a text at the specified coordinates
- //The added values to x and y are just to center the font
- if(gameArr[i][j] == 0)
- {
- var x = i * 25 + 8.25;
- var y = j * 25 + 6.25;
- this.add.text(x, y, '0', { fontSize: '12px', fill: '#ffffff' });
- }
- }
- }
- }
- function update ()
- {
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement