Skip to content
The Chat Archive
amazon.nova-pro-v1:0 LMArena Human Preference 140K

napisz gre w html do gry w ping pong

  1. User

    napisz gre w html do gry w ping pong

  2. Assistant

    Oczywiście! Poniżej znajduje się prosty przykład gry w ping pong (Pong) zaimplementowanej w HTML, CSS i JavaScript.

    Plik HTML: index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Gra w Ping Pong</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body>
        <canvas id="gameCanvas" width="800" height="400"></canvas>
        <script src="script.js"></script>
    </body>
    </html>
    

    Plik CSS: styles.css

    body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        margin: 0;
        background-color: #000;
    }
    
    canvas {
        border: 2px solid #fff;
        display: block;
    }
    

    Plik JavaScript: script.js

    const canvas = document.getElementById('gameCanvas');
    const ctx = canvas.getContext('2d');
    
    // Ustawienia gry
    const ballRadius = 10;
    let x = canvas.width / 2;
    let y = canvas.height / 2;
    let dx = 2;
    let dy = -2;
    const paddleHeight = 100;
    const paddleWidth = 10;
    let paddle1Y = (canvas.height - paddleHeight) / 2;
    let paddle2Y = (canvas.height - paddleHeight) / 2;
    let rightPressed = false;
    let leftPressed = false;
    
    // Obsługa zdarzeń klawiatury
    document.addEventListener('keydown', keyDownHandler, false);
    document.addEventListener('keyup', keyUpHandler, false);
    
    function keyDownHandler(e) {
        if (e.key === 'ArrowUp' || e.key === 'w') {
            upPressed = true;
        } else if (e.key === 'ArrowDown' || e.key === 's') {
            downPressed = true;
        } else if (e.key === 'ArrowLeft' || e.key === 'a') {
            leftPressed = true;
        } else if (e.key === 'ArrowRight' || e.key === 'd') {
            rightPressed = true;
        }
    }
    
    function keyUpHandler(e) {
        if (e.key === 'ArrowUp' || e.key === 'w') {
            upPressed = false;
        } else if (e.key === 'ArrowDown' || e.key === 's') {
            downPressed = false;
        } else if (e.key === 'ArrowLeft' || e.key === 'a') {
            leftPressed = true;
        } else if (e.key === 'ArrowRight' || e.key === 'd') {
            rightPressed = false;
        }
    }
    
    // Rysowanie piłki
    function drawBall() {
        ctx.beginPath();
        ctx.arc(x, y, ballRadius, 0, Math.PI * 2);
        ctx.fillStyle = "#0095DD";
        ctx.fill();
        ctx.closePath();
    }
    
    // Rysowanie paletek
    function drawPaddle1() {
        ctx.beginPath();
        ctx.rect(0, paddle1Y, paddleWidth, paddleHeight);
        ctx.fillStyle = "#0095DD";
        ctx.fill();
        ctx.closePath();
    }
    
    function drawPaddle2() {
        ctx.beginPath();
        ctx.rect(canvas.width - paddleWidth, paddle2Y, paddleWidth, paddleHeight);
        ctx.fillStyle = "#0095DD";
        ctx.fill();
        ctx.closePath();
    }
    
    // Aktualizacja pozycji
    function update() {
        if (upPressed && paddle1Y > 0) {
            paddle1Y -= 7;
        } else if (downPressed && paddle1Y < canvas.height - paddleHeight) {
            paddle1Y += 7;
        }
    
        if (leftPressed && paddle2Y > 0) {
            paddle2Y -= 7;
        } else if (rightPressed && paddle2Y < canvas.height - paddleHeight) {
            paddle2Y += 7;
        }
    
        x += dx;
        y += dy;
    
        // Kolizja z górną i dolną krawędzią
        if (y + dy > canvas.height - ballRadius || y + dy < ballRadius) {
            dy = -dy;
        }
    
        // Kolizja z paletkami
        if (x + dx > canvas.width - ballRadius - paddleWidth && y > paddle2Y && y < paddle2Y + paddleHeight) {
            dx = -dx;
        } else if (x + dx < ballRadius + paddleWidth && y > paddle1Y && y < paddle1Y + paddleHeight) {
            dx = -dx;
        }
    
        // Punkty
        if (x + dx > canvas.width - ballRadius || x + dx < ballRadius) {
            x = canvas.width / 2;
            y = canvas.height / 2;
            dx = -dx;
        }
    }
    
    // Główna pętla gry
    function draw() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        drawBall();
        drawPaddle1();
        drawPaddle2();
        update();
        requestAnimationFrame(draw);
    }
    
    draw();
    

    Jak uruchomić grę

    1. Utwórz folder i umieść w nim pliki index.html, styles.css i script.js.
    2. Otwórz plik index.html w przeglądarce.

    Gra obsługuje ruch paletek za pomocą klawiszy strzałek lub klawiszy WASD. Lewa paletka porusza się za pomocą klawiszy strzałek lub WASD, a prawa paletka za pomocą klawiszy strzałek lub klawiszy z literami JKL;.

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