xxxxxxxxxx
258
let startmenu_display_picture;
let back_button;
let health_changed = false;
let number_of_eggs = 15;
//preload all the functions to ensure smooth gaming experience
function preload()
{
startmenu_display_picture = loadImage("crack_start_menu.png"); //the background that is shown at the start with 600x300 size
back_button = loadImage("back_button.png"); //placeholder back_button
}
function setup() {
createCanvas(300, 600);
randomSeed(Date.now());
}
function draw() {
background(0,0,0);
// if (health_changed == false)
// {
// for (let i = 0; i < number_of_eggs; i++)
// {
// game_object.egg_array[i].egg_health = random(0,5);
// }
// health_changed = true;
// }
game_object.screenDisplay(); //the game class object displays the menu and allows for interactivity
}
//game class will contain everything from user interactivity to the score keeping and life lost etc. Will (probably) be the super class to the egg class
class Game
{
constructor()
{
//canvas dimensions
this.canvas_width = 300;
this.canvas_height = 600;
//start button on menu - coordinates
this.start_button_x = this.canvas_width/6;
this.start_button_y = 4.5 * (this.canvas_height/6);
//instruction button on menu - coordinates
this.instruction_button_x = this.canvas_width/6;
this.instruction_button_y = 5* (this.canvas_height/6);
//dimensions for the start and instruction buttons to be used for mouseClicked interactivity
this.button_width = 2*300/3;
this.button_height = 30;
this.button_arc = 20;
this.score = 0; //will keep the score of all the good eggs that have successfully passed
this.player_lives = 3; //will keep track of the lives for the player and will be displayes as well
this.egg_width = 75;
this.egg_height = 100;
this.side_difference = 12.5;
this.up_difference = 25;
this.num_eggs = 15;
this.egg_array = []; //this array will contains objects of egg class using new Egg() and the .push method
for (let i = 0; i < this.num_eggs; i++)
{
this.egg_array[i] = new Egg(this.canvas_width - this.side_difference - this.egg_width, this.canvas_height + i*(this.egg_height + this.up_difference), this.egg_width, this.egg_height, i, this.up_difference)
}
//controls what is shown to the user at a given time
this.menu_screen = true;
this.start_screen = false;
this.instruction_screen = false;
//will be removed for the start window but will stay for instructions window
this.back_button_x = 10;
this.back_button_y = 10;
this.back_button_width = 75;
this.back_button_height = 50;
}
belt_1_movement()
{
// this.loop_check();
for (let i = 0; i < this.num_eggs; i++)
{
this.egg_array[i].movement();
if (this.egg_array[i].egg_y + this.egg_array[i].egg_height <= 0)
{
this.egg_array[i].egg_y = this.egg_array[this.num_eggs - 1].egg_y + ((i+1) * (this.egg_height + this.up_difference));
}
this.egg_array[i].display();
}
}
//displays different types of screens to the user
screenDisplay()
{
if (this.menu_screen == true)
{
//image(startmenu_display_picture, 0,0);
fill("#541675");
noStroke();
//boxes for the start and instruction buttons
rect(this.start_button_x, this.start_button_y, this.button_width, this.button_height, this.button_arc);
rect(this.instruction_button_x, this.instruction_button_y, this.button_width, this.button_height, this.button_arc);
//if the mouse hovers over the start button
if (mouseX >= this.start_button_x && mouseX <= this.start_button_x + this.button_width && mouseY >= this.start_button_y && mouseY <= this.start_button_y + this.button_height)
{
fill("green"); //turn the text green
}
else
{
fill("gold"); //otherwise the text will be gold
}
//write START in the button
textSize(20);
text("START", this.start_button_x + ((this.button_width)/3), this.start_button_y + (this.button_height/1.3));
//mouse hovering feature for the instruction button
if (mouseX >= this.instruction_button_x && mouseX <= this.instruction_button_x + this.button_width && mouseY >= this.instruction_button_y && mouseY <= this.instruction_button_y + this.button_height)
{
fill("green");
}
else
{
fill("gold");
}
text("INSTRUCTIONS", this.instruction_button_x + ((this.button_width)/8), this.instruction_button_y + (this.button_height/1.3));
}
//if the start button is clicked
else if (this.start_screen == true)
{
background(0,0,0);
this.belt_1_movement();
}
//if the instruction button is clicked then this if statement would run to show the instruction screen
else if (this.instruction_screen == true)
{
//changes background for now but is basically a placeholder for an image with all the instructions
background("gold");
textSize(18);
fill(255, 255, 255);
text("UNDER CONSTRUCTION", this.canvas_width/6.5, this.canvas_height/2);
//placeholder for back button. Image is resized to fit the desired dimensions. the picture will change for a better/formal picture of the back button
back_button.resize(this.back_button_width, this.back_button_height);
image(back_button, this.back_button_x, this.back_button_y);
}
}
}
class Egg
{
constructor(x, y, e_width, e_height, e_index, e_up)
{
this.egg_x = x;
this.egg_y = y;
this.egg_health = 2;
this.speed = 3;
this.egg_height = e_height;
this.egg_width = e_width;
this.move_allowed = true;
this.index_in_array = e_index;
this.up_distance = e_up;
}
movement(block, eggs)
{
this.egg_y = this.egg_y - this.speed;
}
display()
{
if (this.egg_health >= 3)
{
fill(255, 0, 0);
rect(this.egg_x, this.egg_y, this.egg_width, this.egg_height);
text(this.index_in_array, this.egg_x, this.egg_y);
}
else if (this.egg_health == 2)
{
fill(0,255,0);
rect(this.egg_x, this.egg_y, this.egg_width, this.egg_height);
text(this.index_in_array, this.egg_x, this.egg_y);
}
else if (this.egg_health == 1)
{
fill(0, 0, 255);
rect(this.egg_x, this.egg_y, this.egg_width, this.egg_height);
text(this.index_in_array, this.egg_x, this.egg_y);
}
else if (this.egg_health <= 0)
{
fill(225, 225, 225);
rect(this.egg_x, this.egg_y, this.egg_width, this.egg_height);
text(this.index_in_array, this.egg_x, this.egg_y);
}
}
egg_clicked()
{
if (this.egg_health >= 3)
{
this.egg_health = 0;
}
else
{
this.egg_health = this.egg_health - 1;
}
}
}
//creates an object of the game class
let game_object;
game_object = new Game();
function mouseClicked()
{
//if the start button is clicked. Ensure that the button is only clickable when menu screen is displayed to the user
if (mouseX >= game_object.start_button_x && mouseX <= game_object.start_button_x + game_object.button_width && mouseY >= game_object.start_button_y && mouseY <= game_object.start_button_y + game_object.button_height && game_object.menu_screen == true)
{
game_object.menu_screen = false;
game_object.start_screen = true; //turns this true and others false so that only start screen is shown
game_object.instruction_screen = false;
}
//if the instruction button is clicked. Ensure that the button is only clickable when menu screen is displayed to the user
else if (mouseX >= game_object.instruction_button_x && mouseX <= game_object.instruction_button_x + game_object.button_width && mouseY >= game_object.instruction_button_y && mouseY <= game_object.instruction_button_y + game_object.button_height && game_object.menu_screen == true)
{
game_object.menu_screen = false;
game_object.start_screen = false;
game_object.instruction_screen = true;
}
//back button is clicked but can only be clicked when it is displayed in either start or instructions screen
else if (mouseX >= game_object.back_button_x && mouseX <= game_object.back_button_x + game_object.back_button_width && mouseY >= game_object.back_button_y && mouseY <= game_object.back_button_y + game_object.back_button_height && (game_object.instruction_screen == true || game_object.start_screen === true))
{
game_object.menu_screen = true;
game_object.start_screen = false;
game_object.instruction_screen = false;
}
if (game_object.start_screen == true)
{
for (let i = 0; i < game_object.num_eggs; i++)
{
if (mouseX >= game_object.egg_array[i].egg_x && mouseX <= game_object.egg_array[i].egg_x + game_object.egg_width && mouseY >= game_object.egg_array[i].egg_y && mouseY <= game_object.egg_array[i].egg_y + game_object.egg_height)
{
game_object.egg_array[i].egg_clicked();
}
}
}
}