xxxxxxxxxx
181
// let
let game = {
state: "intro",
startButtonImg: null,
shapes: [],
numItems: 20,
player: null,
score: 0,
bottleImg: null,
bgMusic: null,
introBackgroundImg: null,
gameBackgroundImg: null,
endBackgroundImg: null,
restartButtonImg: null,
timer: null,
font: null,
};
//yüklemeler
function preload() {
game.bottleImg = loadImage("bottle.png");
game.startButtonImg = loadImage("start.png");
game.bgMusic = loadSound("music.mp3");
game.introBackgroundImg = loadImage("intro.jpg");
game.gameBackgroundImg = loadImage("background.jpg");
game.endBackgroundImg = loadImage("end.jpg");
game.restartButtonImg = loadImage("restart.png");
game.font = loadFont("BebasNeue-Regular.ttf");
}
//setup
function setup() {
createCanvas(800, 800);
game.player = { x: width / 2, y: height - 50, size: 100 };
game.timer = new Timer(game);
textFont(game.font);
}
function draw() {
if (game.state === "intro") {
drawIntroScreen();
} else if (game.state === "playing") {
playGame();
} else if (game.state === "end") {
drawEndScreen();
}
}
//giriş ekranı
function drawIntroScreen() {
imageMode(CORNER);
image(game.introBackgroundImg, 0, 0, width, height);
imageMode(CENTER);
image(game.startButtonImg, 400, 500, 150, 75);
}
//bitiş ekranı
function drawEndScreen() {
imageMode(CORNER);
image(game.endBackgroundImg, 0, 0, width, height);
push();
textFont(game.font);
textAlign(CENTER, CENTER);
textSize(56);
fill(255);
text(`${game.score}`, width / 2, height / 2 - 30);
pop();
// Restart butonu
imageMode(CENTER);
image(game.restartButtonImg, 400, 500, 150, 75);
}
//buton kontrolü
function mousePressed() {
// Start ve Restart butonları aynı konumda
if (
mouseX > 400 - 75 &&
mouseX < 400 + 75 &&
mouseY > 500 - 37.5 &&
mouseY < 500 + 37.5
) {
if (game.state === "intro" || game.state === "end") {
if (game.state === "end") {
game.timer.restart();
}
startGame();
}
}
}
//oyunu başlatmak için
function startGame() {
game.state = "playing";
game.score = 0;
//timer başlat
game.timer.setCountDown(60);
game.timer.start();
//müzik
if (!game.bgMusic.isPlaying()) {
game.bgMusic.loop();
}
//yeni şekiller oluştur
game.shapes = [];
for (let i = 0; i < game.numItems; i++) {
let x = random(0, width);
let y = random(-100, -200);
game.shapes.push(new Atom(x, y));
}
}
function playGame() {
//background
imageMode(CORNER);
image(game.gameBackgroundImg, 0, 0, width, height);
imageMode(CENTER);
//timer güncelle
game.timer.update();
game.timer.draw(width / 2, 100);
//timer bitince oyuun bitirme
if (game.timer.isFinished) {
game.state = "end";
return;
}
//skor
push();
textFont(game.font);
fill(255);
textSize(24);
textAlign(CENTER);
text(`Score: ${game.score}`, width / 2, 50);
pop();
//şişe gösterimi
image(
game.bottleImg,
game.player.x,
game.player.y,
game.player.size,
game.player.size
);
//atomları göster
for (let i = game.shapes.length - 1; i >= 0; i--) {
let shape = game.shapes[i];
shape.centerY += shape.speed;
shape.display();
//molekülleri toplamak için
if (
shape.centerY + 15 >= game.player.y - game.player.size / 2 &&
shape.centerX > game.player.x - game.player.size / 2 &&
shape.centerX < game.player.x + game.player.size / 2
) {
game.shapes.splice(i, 1);
game.score++;
game.shapes.push(new Atom(random(width), random(-100, -200)));
}
//kaçan şekilleri yenilemek
if (shape.centerY > height + 300) {
shape.centerY = random(-100, -200);
}
}
//kontroller ve sınır kontrolü
if (keyIsDown(LEFT_ARROW)) {
game.player.x = max(game.player.size / 2, game.player.x - 5);
}
if (keyIsDown(RIGHT_ARROW)) {
game.player.x = min(width - game.player.size / 2, game.player.x + 5);
}
}