GRAVITY

Premier test avec JS et ChatGPT

by Jon_S_Goopple

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Le Jeu du JDP</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: black;
            color: white;
            margin: 0;
            padding: 0;
            height: 100vh;
            box-sizing: border-box;
        }
        #gameCanvas {
            margin-top: 20px;
            border: 2px solid white;
        }
        button {
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <h1>Testing js with ChatGPT4</h1>
    <canvas id="gameCanvas" width="600" height="600"></canvas>
    <button id="invertGravity">Invert Gravity</button>
    <button id="switchGravity">Switch Gravity Direction</button>
    <script src="game.js"></script>
</body>
</html>

JavaScript

const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const invertGravityBtn = document.getElementById('invertGravity');
const switchGravityBtn = document.getElementById('switchGravity');

class ImageObject {
  constructor(x, y, src, weight = 1) {
    this.x = x;
    this.y = y;
    this.vx = 0;
    this.vy = 0;
    this.gravity = 0.5 * weight;
    this.gravityDirection = 'down';
    this.bounce = 0.8;
    this.loaded = false;
    this.image = new Image();
    this.image.src = src;
    this.image.onload = () => {
      this.loaded = true;
    };
  }

  draw() {
    ctx.drawImage(this.image, this.x, this.y);
  }

  update() {
    if (this.gravityDirection === 'down') {
      this.vy += this.gravity;
    } else if (this.gravityDirection === 'up') {
      this.vy -= this.gravity;
    } else if (this.gravityDirection === 'left') {
      this.vx -= this.gravity;
    } else if (this.gravityDirection === 'right') {
      this.vx += this.gravity;
    }

    this.x += this.vx;
    this.y += this.vy;

    // Restreindre les images à l'intérieur du cadre d'exécution
    if (this.x < 0) {
      this.x = 0;
      this.vx = -this.vx * this.bounce;
    } else if (this.x + this.image.width > canvas.width) {
      this.x = canvas.width - this.image.width;
      this.vx = -this.vx * this.bounce;
    }

    if (this.y < 0) {
      this.y = 0;
      this.vy = -this.vy * this.bounce;
    } else if (this.y + this.image.height > canvas.height) {
      this.y = canvas.height - this.image.height;
      this.vy = -this.vy * this.bounce;
    }
  }
}

const imageA = new ImageObject(200, 200, 'https://ease.fr/prg/A.png', 0.5);
const imageB = new ImageObject(500, 300, 'https://ease.fr/prg/B.png', 1);
let gameOver = false;

function drawTitle() {
  ctx.font = '32px sans-serif';
  ctx.fillStyle = 'white';
  ctx.textAlign = 'center';
  ctx.fillText('LE JEU DU JDP', canvas.width / 2, 40);
}

function animate() {
  if (!gameOver) {
    ctx.clearRect(0, 0,...