prepared for UI changes and added reset function

This commit is contained in:
Mees Roelofsz
2024-01-06 15:56:44 +01:00
parent c75a16e367
commit 2c928fe1fa

View File

@@ -57,7 +57,9 @@ let chosen = false;
let finalPhase = false; let finalPhase = false;
let nextWave = []; let nextWave = [];
let iIndex = 0; let iIndex = 0;
let buttonSelect = 0; let buttonSelectDead = 0;
let buttonSelectHome = 0;
let buttonSelectPause = 0;
let entered = false; let entered = false;
let submitted = false; let submitted = false;
@@ -83,11 +85,29 @@ function score() {
time += 3 / framerate; time += 3 / framerate;
} }
function reset() {
lives = 1;
time = 0;
bounceX = bossPosX;
bounceY = bossPosY;
initialPlayerPosX = playerPosX;
initialPlayerPosY = playerPosY;
hasMoved = false;
bullets = [];
shot = false;
chosen = false;
finalPhase = false;
isDead = false;
entered = false;
buttonSelectDead = 0;
submitted = false;
}
function gameOver() { function gameOver() {
if (lives == 0) { if (lives == 0) {
//game over screen //game over screen
iIndex = constrain(iIndex, 0, 2); iIndex = constrain(iIndex, 0, 2);
buttonSelect = constrain(buttonSelect, 0, 2); buttonSelectDead = constrain(buttonSelectDead, 0, 2);
isDead = true; isDead = true;
push(); push();
fill(255, 0, 0); fill(255, 0, 0);
@@ -163,14 +183,14 @@ async function keyPressed() {
} }
if (isDead && keyReleasedFlag && entered) { if (isDead && keyReleasedFlag && entered) {
if (keyCode == UP_ARROW) { if (keyCode == UP_ARROW) {
buttonSelect -= 1; buttonSelectDead -= 1;
keyReleasedFlag = false; keyReleasedFlag = false;
} }
if (keyCode == DOWN_ARROW) { if (keyCode == DOWN_ARROW) {
buttonSelect += 1; buttonSelectDead += 1;
keyReleasedFlag = false; keyReleasedFlag = false;
} }
if (buttonSelect == 0) { if (buttonSelectDead == 0) {
if (keyCode == RIGHT_ARROW && !submitted) { if (keyCode == RIGHT_ARROW && !submitted) {
entered = false; entered = false;
nameHS = ''; nameHS = '';
@@ -181,45 +201,16 @@ async function keyPressed() {
keyReleasedFlag = false; keyReleasedFlag = false;
} }
} }
if (buttonSelect == 1) { if (buttonSelectDead == 1) {
if (keyCode == ENTER) { if (keyCode == ENTER) {
//reset all the variables so the game can be played again //reset all the variables so the game can be played again
lives = 1; reset();
time = 0;
bounceX = bossPosX;
bounceY = bossPosY;
initialPlayerPosX = playerPosX;
initialPlayerPosY = playerPosY;
hasMoved = false;
bullets = [];
shot = false;
chosen = false;
finalPhase = false;
isDead = false;
entered = false;
buttonSelect = 0;
submitted = false;
} }
} }
if (buttonSelect == 2) { if (buttonSelectDead == 2) {
if (keyCode == ENTER) { if (keyCode == ENTER) {
homescreenOn = true; homescreenOn = true;
reset();
lives = 1;
time = 0;
bounceX = bossPosX;
bounceY = bossPosY;
initialPlayerPosX = playerPosX;
initialPlayerPosY = playerPosY;
hasMoved = false;
bullets = [];
shot = false;
chosen = false;
finalPhase = false;
isDead = false;
entered = false;
buttonSelect = 0;
submitted = false;
} }
} }
} }
@@ -281,7 +272,7 @@ function draw() {
} }
if (isDead == true) { if (isDead == true) {
if (entered == true) { if (entered == true) {
if (buttonSelect == 0) { if (buttonSelectDead == 0) {
push() push()
stroke(205, 205, 205) stroke(205, 205, 205)
strokeWeight(2) strokeWeight(2)
@@ -289,7 +280,7 @@ function draw() {
rect(width / 2 - 45, height / 2 - 30, 90, 40) rect(width / 2 - 45, height / 2 - 30, 90, 40)
pop() pop()
} }
if (buttonSelect == 1) { if (buttonSelectDead == 1) {
push() push()
stroke(205, 205, 205) stroke(205, 205, 205)
strokeWeight(2) strokeWeight(2)
@@ -297,7 +288,7 @@ function draw() {
rect(width / 2 - 55, height / 2 + 15, 110, 40) rect(width / 2 - 55, height / 2 + 15, 110, 40)
pop() pop()
} }
if (buttonSelect == 2) { if (buttonSelectDead == 2) {
push() push()
stroke(205, 205, 205) stroke(205, 205, 205)
strokeWeight(2) strokeWeight(2)
@@ -558,11 +549,11 @@ function spawnRandomBullet() {
function homescreen() { function homescreen() {
textAlign(CENTER); textAlign(CENTER);
button(0, 0, 0, width / 2 - 90, height / 2, 200, 40, "Start Game") button(255, 255, 255, width / 2 - 90, height / 2, 200, 40, "Start Game")
button(0, 0, 0, width / 2 - 90, height / 2 + 50, 200, 40, "Scores") button(255, 255, 255, width / 2 - 90, height / 2 + 50, 200, 40, "Scores")
push() push()
stroke(255, 0, 0) stroke(255, 255, 255)
strokeWeight(5) strokeWeight(5)
fill(0, 255, 0, 0) fill(0, 255, 0, 0)
rect(100, 150, 300, 400) rect(100, 150, 300, 400)
@@ -570,12 +561,12 @@ function homescreen() {
push() push()
textSize(50) textSize(50)
fill(255, 0, 0) fill(255, 255, 255)
text("Highscores", 250, 200) text("Highscores", 250, 200)
pop() pop()
push() push()
stroke(255, 0, 0) stroke(255, 255, 255)
strokeWeight(5) strokeWeight(5)
fill(0, 255, 0, 0) fill(0, 255, 0, 0)
rect(860, 150, 300, 400) rect(860, 150, 300, 400)
@@ -583,7 +574,7 @@ function homescreen() {
push() push()
textSize(50) textSize(50)
fill(255, 0, 0) fill(255, 255, 255)
text("Controls", 1000, 200) text("Controls", 1000, 200)
pop() pop()
} }
@@ -592,8 +583,8 @@ function homescreen() {
function button(r, g, b, buttonX, buttonY, buttonWidth, buttonHeight, buttonText) { function button(r, g, b, buttonX, buttonY, buttonWidth, buttonHeight, buttonText) {
push() push()
fill(255, 0, 0) fill(0, 0, 50)
rect(buttonX, buttonY, buttonWidth, buttonHeight, 20, 20, 20, 20) rect(buttonX, buttonY, buttonWidth, buttonHeight)
textSize(25) textSize(25)
fill(r, g, b) fill(r, g, b)
text(buttonText, buttonX + 100, buttonY + 30) text(buttonText, buttonX + 100, buttonY + 30)