xxxxxxxxxx
354
// Hunting Game in p5.js
// Variables for interactive elements and animations
let rain = [];
let clouds = [];
let trees = [];
let stars = [];
let birds;
let deer;
let vibe = "homepage"; // homepage, sunny, rainy, night
let ambientMusic;
let backButton;
let score = 0;
let popSound;
function preload() {
// Load ambient music and pop sound
ambientMusic = loadSound('ambient-music.mp3');
popSound = loadSound('pop.mp3');
birds = loadImage('bird.png')
deer = loadImage('deer.png')
}
function setup() {
createCanvas(windowWidth, windowHeight);
initializeRain();
initializeClouds();
initializeTrees();
initializeStars();
initializeBirds();
initializeDeer();
createButtons();
// Play ambient music in a loop
if (ambientMusic) {
ambientMusic.loop();
ambientMusic.setVolume(0.5); // Set volume to 50%
}
}
function draw() {
background(getBackgroundColor());
if (vibe === "homepage") {
drawHomepage();
} else {
// Draw scene based on current vibe
drawScene();
// Draw interactive elements like clouds and rain
if (vibe === "rainy") {
drawRain();
}
drawClouds();
drawTrees();
drawGround();
drawBirds();
drawDeer();
// Show back button
if (!backButton) {
backButton = createButton("Back to Homepage");
backButton.position(10, height - 40);
backButton.mousePressed(() => {
vibe = "homepage";
backButton.hide();
});
}
backButton.show();
// Draw score counter
fill(255);
textSize(24);
textAlign(LEFT, TOP);
text(`Score: ${score}`, 10, 10);
}
}
// --- Initialization Functions ---
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() {
let sunnyButton = createButton("Sunny Scene");
sunnyButton.position(width / 2 - 60, height / 2);
sunnyButton.mousePressed(() => {
vibe = "sunny";
sunnyButton.hide();
rainyButton.hide();
nightButton.hide();
});
let rainyButton = createButton("Rainy Scene");
rainyButton.position(width / 2 - 60, height / 2 + 50);
rainyButton.mousePressed(() => {
vibe = "rainy";
sunnyButton.hide();
rainyButton.hide();
nightButton.hide();
});
let nightButton = createButton("Night Scene");
nightButton.position(width / 2 - 60, height / 2 + 100);
nightButton.mousePressed(() => {
vibe = "night";
sunnyButton.hide();
rainyButton.hide();
nightButton.hide();
});
}
// --- Animation Functions ---
function drawRain() {
for (let drop of rain) {
stroke(173, 216, 230);
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;
// Draw trunk
stroke(139, 69, 19);
strokeWeight(10);
line(0, 0, sway, -tree.height);
// Draw pine leaves
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 drawBirds() {
for (let i = birds.length - 1; i >= 0; i--) {
let bird = birds[i];
fill(255, 0, 0);
ellipse(bird.x, bird.y, bird.size, bird.size / 2);
bird.x += bird.speed;
if (bird.x > width + 50) {
bird.x = random(-200, -50);
bird.y = random(height / 4, height / 2);
}
}
}
function drawDeer() {
for (let i = deer.length - 1; i >= 0; i--) {
let d = deer[i];
fill(139, 69, 19);
rect(d.x, d.y - d.size / 2, d.size, d.size / 2);
d.x += d.speed;
if (d.x > width + 50) {
d.x = random(-200, -50);
}
}
}
function mousePressed() {
for (let i = birds.length - 1; i >= 0; i--) {
let bird = birds[i];
if (dist(mouseX, mouseY, bird.x, bird.y) < 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];
if (
mouseX > d.x &&
mouseX < d.x + d.size &&
mouseY > d.y - d.size / 2 &&
mouseY < d.y
) {
deer.splice(i, 1);
score++;
if (popSound) popSound.play();
}
}
}
// --- Scene Functions ---
function drawHomepage() {
textAlign(CENTER, CENTER);
textSize(32);
fill(255);
text("Welcome to the Hunting Simulator", width / 2, height / 3);
let sunnyButton = createButton("Sunny Scene");
sunnyButton.position(width / 2 - 60, height / 2);
sunnyButton.mousePressed(() => {
vibe = "sunny";
sunnyButton.hide();
rainyButton.hide();
nightButton.hide();
});
let rainyButton = createButton("Rainy Scene");
rainyButton.position(width / 2 - 60, height / 2 + 50);
rainyButton.mousePressed(() => {
vibe = "rainy";
sunnyButton.hide();
rainyButton.hide();
nightButton.hide();
});
let nightButton = createButton("Night Scene");
nightButton.position(width / 2 - 60, height / 2 + 100);
nightButton.mousePressed(() => {
vibe = "night";
sunnyButton.hide();
rainyButton.hide();
nightButton.hide();
});
}
function drawScene() {
if (vibe === "sunny") {
noStroke();
fill(255, 223, 0);
ellipse(width - 100, 100, 80, 80);
} else if (vibe === "night") {
noStroke();
fill(255, 255, 153);
ellipse(width - 100, 100, 50, 50);
drawStars();
}
}
function drawStars() {
for (let star of stars) {
fill(255);
ellipse(star.x, star.y, 2, 2);
}
}
function getBackgroundColor() {
if (vibe === "homepage") {
return color(50, 50, 50);
} 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 drawGround() {
// Draw dirt layer
noStroke();
fill(139, 69, 19);
rect(0, height - 50, width, 50);
// Draw grass on top of the dirt
fill(34, 139, 34);
for (let i = 0; i < width; i += 10) {
triangle(i, height - 50, i + 5, height - 60, i + 10, height - 50);
}
}