xxxxxxxxxx
149
// Global variables
let slider;
let coins = [];
let prize;
let tokens = [];
let knife;
let watch;
let score = 0;
let attempts = 3;
function setup() {
createCanvas(600, 400);
// Create the slider
slider = createSlider(0, width, width / 2, 10);
slider.position(50, height - 50);
// Create the coins
for (let i = 0; i < 10; i++) {
let x = random(width);
let y = random(height / 2);
coins.push(new Coin(x, y));
}
// Create the prize tokens
for (let i = 0; i < 3; i++) {
tokens.push(new Token());
}
// Create the special prizes
knife = new SpecialPrize("Knife");
watch = new SpecialPrize("Designer Watch");
}
function draw() {
background(220);
// Update and display the coins
for (let i = coins.length - 1; i >= 0; i--) {
let coin = coins[i];
coin.update();
coin.display();
// Check if the coin falls off the edge
if (coin.isOffEdge()) {
coins.splice(i, 1);
if (coin.isJackpot()) {
score++;
if (score === 3) {
prize = tokens.pop();
}
if (score === 5) {
prize = knife;
}
if (score === 7) {
prize = watch;
}
}
}
}
// Display the slider
let sliderX = slider.value();
fill(255, 0, 0);
rect(sliderX - 25, height - 70, 50, 20);
// Check if the slider pushes any coins
for (let coin of coins) {
if (coin.hitsSlider(sliderX)) {
coin.push();
}
}
// Display the prize
if (prize) {
fill(255);
textSize(20);
textAlign(CENTER);
text(`Congratulations! You won: ${prize.name}`, width / 2, height - 100);
}
// Display the score and attempts
fill(0);
textSize(16);
textAlign(LEFT);
text(`Score: ${score}`, 20, 30);
text(`Attempts: ${attempts}`, 20, 60);
}
function mousePressed() {
if (attempts > 0) {
let x = mouseX;
let y = mouseY;
coins.push(new Coin(x, y));
attempts--;
}
}
class Coin {
constructor(x, y) {
this.x = x;
this.y = y;
this.speed = random(1, 3);
this.radius = 15;
this.jackpot = random() < 0.1; // 10% chance of jackpot
}
update() {
this.y += this.speed;
}
display() {
fill(255);
ellipse(this.x, this.y, this.radius * 2);
}
isOffEdge() {
return this.y > height + this.radius;
}
isJackpot() {
return this.jackpot;
}
hitsSlider(sliderX) {
return (
this.y + this.radius > height - 70 &&
this.x > sliderX - 25 &&
this.x < sliderX + 25
);
}
push() {
this.speed += 0.5;
}
}
class Token {
constructor() {
this.name = "Token";
}
}
class SpecialPrize {
constructor(name) {
this.name = name;
}
}