I have to create a game for my final project. The concept of the game is to move the square around and reach the white circle while avoiding the black circles. I was able to create a "Game Over" screen when the square touches the black circles, however, when I use the same logic for the white square it doesn't work. It works occasionally, when the square is in a certain position but the black circles are on top of the text.
My second question is, that I want to have a Title Screen that only runs in the beginning. I want the title screen to go away once the letter P is pressed on the keyboard. When I tried putting the "title screen" in the setup function, the game doesn't run. If I put the "title screen" in draw, it runs every time (I understand why though). I'm not skilled enough to write my own functions for the title screen either.
My main concern is the "You Won" screen though. If creating the "title screen" is too complex I don't need to add that in.
Here is my code: https://editor.p5js.org/pawlisko/sketches/WClx1upD-
var player;
var goals = [];
var enemies = [];
function setup() {
createCanvas(400, 400);
noStroke();
for(let i=0;i<150;i++) {
enemies.push(new Enemy());
}
for(let i=0;i<1;i++) {
goals.push(new Goal());
}
player = {};
player.x = 350;
player.y = 10;
player.w = 10;
player.h = 10;
}
function draw() {
background(0);
//start screen
fill(227, 101, 91);
textSize(50);
textAlign(CENTER);
text("Circle Maze",200, 200);
textSize(12);
textAlign(CENTER);
text("Press 'P' to play.",200, 250);
//text
text("Goal: Reach the gold square. Don't touch black circles.", 200, 290);
text("Use keys 'W, A, S, D' to move. Refresh page to play again.", 200, 330);
if(key == 'p'){
background(227, 101, 91);
//player
fill(255,183,173);
rect(player.x, player.y, player.w, player.h);
//goal
for(let i=0;i<goals.length;i++) {
goals[i].show();
var end = goals[i];
//game won
for(let g of goals) {
d = dist(player.x, player.y, g.x, g.y);
if(d<player.w && player.h + g.r) {
fill(0);
rect(0,0,400,400);
fill(227, 101, 91);
textSize(50);
textAlign(CENTER);
text("You Win",200, 200);
textSize(12);
textAlign(CENTER);
text("Press 'R' to play again.",200, 250);
}
}
}
//enemies
for(let i=0;i<enemies.length;i++) {
enemies[i].show();
var end2 = enemies[i];
//game over
for(let e of enemies) {
d = dist(player.x, player.y, e.x, e.y);
if(d<player.w && player.h + e.x) {
fill(0);
rect(0,0,400,400);
fill(227, 101, 91);
textSize(50);
textAlign(CENTER);
text("Game Over",200, 200);
textSize(12);
textAlign(CENTER);
text("Press 'R' to play again.",200, 250);
}
}
}
}
}
function keyTyped () {
//Moving character
if(key === 'w') {
player.y -= 10;
direction = 0;
}
else if(key === 's') {
player.y += 10;
direction = 1;
}
if(key === 'a'){
player.x -= 10;
direction = 3;
}
else if(key === 'd') {
player.x += 10;
direction = 2;
}
if(key === 'r') {
resetSketch();
}
}
class Enemy {
constructor() {
this.x = random(width);
this.y = random(height);
this.r = 8;
}
show() {
fill(5);
circle(this.x,this.y,this.r);
}
}
class Goal {
constructor() {
this.x = random(width);
this.y = random(height);
this.r = 50;
}
show() {
fill(255);
circle(this.x,this.y,this.r);
}
}