xxxxxxxxxx
396
let rain = [];
let clouds = [];
let trees = [];
let stars = [];
let birds = [];
let deer = [];
let vibe = "homepage";
let backButton;
let score = 0;
let birdImage, deerImage;
let popSound, ambientMusic;
let birdSpawnTimer = 0;
let deerSpawnTimer = 0;
function preload() {
birdImage = loadImage("bird.png");
deerImage = loadImage("deer.png");
popSound = loadSound("pop.mp3");
ambientMusic = loadSound("ambientmusic.mp3", () => {
ambientMusic.loop();
ambientMusic.setVolume(0.5);
});
}
function setup() {
createCanvas(windowWidth, windowHeight);
initializeRain();
initializeClouds();
initializeTrees();
initializeStars();
initializeBirds();
initializeDeer();
createButtons();
}
function drawHomepage() {
background(135, 206, 250);
textAlign(CENTER, CENTER);
textSize(32);
fill(255);
text("Welcome to the Hunting Simulator", width / 2, height / 3);
// Remove existing buttons if they were created previously
if (typeof sunnyButton !== "undefined") sunnyButton.show();
if (typeof rainyButton !== "undefined") rainyButton.show();
if (typeof nightButton !== "undefined") nightButton.show();
if (backButton) backButton.hide();
}
function drawScene() {
if (vibe === "sunny") {
noStroke();
fill(255, 223, 0);
ellipse(width - 100, 100, 80, 80);
} else if (vibe === "rainy") {
noStroke();
fill(173, 216, 230);
rect(0, 0, width, height);
} else if (vibe === "night") {
noStroke();
fill(255, 255, 153);
ellipse(width - 100, 100, 50, 50);
drawStars();
}
}
function getBackgroundColor() {
if (vibe === "homepage") {
return color(135, 206, 250);
} else if (vibe === "sunny") {
return color(135, 206, 250);
} else if (vibe === "rainy") {
return color(100, 149, 237);
} else if (vibe === "night") {
return color(25, 25, 112);
}
return color(200);
}
function draw() {
background(getBackgroundColor());
if (vibe === "homepage") {
drawHomepage();
} else {
drawScene();
if (vibe === "rainy") {
drawRain();
}
drawClouds();
drawTrees();
drawGround();
drawBirds();
drawDeer();
if (!backButton) {
backButton = createButton("Back to Homepage");
backButton.position(10, height - 40);
backButton.mousePressed(() => {
vibe = "homepage";
});
}
backButton.show();
fill(255);
textSize(24);
textAlign(LEFT, TOP);
text(`Score: ${score}`, 10, 10);
}
}
function initializeRain() {
for (let i = 0; i < 100; i++) {
rain.push({
x: random(width),
y: random(-height, 0),
speed: random(2, 5),
});
}
}
function initializeClouds() {
for (let i = 0; i < 5; i++) {
clouds.push({
x: random(width),
y: random(height / 4),
speed: random(1, 3),
});
}
}
function initializeTrees() {
for (let i = 0; i < 10; i++) {
trees.push({
x: random(width),
height: random(100, 150),
swayOffset: random(TWO_PI),
});
}
}
function initializeStars() {
for (let i = 0; i < 50; i++) {
stars.push({
x: random(width),
y: random(height / 2),
});
}
}
function initializeBirds() {
for (let i = 0; i < 5; i++) {
birds.push({
x: random(-200, -50),
y: random(height / 4, height / 2),
speed: random(2, 5),
size: random(30, 50),
});
}
}
function initializeDeer() {
for (let i = 0; i < 5; i++) {
deer.push({
x: random(-200, -50),
y: height - 100,
speed: random(3, 6),
size: random(40, 60),
});
}
}
function createButtons() {
sunnyButton = createButton("Sunny Scene");
sunnyButton.position(width / 2 - 60, height / 2);
sunnyButton.mousePressed(() => {
vibe = "sunny";
hideSceneButtons();
});
rainyButton = createButton("Rainy Scene");
rainyButton.position(width / 2 - 60, height / 2 + 50);
rainyButton.mousePressed(() => {
vibe = "rainy";
hideSceneButtons();
});
nightButton = createButton("Night Scene");
nightButton.position(width / 2 - 60, height / 2 + 100);
nightButton.mousePressed(() => {
vibe = "night";
hideSceneButtons();
});
}
function hideSceneButtons() {
sunnyButton.hide();
rainyButton.hide();
nightButton.hide();
}
function drawRain() {
for (let drop of rain) {
stroke(52, 113, 235);
line(drop.x, drop.y, drop.x, drop.y + 10);
drop.y += drop.speed;
if (drop.y > height) {
drop.y = random(-20, 0);
drop.x = random(width);
}
}
}
function drawClouds() {
noStroke();
fill(255, 255, 255, 200);
for (let cloud of clouds) {
ellipse(cloud.x, cloud.y, 100, 60);
ellipse(cloud.x + 30, cloud.y + 10, 80, 50);
ellipse(cloud.x - 30, cloud.y + 10, 80, 50);
cloud.x += cloud.speed;
if (cloud.x > width + 50) {
cloud.x = -50;
cloud.y = random(height / 4);
}
}
}
function drawTrees() {
for (let tree of trees) {
push();
translate(tree.x, height - 50);
let sway = sin(frameCount * 0.01 + tree.swayOffset) * 5;
stroke(139, 69, 19);
strokeWeight(10);
line(0, 0, sway, -tree.height);
fill(34, 139, 34);
noStroke();
triangle(
sway - 30,
-tree.height + 30,
sway,
-tree.height - 20,
sway + 30,
-tree.height + 30
);
triangle(
sway - 25,
-tree.height + 50,
sway,
-tree.height,
sway + 25,
-tree.height + 50
);
triangle(
sway - 20,
-tree.height + 70,
sway,
-tree.height + 30,
sway + 20,
-tree.height + 70
);
pop();
}
}
function drawGround() {
// dirt
noStroke();
fill(139, 69, 19);
rect(0, height - 50, width, 50);
// grass
fill(34, 139, 34);
for (let i = 0; i < width; i += 10) {
triangle(i, height - 50, i + 5, height - 60, i + 10, height - 50);
}
}
function drawStars() {
for (let star of stars) {
fill(255);
noStroke();
ellipse(star.x, star.y, 2, 2);
}
}
function spawnBird() {
let newBird;
let tries = 0;
do {
newBird = {
x: random(-200, -50),
y: random(height / 4, height / 2),
speed: random(2, 5),
size: random(30, 50),
};
tries++;
} while (isOverlapping(newBird, birds) && tries < 10);
birds.push(newBird);
}
function spawnDeer() {
let newDeer;
let tries = 0;
do {
newDeer = {
x: random(-200, -50),
y: height - 100,
speed: random(3, 6),
size: random(40, 60),
};
tries++;
} while (isOverlapping(newDeer, deer) && tries < 10);
deer.push(newDeer);
}
function isOverlapping(newEntity, existingEntities) {
for (let entity of existingEntities) {
let distX = abs(newEntity.x - entity.x);
let distY = abs(newEntity.y - entity.y);
let minDist = (newEntity.size + entity.size) / 2;
if (distX < minDist && distY < minDist) {
return true;
}
}
return false;
}
function drawBirds() {
for (let i = birds.length - 1; i >= 0; i--) {
let bird = birds[i];
image(birdImage, bird.x, bird.y, bird.size, bird.size / 2);
bird.x += bird.speed;
if (bird.x > width + 50) {
birds.splice(i, 1); // Remove bird if it goes off-screen
}
}
birdSpawnTimer++;
if (birdSpawnTimer > random(50, 200)) {
spawnBird();
birdSpawnTimer = 0;
}
}
function drawDeer() {
for (let i = deer.length - 1; i >= 0; i--) {
let d = deer[i];
let dirtTopY = height - 50;
let deerBottomY = dirtTopY;
let deerTopY = deerBottomY - d.size;
image(deerImage, d.x, deerTopY, d.size, d.size);
d.x += d.speed;
if (d.x > width + 50) {
deer.splice(i, 1);
}
}
deerSpawnTimer++;
if (deerSpawnTimer > random(50, 200)) {
// Spawn a new deer every 3 seconds
spawnDeer();
deerSpawnTimer = 0;
}
}
function mousePressed() {
for (let i = birds.length - 1; i >= 0; i--) {
let bird = birds[i];
if (
dist(mouseX, mouseY, bird.x + bird.size / 2, bird.y + bird.size / 4) <
bird.size / 2
) {
birds.splice(i, 1);
score++;
if (popSound) popSound.play();
}
}
for (let i = deer.length - 1; i >= 0; i--) {
let d = deer[i];
let dirtTopY = height - 50; // Top of the dirt layer
let deerBottomY = dirtTopY; // Bottom of the deer
let deerTopY = deerBottomY - d.size; // Top of the deer
if (
mouseX > d.x &&
mouseX < d.x + d.size &&
mouseY > deerTopY &&
mouseY < deerBottomY
) {
deer.splice(i, 1);
score++;
if (popSound) popSound.play();
}
}
}