Skip to content
The Chat Archive
hunyuan-turbos-20250416· Tencent LMArena Human Preference 140K

Napisz prostą grę, którą mogę włączyć w przeglądarce. Gra to prosty tenis 2D.

  1. User

    Napisz prostą grę, którą mogę włączyć w przeglądarce. Gra to prosty tenis 2D.

  2. Assistant

    Oczywiście! Poniżej znajdziesz prosty kod gry typu tenis 2D, który możesz uruchomić bezpośrednio w przeglądarce. Wykorzystuje on HTML5, CSS i JavaScript — wszystko w jednym pliku.

    🔽 Skopiuj cały kod poniżej do pliku o nazwie np. tenis.html i otwórz go w przeglądarce:

    <!DOCTYPE html>
    <html lang="pl">
    <head>
      <meta charset="UTF-8">
      <title>Prosty Tenis 2D</title>
      <style>
        body {
          margin: 0;
          background-color: #222;
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100vh;
          font-family: Arial, sans-serif;
        }
        canvas {
          background-color: #000;
          border: 2px solid white;
        }
        #score {
          position: absolute;
          top: 20px;
          color: white;
          font-size: 24px;
          text-align: center;
          width: 100%;
        }
      </style>
    </head>
    <body>
    
      <div id="score">Gracz A: 0 | Gracz B: 0</div>
      <canvas id="gameCanvas" width="800" height="400"></canvas>
    
      <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');
        const scoreDisplay = document.getElementById('score');
    
        const paddleWidth = 10;
        const paddleHeight = 80;
        const ballSize = 10;
    
        let playerAScore = 0;
        let playerBScore = 0;
    
        // Paletki
        const paddleA = {
          x: 20,
          y: canvas.height / 2 - paddleHeight / 2,
          width: paddleWidth,
          height: paddleHeight,
          dy: 7
        };
    
        const paddleB = {
          x: canvas.width - 20 - paddleWidth,
          y: canvas.height / 2 - paddleHeight / 2,
          width: paddleWidth,
          height: paddleHeight,
          dy: 7
        };
    
        // Piłka
        const ball = {
          x: canvas.width / 2,
          y: canvas.height / 2,
          size: ballSize,
          speedX: 5,
          speedY: 5
        };
    
        // Sterowanie klawiaturą
        const keys = {};
    
        window.addEventListener('keydown', e => {
          keys[e.key] = true;
        });
    
        window.addEventListener('keyup', e => {
          keys[e.key] = false;
        });
    
        function movePaddles() {
          if (keys['w'] || keys['W']) {
            paddleA.y -= paddleA.dy;
          }
          if (keys['s'] || keys['S']) {
            paddleA.y += paddleA.dy;
          }
          if (keys['ArrowUp']) {
            paddleB.y -= paddleB.dy;
          }
          if (keys['ArrowDown']) {
            paddleB.y += paddleB.dy;
          }
    
          // Ograniczenia ruchu paletek
          paddleA.y = Math.max(0, Math.min(canvas.height - paddleA.height, paddleA.y));
          paddleB.y = Math.max(0, Math.min(canvas.height - paddleB.height, paddleB.y));
        }
    
        function moveBall() {
          ball.x += ball.speedX;
          ball.y += ball.speedY;
    
          // Kolizja z górną i dolną ścianą
          if (ball.y <= 0 || ball.y + ball.size >= canvas.height) {
            ball.speedY *= -1;
          }
    
          // Kolizja z paletkami
          if (
            ball.x <= paddleA.x + paddleA.width &&
            ball.y + ball.size >= paddleA.y &&
            ball.y <= paddleA.y + paddleA.height
          ) {
            ball.speedX *= -1;
            // Trochę losowości
            ball.speedY += (Math.random() - 0.5) * 2;
          }
    
          if (
            ball.x + ball.size >= paddleB.x &&
            ball.y + ball.size >= paddleB.y &&
            ball.y <= paddleB.y + paddleB.height
          ) {
            ball.speedX *= -1;
            ball.speedY += (Math.random() - 0.5) * 2;
          }
    
          // Punktacja
          if (ball.x <= 0) {
            playerBScore++;
            resetBall();
          } else if (ball.x + ball.size >= canvas.width) {
            playerAScore++;
            resetBall();
          }
    
          updateScore();
        }
    
        function resetBall() {
          ball.x = canvas.width / 2;
          ball.y = canvas.height / 2;
          ball.speedX = -ball.speedX;
          ball.speedY = 5 * (Math.random() > 0.5 ? 1 : -1);
        }
    
        function updateScore() {
          scoreDisplay.textContent = `Gracz A: ${playerAScore} | Gracz B: ${playerBScore}`;
        }
    
        function draw() {
          // Czyszczenie ekranu
          ctx.clearRect(0, 0, canvas.width, canvas.height);
    
          // Rysowanie środka
          ctx.beginPath();
          ctx.setLineDash([5, 15]);
          ctx.moveTo(canvas.width / 2, 0);
          ctx.lineTo(canvas.width / 2, canvas.height);
          ctx.strokeStyle = 'white';
          ctx.stroke();
          ctx.setLineDash([]);
    
          // Rysowanie paletek
          ctx.fillStyle = 'white';
          ctx.fillRect(paddleA.x, paddleA.y, paddleA.width, paddleA.height);
          ctx.fillRect(paddleB.x, paddleB.y, paddleB.width, paddleB.height);
    
          // Rysowanie piłki
          ctx.beginPath();
          ctx.arc(ball.x + ball.size/2, ball.y + ball.size/2, ball.size/2, 0, Math.PI * 2);
          ctx.fill();
        }
    
        function gameLoop() {
          movePaddles();
          moveBall();
          draw();
          requestAnimationFrame(gameLoop);
        }
    
        gameLoop();
      </script>
    
    </body>
    </html>
    

    🕹️ Jak grać:

    • Gracz A steruje lewą paletką klawiszami: W (w górę) i S (w dół)
    • Gracz B steruje prawą paletką klawiszami strzałek: ↑ (w górę) i ↓ (w dół)

    🎯 Cel gry: Uderzaj piłkę swoją paletką tak długo, jak to możliwe, aby zdobyć punkt. Piłka przelatująca obok paletki daje punkt przeciwnikowi.

    Jeśli chcesz dodać dźwięki, AI do gry jednoosobowej lub ulepszyć wygląd – chętnie pomogę!

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