xxxxxxxxxx
let y;
let w = 15;
let h = 15;
let redx;
let redy;
let a = 15;
let b = 15;
let s = 1;
let score = 0;
let stop_game = false;
let highscore = 0;
let prev_key = null;
let princess; // Princess image
let backgroundImage; // Background image
let objectImage; // Key image
let video;
let detections;
let faceapi;
const detection_options = { withLandmarks: true, withDescriptors: false };
function preload() {
princess = loadImage('princess.png');
backgroundImage = loadImage('castle.png');
objectImage = loadImage('key.png');
}
function setup() {
createCanvas(windowWidth, windowHeight);
x = width / 2;
y = height / 2;
redx = random(width / 4, 3 * width / 4); // x koordinatı ekranın yatay ortasına yakın bir konumda
redy = random(height / 4, 3 * height / 4); // y koordinatı ekranın dikey ortasına yakın bir konumda
video = createCapture(VIDEO);
video.size(width, height);
video.hide();
faceapi = ml5.faceApi(video, detection_options, modelReady);
// Resize the background image to fit canvas size
backgroundImage.resize(width, height);
rectMode(CENTER);
}
function modelReady() {
console.log('Face API model loaded');
faceapi.detect(gotResults);
}
function gotResults(err, result) {
if (err) {
console.error(err);
return;
}
detections = result;
faceapi.detect(gotResults);
if (detections && detections.length > 0) {
const alignedRect = detections[0].alignedRect;
const box = alignedRect._box;
x = map(video.width - (box._x + box._width / 2), 0, video.width, 0, width); // Aynalama işlemi
y = map(box._y + box._height / 2, 0, video.height, 0, height);
}
}
function draw() {
if (stop_game == true) {
if (keyCode == ENTER) {
restart_game();
}
} else {
// Draw the background image
image(backgroundImage, 0, 0);
// Draw the princess image
image(princess, x - 120, y - 120, 320, 400);
textSize(40);
fill(255, 0, 0);
textFont('Haettenschweiler');
text("Help the princess regain her freedom by collecting the keys!", width / 4, 80);
fill(232, 67, 147);
text("Score:", width - 180, 460);
text(score, width - 120, 500);
text("Highscore:", 100, 50);
text(highscore, 280, 51);
if (
// Check top line
(y - 20 < 100) ||
// Check bottom line
(y + 80 > height - 50) ||
// Check left line
(x - 20 < 0) ||
// Check right line
(x + 100 > width - 50)
) {
game_over();
}
if (keyCode == UP_ARROW) {
move_up();
prev_key = UP_ARROW;
} else if (keyCode == DOWN_ARROW) {
move_down();
prev_key = DOWN_ARROW;
} else if (keyCode == LEFT_ARROW) {
move_left();
prev_key = LEFT_ARROW;
} else if (keyCode == RIGHT_ARROW) {
move_right();
prev_key = RIGHT_ARROW;
} else if (prev_key == LEFT_ARROW) {
move_left();
prev_key = LEFT_ARROW;
} else if (prev_key == UP_ARROW) {
move_up();
prev_key = UP_ARROW;
} else if (prev_key == RIGHT_ARROW) {
move_right();
prev_key = RIGHT_ARROW;
} else if (prev_key == DOWN_ARROW) {
move_down();
prev_key = DOWN_ARROW;
}
// Draw the dangerous-looking spikes
drawDangerousSpikes(0, 100, 0, height - 50); // Left line
drawDangerousSpikes(0, height - 50, width - 50, height - 50); // Bottom line
drawDangerousSpikes(width - 50, 100, width - 50, height); // Right line
drawDangerousSpikes(0, 100, width - 50, 100); // Top line
// Draw the object image
image(objectImage, redx - 30, redy - 30, 60, 60);
if (x + w / 0.7 >= redx - 30 / 0.7 &&
x - w / 0.7 <= redx + 30 / 0.7 &&
y - h / 0.7 <= redy + 30 / 0.7 &&
y + h / 0.7 >= redy - 30 / 0.7) {
s += 0.5;
score++;
redx = random(width / 4, 3 * width / 4); // x koordinatı ekranın yatay ortasına yakın bir konumda
redy = random(height / 4, 3 * height / 4); // y koordinatı ekranın dikey ortasına yakın bir konumda
} else if (score > highscore) {
highscore++;
}
if (x > width - 50) {
game_over();
}
if (x < 50) {
game_over();
}
if (y > height - 50) {
game_over();
}
if (y < 100) {
game_over();
}
}
}
function drawDangerousSpikes(x1, y1, x2, y2) {
stroke(255);
fill(255);
let spacing = 50; // Distance between spikes
let spikeHeight = 30; // Height of the spikes
let spikeWidth = 15; // Width of the base of the spikes
if (x1 === x2) { // Vertical line
for (let y = y1 + spacing / 2; y < y2; y += spacing) {
triangle(x1, y, x1 - spikeWidth, y + spikeHeight, x1 + spikeWidth, y + spikeHeight);
triangle(x1, y - spacing / 2, x1 - spikeWidth, y + spikeHeight - spacing / 2, x1 + spikeWidth, y + spikeHeight - spacing / 2);
}
} else if (y1 === y2) { // Horizontal line
for (let x = x1 + spacing / 2; x < x2; x += spacing) {
triangle(x, y1, x + spikeHeight, y1 - spikeWidth, x + spikeHeight, y1 + spikeWidth);
triangle(x - spacing / 2, y1, x + spikeHeight - spacing / 2, y1 - spikeWidth, x + spikeHeight - spacing / 2, y1 + spikeWidth);
}
}
}
function restart_game() {
x = width / 2;
y = height / 2;
s = 1;
prev_key = false;
highscore = highscore;
score = 0;
stop_game = false;
// Draw the background image
image(backgroundImage, 0, 0);
// Draw the princess image
image(princess, x - 120, y - 120, 320, 400);
textSize(40);
textFont('Haettenschweiler');
fill(255, 0, 0);
text("Help the princess regain her freedom by collecting the keys!", width / 4, 80);
fill(232, 67, 147);
text("Highscore:", 100, 50);
text(highscore, 280, 51);
fill(232, 67, 147);
text("Score:", width - 180, 460);
text(score, width - 120, 500);
strokeWeight(10);
line(0, 100, 0, height - 50);
line(0, height - 50, width - 50, height - 50);
line(width - 50, 100, width - 50, height);
line(0, 100, width - 50, 100);
fill(255, 0, 0);
// Draw the object image
image(objectImage, redx - 30, redy - 30, 60, 60);
if (score > highscore) {
highscore++;
}
}
function game_over() {
stop_game = true;
textSize(120);
text("Game Over", width / 4, height / 2);
textSize(80);
text("Press Enter To Reset", width / 4 - 100, height / 2 + 75);
if (keyCode == ENTER) {
restart_game();
}
}
function move_up() {
y = y - s;
image(princess, x - 120, y - 120, 320, 400);
}
function move_down() {
y = y + s;
image(princess, x - 120, y - 120, 320, 400);
}
function move_left() {
x = x - s;
image(princess, x - 120, y - 120, 320, 400);
}
function move_right() {
x = x + s;
image(princess, x - 120, y - 120, 320, 400);
}