Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const canvas = document.getElementById('gameCanvas');
- const ctx = canvas.getContext('2d');
- const scoreDisplay = document.getElementById('score');
- const timerDisplay = document.getElementById('timer');
- canvas.width = window.innerWidth * 0.8;
- canvas.height = window.innerHeight * 0.8;
- let pills = [];
- let score = 0;
- let timeLeft = 30;
- let gameInterval;
- let timerInterval;
- // Create pill objects
- function createPill() {
- const pill = {
- x: Math.random() * canvas.width,
- y: Math.random() * canvas.height,
- size: 50,
- isSliced: false,
- sliceAnimation: null,
- };
- pills.push(pill);
- }
- // Draw pills
- function drawPills() {
- pills.forEach(pill => {
- if (!pill.isSliced) {
- drawWholePill(pill);
- } else if (pill.sliceAnimation) {
- animateSlicedPill(pill);
- }
- });
- }
- // Draw a whole pill
- function drawWholePill(pill) {
- ctx.beginPath();
- ctx.arc(pill.x, pill.y, pill.size, 0, Math.PI * 2);
- ctx.fillStyle = 'red';
- ctx.fill();
- ctx.closePath();
- }
- // Animate sliced pill
- function animateSlicedPill(pill) {
- const { x, y, size, sliceAnimation } = pill;
- const { leftX, leftY, rightX, rightY } = sliceAnimation;
- // Left half
- ctx.beginPath();
- ctx.arc(leftX, leftY, size, Math.PI / 2, Math.PI * 1.5);
- ctx.lineTo(leftX, leftY - size);
- ctx.fillStyle = 'red';
- ctx.fill();
- ctx.closePath();
- // Right half
- ctx.beginPath();
- ctx.arc(rightX, rightY, size, -Math.PI / 2, Math.PI / 2);
- ctx.lineTo(rightX, rightY + size);
- ctx.fillStyle = 'red';
- ctx.fill();
- ctx.closePath();
- // Update animation
- pill.sliceAnimation.leftY += 2;
- pill.sliceAnimation.rightY += 2;
- pill.sliceAnimation.leftX -= 1;
- pill.sliceAnimation.rightX += 1;
- // Remove pill after animation
- if (pill.sliceAnimation.leftY > canvas.height && pill.sliceAnimation.rightY > canvas.height) {
- pill.sliceAnimation = null;
- }
- }
- // Check for slicing
- canvas.addEventListener('mousemove', (event) => {
- const mouseX = event.clientX - canvas.offsetLeft;
- const mouseY = event.clientY - canvas.offsetTop;
- pills.forEach(pill => {
- if (!pill.isSliced && Math.hypot(pill.x - mouseX, pill.y - mouseY) < pill.size) {
- pill.isSliced = true;
- score += 10;
- scoreDisplay.textContent = `Score: ${score}`;
- pill.sliceAnimation = {
- leftX: pill.x - pill.size / 2,
- leftY: pill.y,
- rightX: pill.x + pill.size / 2,
- rightY: pill.y,
- };
- }
- });
- });
- // Game loop
- function gameLoop() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- drawPills();
- if (Math.random() < 0.05) {
- createPill();
- }
- }
- // Timer
- function updateTimer() {
- timeLeft -= 1;
- timerDisplay.textContent = `Time: ${timeLeft}`;
- if (timeLeft <= 0) {
- clearInterval(gameInterval);
- clearInterval(timerInterval);
- alert(`Game Over! Your score is ${score}`);
- }
- }
- // Start game
- function startGame() {
- gameInterval = setInterval(gameLoop, 100);
- timerInterval = setInterval(updateTimer, 1000);
- }
- startGame();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement