<script src="phaser.js"></script> <script type="text/javascript"> var game = new Phaser.Game(480, 640, Phaser.AUTO, '', { preload: preload, create: create, update: update }); function preload() { game.load.image('bet', 'assets/bet.png'); game.load.image('ball', 'assets/ball.png'); game.load.image('background', 'assets/starfield.jpg'); } function create() { game.add.tileSprite(0, 0, 480, 640, 'background'); } function update () { } </script>
Game
type, setting the resolution of our application, in this case, the width is 480 and the height is 640 pixels. Phaser.AUTO
means that the render type will be selected automatically. If desired, you can specify a Canvas or WebGL. The fourth parameter sets the parent DOM object for the game, we do not specify it.preload()
contains the code for loading resources, create()
- initializing the game, and update()
- commands that are executed when updating the application. On the desktop, update()
is called approximately 60 times per second. This function will contain the main game logic.create()
function, we add a static sprite with the background of our game. The sprite fills the space specified in the first four parameters of tileSprite
.game
variable and before the preload()
function, preload()
declare objects with the player’s and computer’s racket, a ball, and also indicate their speeds: var playerBet; var computerBet; var ball; var computerBetSpeed = 190; var ballSpeed = 300;
createBet(x, y)
: function createBet(x, y) { var bet = game.add.sprite(x, y, 'bet'); bet.anchor.setTo(0.5, 0.5); bet.body.collideWorldBounds = true; bet.body.bounce.setTo(1, 1); bet.body.immovable = true; return bet; }
anchor
field is responsible for the reference point of the coordinates of the sprite, set it in the center of the image of the racket. body
contains elements for working with physics. Here we restrict the movement of the racket to the limits of the playing space, assign the force of a “rebound” and indicate that when confronted with objects, the racket will not fly away. playerBet = createBet(game.world.centerX, 600); computerBet = createBet(game.world.centerX, 20);
createBet()
function in create()
: ball = game.add.sprite(game.world.centerX, game.world.centerY, 'ball'); ball.anchor.setTo(0.5, 0.5); ball.body.collideWorldBounds = true; ball.body.bounce.setTo(1, 1);
var ballReleased = false; function releaseBall() { if (!ballReleased) { ball.body.velocity.x = ballSpeed; ball.body.velocity.y = -ballSpeed; ballReleased = true; } }
game.input.onDown.add(releaseBall, this);
update()
function: function update () { // playerBet.x = game.input.x; var playerBetHalfWidth = playerBet.width / 2; if (playerBet.x < playerBetHalfWidth) { playerBet.x = playerBetHalfWidth; } else if (playerBet.x > game.width - playerBetHalfWidth) { playerBet.x = game.width - playerBetHalfWidth; } // if(computerBet.x - ball.x < -15) { computerBet.body.velocity.x = computerBetSpeed; } else if(computerBet.x - ball.x > 15) { computerBet.body.velocity.x = -computerBetSpeed; } else { computerBet.body.velocity.x = 0; } }
x
coordinates so that she does not try to jump out of the playing field.update()
: // game.physics.collide(ball, playerBet, ballHitsBet, null, this); game.physics.collide(ball, computerBet, ballHitsBet, null, this);
collide
method checks the collision of two objects (the first two parameters) and calls the function specified in the third to perform any actions on the encountered sprites. This function looks like this: function ballHitsBet (_ball, _bet) { var diff = 0; if (_ball.x < _bet.x) { // diff = _bet.x - _ball.x; _ball.body.velocity.x = (-10 * diff); } else if (_ball.x > _bet.x) { // diff = _ball.x -_bet.x; _ball.body.velocity.x = (10 * diff); } else { // , _ball.body.velocity.x = 2 + Math.random() * 8; } }
function checkGoal() { if (ball.y < 15) { setBall(); } else if (ball.y > 625) { setBall(); } } function setBall() { if (ballReleased) { ball.x = game.world.centerX; ball.y = game.world.centerY; ball.body.velocity.x = 0; ball.body.velocity.y = 0; ballReleased = false; } }
checkGoal()
is constantly called, so we copy it to the end of update()
: //, - checkGoal();
<script src="phaser.js"></script> <script type="text/javascript"> var game = new Phaser.Game(480, 640, Phaser.AUTO, '', { preload: preload, create: create, update: update }); var playerBet; var computerBet; var ball; var computerBetSpeed = 190; var ballSpeed = 300; var ballReleased = false; function preload() { game.load.image('bet', 'assets/bet.png'); game.load.image('ball', 'assets/ball.png'); game.load.image('background', 'assets/starfield.jpg'); } function create() { game.add.tileSprite(0, 0, 480, 640, 'background'); playerBet = createBet(game.world.centerX, 600); computerBet = createBet(game.world.centerX, 20); ball = game.add.sprite(game.world.centerX, game.world.centerY, 'ball'); ball.anchor.setTo(0.5, 0.5); ball.body.collideWorldBounds = true; ball.body.bounce.setTo(1, 1); game.input.onDown.add(releaseBall, this); } function createBet(x, y) { var bet = game.add.sprite(x, y, 'bet'); bet.anchor.setTo(0.5, 0.5); bet.body.collideWorldBounds = true; bet.body.bounce.setTo(1, 1); bet.body.immovable = true; return bet; } function update () { // playerBet.x = game.input.x; var playerBetHalfWidth = playerBet.width / 2; if (playerBet.x < playerBetHalfWidth) { playerBet.x = playerBetHalfWidth; } else if (playerBet.x > game.width - playerBetHalfWidth) { playerBet.x = game.width - playerBetHalfWidth; } // if(computerBet.x - ball.x < -15) { computerBet.body.velocity.x = computerBetSpeed; } else if(computerBet.x - ball.x > 15) { computerBet.body.velocity.x = -computerBetSpeed; } else { computerBet.body.velocity.x = 0; } // game.physics.collide(ball, playerBet, ballHitsBet, null, this); game.physics.collide(ball, computerBet, ballHitsBet, null, this); //, - checkGoal(); } function ballHitsBet (_ball, _bet) { var diff = 0; if (_ball.x < _bet.x) { // diff = _bet.x - _ball.x; _ball.body.velocity.x = (-10 * diff); } else if (_ball.x > _bet.x) { // diff = _ball.x -_bet.x; _ball.body.velocity.x = (10 * diff); } else { // , _ball.body.velocity.x = 2 + Math.random() * 8; } } function checkGoal() { if (ball.y < 15) { setBall(); } else if (ball.y > 625) { setBall(); } } function setBall() { if (ballReleased) { ball.x = game.world.centerX; ball.y = game.world.centerY; ball.body.velocity.x = 0; ball.body.velocity.y = 0; ballReleased = false; } } function releaseBall() { if (!ballReleased) { ball.body.velocity.x = ballSpeed; ball.body.velocity.y = -ballSpeed; ballReleased = true; } } </script>
Source: https://habr.com/ru/post/197416/
All Articles