Advertisement
here2share

Physix Pac-Mac gravity-mouse version

Dec 16th, 2022 (edited)
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title>Physix Pac-Mac</title>
  5. </head>
  6. <body>
  7.   <canvas id="PhysixPacMac" width="1200" height="500"></canvas>
  8.   <script>
  9.  
  10.     // Define the Pac-Man character's radius
  11.     const characterRadius = 25;
  12.  
  13.     // Define the dimensions of the game board
  14.     const gameBoardWidth = 1200;
  15.     const gameBoardHeight = 500;
  16.  
  17.     // Define the obstacles in the game
  18.     const obstacles = [
  19.       { x: 100, y: 100 },
  20.       { x: 200, y: 200 },
  21.       { x: 300, y: 300 }
  22.     ];
  23.  
  24.     // Define the power pellets in the game
  25.     const powerPellets = [
  26.       { x: 400, y: 400, points: 50 },
  27.       { x: 500, y: 500, points: 100 }
  28.     ];
  29.  
  30.     // Define the ghosts in the game
  31.     const ghosts = [
  32.       { x: 600, y: 600 },
  33.       { x: 700, y: 700 }
  34.     ];
  35.  
  36.     // Define the dimensions of the character, obstacles, and power pellets
  37.     const characterWidth = characterHeight = 50;
  38.     const obstacleWidth = obstacleHeight = 50;
  39.     const powerPelletWidth = powerPelletHeight = 25;
  40.     const ghostWidth = ghostHeight = 50;
  41.  
  42.     // Initialize the score and lives
  43.     let score = 0;
  44.     let lives = 3;
  45.  
  46.     // Define the starting position of the Pac-Man character
  47.     const startingX = 50;
  48.     const startingY = 50;
  49.  
  50.  
  51.     var canvas = document.getElementById('PhysixPacMac');
  52.     var context = canvas.getContext('2d');
  53.    
  54.     var w = canvas.width = 1200;
  55.     var h = canvas.height = 500;
  56.  
  57.     // Set the fill style to yellow
  58.     context.fillStyle = 'yellow';
  59.  
  60.     // Draw a filled circle at the Pac-Man character's position
  61.     context.beginPath();
  62.     context.arc(x, y, characterRadius, 0, 2 * Math.PI);
  63.     context.fill();
  64.  
  65.     // Initialize the Pac-Man character's position and velocity
  66.     let x = 50;
  67.     let y = 50;
  68.     let vx = 0;
  69.     let vy = 0;
  70.  
  71.     // Set up the game loop
  72.     function gameLoop() {
  73.       // Update the Pac-Man character's velocity based on the mouse position
  74.       vx += (mouseX - x) * 0.1;
  75.       vy += (mouseY - y) * 0.1;
  76.      
  77.       // Update the Pac-Man character's position based on its velocity
  78.       x += vx;
  79.       y += vy;
  80.      
  81.       // Check for collisions with walls
  82.       if (x < 0 || x > gameBoardWidth || y < 0 || y > gameBoardHeight) {
  83.         // Reverse the direction of the velocity to bounce off the wall
  84.         vx = -vx;
  85.         vy = -vy;
  86.       }
  87.      
  88.       // Check for collisions with obstacles
  89.       for (let i = 0; i < obstacles.length; i++) {
  90.         const obstacle = obstacles[i];
  91.         if (x + characterWidth > obstacle.x && x < obstacle.x + obstacleWidth &&
  92.             y + characterHeight > obstacle.y && y < obstacle.y + obstacleHeight) {
  93.           // Reverse the direction of the velocity to bounce off the obstacle
  94.           vx = -vx;
  95.           vy = -vy;
  96.         }
  97.       }
  98.      
  99.       // Check for collisions with power pellets
  100.       for (let i = 0; i < powerPellets.length; i++) {
  101.         const powerPellet = powerPellets[i];
  102.         if (x + characterWidth > powerPellet.x && x < powerPellet.x + powerPelletWidth &&
  103.             y + characterHeight > powerPellet.y && y < powerPellet.y + powerPelletHeight) {
  104.           // Increase the score and remove the power pellet
  105.           score += powerPellet.points;
  106.           powerPellets.splice(i, 1);
  107.         }
  108.       }
  109.      
  110.       // Check for collisions with ghosts
  111.       for (let i = 0; i < ghosts.length; i++) {
  112.         const ghost = ghosts[i];
  113.         if (x + characterWidth > ghost.x && x < ghost.x + ghostWidth &&
  114.             y + characterHeight > ghost.y && y < ghost.y + ghostHeight) {
  115.           // Decrease the lives and reset the Pac-Man character's position
  116.           lives--;
  117.           x = startingX;
  118.           y = startingY;
  119.         }
  120.       }
  121.      
  122.       // Redraw the game board with the Pac-Man character at its new position
  123.       drawGameBoard();
  124.     }
  125.  
  126.     // Set up the mousemove event listener to detect changes in the mouse position
  127.     document.addEventListener('mousemove', (event) => {
  128.       // Update the mouse position
  129.       mouseX = event.clientX;
  130.       mouseY = event.clientY;
  131.     });
  132.  
  133.     // Start the game loop
  134.     setInterval(gameLoop, 1000 / 60); // 60 FPS
  135.   </script>
  136. </body>
  137. </html>
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement