You have two pressing issues
1: sprite depth
2: not hiding sprites
var score = 0;
var lives = 9;
var started = false;
console.log("not started");
// main();
var main_screen = createSprite(200, 200);
var player = createSprite(200, 350);
player.setAnimation("player");
var target_1 = createSprite (90,30);
target_1.setAnimation("target_1");
target_1.scale = 0.1;
target_1.velocityY = 2;
var target_2 = createSprite (150,30);
target_2.setAnimation("target_2");
target_2.scale = 0.1;
target_2.velocityY = 2;
var target_3 = createSprite (220,30);
target_3.setAnimation("target_3");
target_3.scale = 0.1;
target_3.velocityY = 2;
var obstacle_1 = createSprite (290,30);
obstacle_1.setAnimation("obstacle_1");
obstacle_1.scale = 0.4;
obstacle_1.velocityY = 2;
var obstacle_2 = createSprite (370,30);
obstacle_2.setAnimation("obstacle_2");
obstacle_2.scale = 0.15;
obstacle_2.velocityY = 2;
var target_4 = createSprite(30, 30);
target_4.setAnimation("target_4");
target_4.scale = 0.1;
target_4.velocityY = 2;
var start_screen = createSprite(200, 200);
function draw() {
if (started) {
sprite_interactions();
player_controls();
looping();
}
drawSprites();
if ( started ) {
main();
score_lives();
} else {
console.log("false");
start();
}
if (keyDown ("space")) {
started = true;
start_screen.visible = false;
console.log("started");
}
win();
lose();
}
//player movement
function player_controls() {
if (keyDown("left")) {
player.velocityX = -3;
}
if (keyDown("right")) {
player.velocityX = 3;
}
}
//sprite looping across the screen
function looping() {
if (target_1.y > 405) {
target_1.x = randomNumber(4, 390);
target_1.y = -2;
}
if (target_2.y > 405) {
target_2.x = randomNumber(4, 390);
target_2.y = -2;
}
if (target_3.y > 405) {
target_3.x = randomNumber(4, 390);
target_3.y = -2;
}
if (target_4.y > 405) {
target_4.x = randomNumber(4, 390);
target_4.y = -2;
}
if (obstacle_1.y > 405) {
obstacle_1.x = randomNumber(4, 390);
obstacle_1.y = -2;
}
if (obstacle_2.y > 405) {
obstacle_2.x = randomNumber(4, 390);
obstacle_2.y = -2;
}
}
//sprite movement and interactions
function sprite_interactions() {
if (player.isTouching(target_1)) {
score = score + 1;
target_1.x = randomNumber(4, 390);
target_1.y = -2;
target_1.velocityY = randomNumber(1, 5);
}
if (player.isTouching(target_2)) {
score = score + 1;
target_2.x = randomNumber(4, 390);
target_2.y = -2;
target_2.velocityY = randomNumber(1, 5);
}
if (player.isTouching(target_3)) {
score = score + 1;
target_3.x = randomNumber(4, 390);
target_3.y = -2;
target_3.velocityY = randomNumber(1, 5);
}
if (player.isTouching(target_4)) {
score = score + 1;
target_4.x = randomNumber(4, 390);
target_4.y = -2;
target_4.velocityY = randomNumber(1, 5);
}
if (player.isTouching(obstacle_1)) {
lives = lives - 1;
obstacle_1.x = randomNumber(4, 390);
obstacle_1.y = -2;
obstacle_1.velocityY = randomNumber(1, 5);
}
if (player.isTouching(obstacle_2)) {
lives = lives - 1;
obstacle_2.x = randomNumber(4, 390);
obstacle_2.y = -2;
obstacle_2.velocityY = randomNumber(1, 5);
}
}
//score board and lives
function score_lives() {
fill("white");
textFont("cursive");
textSize(20);
text("Score:", 20, 30);
text(score, 90, 31);
text("Lives:", 20, 60);
text(lives, 90, 60);
}
//screen seen if score = 50
function win() {
if (score > 49) {
var win_screen = createSprite(200, 200);
win_screen.setAnimation("win_screen");
win_screen.height = 400;
win_screen.width = 400;
fill(rgb(randomNumber(1, 300), randomNumber(1, 300),randomNumber(1, 300)));
textSize(70);
text("YOU WIN", 30, 190);
}
}
//screen seen if lives = 0
function lose() {
if (lives < 1) {
var end_screen = createSprite(200, 200);
end_screen.setAnimation("end_screen");
end_screen.height = 400;
end_screen.width = 400;
fill(rgb(randomNumber(1, 300), randomNumber(1, 300), randomNumber(1, 300)));
textSize(70);
text("YOU LOSE", 15, 330);
}
}
//screen seen during game
function main() {
main_screen.setAnimation("main_screen");
main_screen.height = 400;
main_screen.width = 400;
}
//screen with instructions
function start() {
start_screen.setAnimation("start_screen");
start_screen.width = 400;
start_screen.height = 400;
fill("white");
textSize(25);
text("Collect:", 180, 30);
text("๐ ", 220, 60);
text("๐", 220, 90);
text("๐", 220, 120);
text("Avoid:", 300, 30);
text("๐ฆด", 330, 60);
text("๐ก๏ธ", 330, 90);
text("<-- player goes left", 100, 160);
text("player goes right -->", 170, 190);
textSize(30);
fill(rgb(randomNumber(1, 300), randomNumber(1, 300), randomNumber(1, 300)));
textFont("cursive");
text("Press space bar ", 20, 270);
text("to begin", 20, 310);
}
iโve moved the inital declaration of screens outside of the loop by using .visible
you can control what sprites are currently showing within your program when drawSprites is called you can also use .destroy
but assuming you wish to bring the user back to the start screen when losing setting the boolean .visible
property to false is your best bet