|
| 1 | +/** |
| 2 | + * @name Circle Clicker |
| 3 | + * @description This example demonstrates a game with a time limit and score. The browser's |
| 4 | + * <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage" target="_blank">local storage</a> |
| 5 | + * stores the high score so when the game is played again using the same browser, |
| 6 | + * the high score remains. Clearing the browser data also clears the high score. |
| 7 | + */ |
| 8 | + |
| 9 | +let circleX; |
| 10 | +let circleY; |
| 11 | +let circleRadius; |
| 12 | +let circleMaximumRadius; |
| 13 | +let circleColor; |
| 14 | +let score = 0; |
| 15 | +let highScore; |
| 16 | + |
| 17 | +function setup() { |
| 18 | + createCanvas(720, 400); |
| 19 | + colorMode(HSB); |
| 20 | + noStroke(); |
| 21 | + ellipseMode(RADIUS); |
| 22 | + textSize(36); |
| 23 | + |
| 24 | + // Get the last saved high score |
| 25 | + highScore = getItem('high score'); |
| 26 | + |
| 27 | + // If not score was saved, start with a value of 0 |
| 28 | + if (highScore === null) { |
| 29 | + highScore = 0; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function draw() { |
| 34 | + background(20); |
| 35 | + |
| 36 | + // If the circle had not shrunk completely |
| 37 | + if (circleRadius > 0) { |
| 38 | + // Draw the circle |
| 39 | + fill(circleColor); |
| 40 | + circle(circleX, circleY, circleRadius); |
| 41 | + describeElement('Circle', 'Randomly colored shrinking circle'); |
| 42 | + |
| 43 | + // Shrink it |
| 44 | + circleRadius -= 1; |
| 45 | + |
| 46 | + // Show the score |
| 47 | + textAlign(RIGHT, TOP); |
| 48 | + fill(220); |
| 49 | + text(score, width - 20, 20); |
| 50 | + describeElement('Score', `Text with current score: ${score}`); |
| 51 | + } else { |
| 52 | + // Otherwise show the start/end screen |
| 53 | + endGame(); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function startGame() { |
| 58 | + // Reset the score to 0 |
| 59 | + score = 0; |
| 60 | + |
| 61 | + // Start with the circle's radius maximum at half the minimum canvas dimension |
| 62 | + circleMaximumRadius = min(height / 2, width / 2); |
| 63 | + resetCircle(); |
| 64 | +} |
| 65 | + |
| 66 | +function endGame() { |
| 67 | + // Pause the sketch |
| 68 | + noLoop(); |
| 69 | + |
| 70 | + // Store the new high score |
| 71 | + highScore = max(highScore, score); |
| 72 | + storeItem('high score', highScore); |
| 73 | + |
| 74 | + textAlign(CENTER, CENTER); |
| 75 | + fill(220); |
| 76 | + let startText = `Circle Clicker |
| 77 | + Click the circle before it gets too small |
| 78 | + Score: ${score} |
| 79 | + High score: ${highScore} |
| 80 | + Click to play`; |
| 81 | + text(startText, 0, 0, width, height); |
| 82 | + describeElement('Start text', `Text reading, "${startText}"`); |
| 83 | +} |
| 84 | + |
| 85 | +function resetCircle() { |
| 86 | + // Start with the circle's radius at its maximum value |
| 87 | + circleRadius = circleMaximumRadius; |
| 88 | + circleX = random(circleRadius, width - circleRadius); |
| 89 | + circleY = random(circleRadius, height - circleRadius); |
| 90 | + circleColor = color(random(240, 360), random(40, 80), random(50, 90)); |
| 91 | +} |
| 92 | + |
| 93 | +function mousePressed() { |
| 94 | + // If the game is unpaused |
| 95 | + if (isLooping() === true) { |
| 96 | + // Check how far the mouse is from the circle |
| 97 | + let distanceToCircle = dist(mouseX, mouseY, circleX, circleY); |
| 98 | + |
| 99 | + // If the mouse is closer to the circle's center than the circle's radius, |
| 100 | + // that means the player clicked on it |
| 101 | + if (distanceToCircle < circleRadius) { |
| 102 | + // Decrease the maximum radius, but don't go below 15 |
| 103 | + circleMaximumRadius = max(circleMaximumRadius - 1, 15); |
| 104 | + resetCircle(); |
| 105 | + |
| 106 | + // Give the player a point |
| 107 | + score += 1; |
| 108 | + } |
| 109 | + // If the game is paused |
| 110 | + } else { |
| 111 | + // Start and unpause it |
| 112 | + startGame(); |
| 113 | + loop(); |
| 114 | + } |
| 115 | +} |
0 commit comments