Advertisement
formulake

Game

May 24th, 2024 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.26 KB | Source Code | 0 0
  1. const canvas = document.getElementById('gameCanvas');
  2. const ctx = canvas.getContext('2d');
  3. const scoreDisplay = document.getElementById('score');
  4. const timerDisplay = document.getElementById('timer');
  5.  
  6. canvas.width = window.innerWidth * 0.8;
  7. canvas.height = window.innerHeight * 0.8;
  8.  
  9. let pills = [];
  10. let score = 0;
  11. let timeLeft = 30;
  12. let gameInterval;
  13. let timerInterval;
  14.  
  15. // Create pill objects
  16. function createPill() {
  17.     const pill = {
  18.         x: Math.random() * canvas.width,
  19.         y: Math.random() * canvas.height,
  20.         size: 50,
  21.         isSliced: false,
  22.         sliceAnimation: null,
  23.     };
  24.     pills.push(pill);
  25. }
  26.  
  27. // Draw pills
  28. function drawPills() {
  29.     pills.forEach(pill => {
  30.         if (!pill.isSliced) {
  31.             drawWholePill(pill);
  32.         } else if (pill.sliceAnimation) {
  33.             animateSlicedPill(pill);
  34.         }
  35.     });
  36. }
  37.  
  38. // Draw a whole pill
  39. function drawWholePill(pill) {
  40.     ctx.beginPath();
  41.     ctx.arc(pill.x, pill.y, pill.size, 0, Math.PI * 2);
  42.     ctx.fillStyle = 'red';
  43.     ctx.fill();
  44.     ctx.closePath();
  45. }
  46.  
  47. // Animate sliced pill
  48. function animateSlicedPill(pill) {
  49.     const { x, y, size, sliceAnimation } = pill;
  50.     const { leftX, leftY, rightX, rightY } = sliceAnimation;
  51.  
  52.     // Left half
  53.     ctx.beginPath();
  54.     ctx.arc(leftX, leftY, size, Math.PI / 2, Math.PI * 1.5);
  55.     ctx.lineTo(leftX, leftY - size);
  56.     ctx.fillStyle = 'red';
  57.     ctx.fill();
  58.     ctx.closePath();
  59.  
  60.     // Right half
  61.     ctx.beginPath();
  62.     ctx.arc(rightX, rightY, size, -Math.PI / 2, Math.PI / 2);
  63.     ctx.lineTo(rightX, rightY + size);
  64.     ctx.fillStyle = 'red';
  65.     ctx.fill();
  66.     ctx.closePath();
  67.  
  68.     // Update animation
  69.     pill.sliceAnimation.leftY += 2;
  70.     pill.sliceAnimation.rightY += 2;
  71.     pill.sliceAnimation.leftX -= 1;
  72.     pill.sliceAnimation.rightX += 1;
  73.  
  74.     // Remove pill after animation
  75.     if (pill.sliceAnimation.leftY > canvas.height && pill.sliceAnimation.rightY > canvas.height) {
  76.         pill.sliceAnimation = null;
  77.     }
  78. }
  79.  
  80. // Check for slicing
  81. canvas.addEventListener('mousemove', (event) => {
  82.     const mouseX = event.clientX - canvas.offsetLeft;
  83.     const mouseY = event.clientY - canvas.offsetTop;
  84.  
  85.     pills.forEach(pill => {
  86.         if (!pill.isSliced && Math.hypot(pill.x - mouseX, pill.y - mouseY) < pill.size) {
  87.             pill.isSliced = true;
  88.             score += 10;
  89.             scoreDisplay.textContent = `Score: ${score}`;
  90.             pill.sliceAnimation = {
  91.                 leftX: pill.x - pill.size / 2,
  92.                 leftY: pill.y,
  93.                 rightX: pill.x + pill.size / 2,
  94.                 rightY: pill.y,
  95.             };
  96.         }
  97.     });
  98. });
  99.  
  100. // Game loop
  101. function gameLoop() {
  102.     ctx.clearRect(0, 0, canvas.width, canvas.height);
  103.     drawPills();
  104.     if (Math.random() < 0.05) {
  105.         createPill();
  106.     }
  107. }
  108.  
  109. // Timer
  110. function updateTimer() {
  111.     timeLeft -= 1;
  112.     timerDisplay.textContent = `Time: ${timeLeft}`;
  113.     if (timeLeft <= 0) {
  114.         clearInterval(gameInterval);
  115.         clearInterval(timerInterval);
  116.         alert(`Game Over! Your score is ${score}`);
  117.     }
  118. }
  119.  
  120. // Start game
  121. function startGame() {
  122.     gameInterval = setInterval(gameLoop, 100);
  123.     timerInterval = setInterval(updateTimer, 1000);
  124. }
  125.  
  126. startGame();
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement