xxxxxxxxxx
224
class SlotMachine {
constructor() {
this.slots = [[0, 0, 0,0, 0, 0 ], [0, 0, 0,0, 0, 0], [0, 0,0, 0, 0, 0]]; // Initialize slots to all 0
this.slotPositions = [0, 0, 0]; // Initialize slot positions to all 0
this.slotSpeeds = [0,0,0]; // Set the slot speeds
this.winningCombinations = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]; // Set winning combinations
this.won = false; // Initialize won to false
this.spinning = true; // Initialize spinning to true
// Preload sound effects
this.spinningSound = loadSound("spinning-slots.mp3");
this.wonSound = loadSound("won-slots.mp3");
}
spin() {
// Generate random numbers for each slot
for (let i = 0; i < this.slots.length; i++) {
this.slots[i] = this.slots[i].map((e) => floor(random(1,5)))
this.slotSpeeds[i] = random(4,10);
}
// Play the spinning sound
this.spinningSound.play();
this.spintime = 0;
}
update() {
// Update the position of each slot based on the slot speed
this.spintime++;
for (let i = 0; i < this.slots.length; i++) {
this.slotPositions[i] += this.slotSpeeds[i];
if (this.spintime > 450 && this.slotPositions[i] >= 95 && this.slotPositions[i] <= 105 && this.slotSpeeds[i] > 0) {
this.slotSpeeds[i] = 0;
this.slotPositions[i] = 100;
} else
if (this.slotPositions[i] >= 200) {
this.slotPositions[i] = -50;
}
}
// Check if the player has won
for (let i = 0; i < this.winningCombinations.length; i++) {
let combination = this.winningCombinations[i];
if (arraysEqual(this.slots[0], combination) && arraysEqual(this.slots[1], combination) && arraysEqual(this.slots[2], combination)) {
this.won = true;
this.wonSound.play();
break;
}
}
// Check if the slots are still spinning
this.spinning = false;
for (let i = 0; i < this.slotSpeeds.length; i++) {
if (this.slotSpeeds[i] > 0) {
this.spinning = true;
break;
}
}
}
draw() {
// Draw the slots
for (let i = 0; i < this.slots.length; i++) {
push();
translate(i * 100 + 50, 200); // Position the slot
rect(-50, -200, 100, 400); // Draw the slot
for (let j = 0; j < 6; j++) {
let y = -200 + this.slotPositions[i] + j * 100;
for (let k = 0; k < this.slots[i].length; k++) {
fill(getColor(this.slots[i][k])); // Set the tile color
rect(-50, y + k * 100, 100, 100); // Draw the tile
fill(0); // Set the text color to black
text(this.slots[i][k], 0, y + k * 100 + 50); // Draw the tile's number
}
}
pop();
}
// Display a message if the player has won
if (this.won) {
fill(0);
textSize(32);
textAlign(CENTER, CENTER);
text("You won!", width / 2, height / 2);
}
}
}
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
class SlotMachine2 {
constructor() {
this.slots = [0, 0, 0]; // Initialize slots to all 0
this.slotPositions = [0, 0, 0]; // Initialize slot positions to all 0
this.slotSpeed = 2; // Set the slot speed
this.winningCombinations = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]; // Set winning combinations
this.won = false; // Initialize won to false
// Preload sound effects
this.spinningSound = loadSound("spinning-slots.mp3");
this.wonSound = loadSound("won-slots.mp3");
}
spin() {
// Generate random numbers for each slot
this.slots = [
floor(random(1, 4)),
floor(random(1, 4)),
floor(random(1, 4))
];
// Play the spinning sound
this.spinningSound.play();
}
update() {
// Update the position of each slot based on the slot speed
this.slotPositions[0] += this.slotSpeed;
this.slotPositions[1] += this.slotSpeed;
this.slotPositions[2] += this.slotSpeed;
// If a slot has reached the end, reset its position to 0
for (let i = 0; i < this.slotPositions.length; i++) {
if (this.slotPositions[i] >= 200) {
this.slotPositions[i] = 0;
}
}
// Check if the player has won
for (let i = 0; i < this.winningCombinations.length; i++) {
let combination = this.winningCombinations[i];
if (arraysEqual(this.slots, combination)) {
this.won = true;
this.wonSound.play();
break;
}
}
}
draw() {
// Draw the slots
for (let i = 0; i < this.slots.length; i++) {
push();
translate(i * 100 + 50, 200); // Position the slot
rect(-50, -200, 100, 400); // Draw the slot
for (let j = 0; j < 6; j++) {
let y = -200 + this.slotPositions[i] + j * 100;
fill(getColor(this.slots[i])); // Set the tile color
rect(-50, y, 100, 100); // Draw the tile
fill(0); // Set the text color to black
text(this.slots[i], 0, y + 50); // Draw the tile's number
}
pop();
}
// Display a message if the player has won
if (this.won) {
fill(0);
textSize(32);
textAlign(CENTER, CENTER);
text("You won!", width / 2, height / 2);
}
}
}
function getColor(number) {
switch (number) {
case 1:
return color(255, 0, 0); // Red
case 2:
return color(0, 255, 0); // Green
case 3:
default:
return color(0, 0, 255); // Blue
}
}
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
let slotMachine;
function setup() {
createCanvas(300, 400);
slotMachine = new SlotMachine();
}
function draw() {
background(220);
slotMachine.update();
slotMachine.draw();
}
function mousePressed() {
// Spin the slots when the mouse is pressed
slotMachine.spin();
}