xxxxxxxxxx
253
let player;
let star1, star2;
let collisionRadius = 10;
let score = 0;
let gameState = 0;
let charIdSprite, charWalkSprite, charEatSprite;
let starSprite, disSprite;
let startButtonImage, instructionsButtonImage;
let titleScreen, intructionsScreen;
let gameBackground;
let backgroundMusic, popUpSound;
function preload() {
charIdSprite = loadImage("sprites/stay.gif");
charWalkSprite = loadImage("sprites/New Piskel (3).gif");
charEatSprite = loadImage("sprites/New Piskel (4).gif");
starSprite = loadImage("sprites/star.gif");
disSprite = loadImage("sprites/dis.gif");
backgroundMusic = loadSound("audio/space.mp3");
popUpSound = loadSound("audio/popup.mp3");
gameBackground = loadImage("back/back1.png");
startButtonImage = loadImage("button/bu0.png");
instructionsButtonImage = loadImage("button/bu1.png");
titleScreen = loadImage("back/back0.png");
instructionsScreen = loadImage("back/back2.png");
}
function setup() {
createCanvas(400, 400);
imageMode(CENTER);
player = Player();
star1 = Star();
star2 = Star();
startButton = Button(width / 2 - 80, height - 60, startButtonImage);
instructionsButton = Button(width / 2 + 80, height - 60, instructionsButtonImage);
backgroundMusic.play();
backgroundMusic.loop(true);
}
function draw() {
background(220);
if (gameState == 0) {
startMenu();
} else if (gameState == 1) {
intructionsMenu();
} else if (gameState == 2) {
game();
}
}
function startMenu() {
image(titleScreen, width / 2, height / 2);
startButton.display();
instructionsButton.display();
if (startButton.clicked()) {
gameState = 2;
}
if (instructionsButton.clicked()) {
gameState = 1;
}
}
function intructionsMenu() {
image(instructionsScreen, width / 2, height / 2);
startButton.display();
if (startButton.clicked()) {
gameState = 2;
}
}
function Button(x, y, img) {
let w = img.width;
let h = img.height;
let isClicked = false;
function display() {
image(img, x, y);
}
function clicked() {
if (mouseIsPressed) {
if (
mouseX > x - w / 2 &&
mouseX < x + w / 2 &&
mouseY > y - h / 2 &&
mouseY < y + h / 2
) {
if (!isClicked) {
isClicked = true;
return true;
}
}
} else if (isClicked) {
isClicked = false;
}
return false;
}
return { display, clicked };
}
function game() {
image(gameBackground, width / 2, height / 2);
player.move();
player.display();
star1.move();
star1.display();
star2.move();
star2.display();
let star1Collide = detectCollision(player, star1);
if (star1Collide) {
player.eat();
star1.dissapear();
score = score + 1;
if (!popUpSound.isPlaying()) {
popUpSound.play();
}
}
let star2Collide = detectCollision(player, star2);
if (star2Collide) {
player.eat();
star2.dissapear();
score = score + 1;
if (score >= 50) {
gameState = 0;
score = 0;
star1.reset();
star2.reset();
}
}
fill("black");
textSize(20);
textFont("Time New Roman");
text("Score: " + score, 20, 30);
}
function Player() {
let x = 200;
let y = 300;
let animationState = 0;
function move() {
if (animationState != 2) {
animationState = 0;
}
if (keyIsDown(RIGHT_ARROW)) {
x = x + 3;
animationState = 1;
}
if (keyIsDown(LEFT_ARROW)) {
x = x - 3;
animationState = 1;
}
}
function display() {
if (animationState == 0) {
image(charIdSprite, x, y);
} else if (animationState == 1) {
image(charWalkSprite, x, y);
} else if (animationState == 2) {
image(charEatSprite, x, y);
}
}
function getPosition() {
return { x, y };
}
function eat() {
animationState = 2;
}
return { move, display, getPosition, eat };
}
function Star() {
let x = random(0, width);
let y = 0;
let speed = random(3, 8);
let animationState = 0;
function move() {
y = y + speed;
if (y > height) {
reset();
}
}
function reset() {
y = 0;
x = random(0, width);
speed = random(2, 8);
animationState = 0;
}
function display() {
if (animationState == 0) {
image(starSprite, x, y);
} else if (animationState == 1) {
image(disSprite, x, y);
}
}
function getPosition() {
return { x, y };
}
function dissapear() {
animationState = 1;
}
return { move, display, getPosition, dissapear, reset };
}
function detectCollision(objA, objB) {
let aPosition = objA.getPosition();
let bPosition = objB.getPosition();
let d = dist(aPosition.x, aPosition.y, bPosition.x, bPosition.y);
let isColliding = d < collisionRadius;
return isColliding;
}
function mousePressed() {
}