Skip to content
The Chat Archive
llama-4-maverick-17b-128e-instruct· Meta LMArena Human Preference 140K

create the game pong using Phaser 3.88 game framework

  1. User

    create the game pong using Phaser 3.88 game framework

  2. Assistant

    Pong Game using Phaser 3.88

    Game Description

    Pong is a classic tennis-like game where two players hit a ball back and forth on a simple court. The game is won by the player who fails to return the ball to the other player.

    Code

    // Import Phaser
    import Phaser from 'phaser';
    
    // Game configuration
    const config = {
      type: Phaser.AUTO,
      width: 800,
      height: 600,
      scene: {
        preload: preload,
        create: create,
        update: update
      },
      physics: {
        default: 'arcade',
        arcade: {
          gravity: { x: 0, y: 0 }
        }
      }
    };
    
    // Create a new Phaser game
    const game = new Phaser.Game(config);
    
    // Game variables
    let ball;
    let paddle1;
    let paddle2;
    let score1 = 0;
    let score2 = 0;
    let scoreText1;
    let scoreText2;
    
    // Preload assets
    function preload() {
      this.load.setBaseURL('https://labs.phaser.io');
      this.load.setPath('src/games/firstgame/assets/');
      this.load.image('ball', 'sky.png'); // Using sky.png as a placeholder for the ball
      this.load.image('paddle', 'sky.png'); // Using sky.png as a placeholder for the paddles
    }
    
    // Create game objects
    function create() {
      // Create the ball
      ball = this.physics.add.sprite(400, 300, 'ball');
      ball.setCollideWorldBounds(true);
      ball.setBounce(1);
      ball.setVelocity(200, 200);
    
      // Create paddles
      paddle1 = this.physics.add.sprite(50, 300, 'paddle');
      paddle1.setImmovable();
      paddle1.setCollideWorldBounds(true);
    
      paddle2 = this.physics.add.sprite(750, 300, 'paddle');
      paddle2.setImmovable();
      paddle2.setCollideWorldBounds(true);
    
      // Create score text
      scoreText1 = this.add.text(350, 20, '0', { fontSize: '32px', fill: '#fff' });
      scoreText2 = this.add.text(450, 20, '0', { fontSize: '32px', fill: '#fff' });
    
      // Set up collisions
      this.physics.add.collider(ball, paddle1);
      this.physics.add.collider(ball, paddle2);
    
      // Set up keyboard input
      this.cursors = this.input.keyboard.createCursorKeys();
      this.W = this.input.keyboard.addKey(Phaser.Input.Keyboard.W);
      this.S = this.input.keyboard.addKey(Phaser.Input.Keyboard.S);
    }
    
    // Update game state
    function update(time, delta) {
      // Move paddles
      if (this.cursors.up.isDown) {
        paddle2.y -= 5;
      } else if (this.cursors.down.isDown) {
        paddle2.y += 5;
      }
    
      if (this.W.isDown) {
        paddle1.y -= 5;
      } else if (this.S.isDown) {
        paddle1.y += 5;
      }
    
      // Check for ball out of bounds
      if (ball.x < 0) {
        score2++;
        scoreText2.setText(score2.toString());
        ball.x = 400;
        ball.y = 300;
        ball.setVelocity(200, 200);
      } else if (ball.x > 800) {
        score1++;
        scoreText1.setText(score1.toString());
        ball.x = 400;
        ball.y = 300;
        ball.setVelocity(-200, -200);
      }
    }
    

    How to Run

    1. Create a new HTML file (e.g., index.html) and add the following code:
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8" />
      <title>Pong Game</title>
      <script src="https://cdn.jsdelivr.net/npm/phaser@3.88/dist/phaser.min.js"></script>
    </head>
    <body>
      <script src="pong.js"></script>
    </body>
    </html>
    
    1. Create a new JavaScript file (e.g., pong.js) and add the code above.
    2. Open index.html in a web browser to run the game.

    Controls

    • Player 1 (left): W and S keys to move up and down.
    • Player 2 (right): Up and Down arrow keys to move up and down.

    Notes

    • This implementation uses Phaser 3.88 and assumes a basic understanding of the framework.
    • The game uses a simple collision detection system and does not include any sound effects or advanced graphics.
    • You can modify the game configuration, assets, and code to suit your needs.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.