xxxxxxxxxx
374
/*
Midterm
Name: Samyam Lamichhane
Date: October 9, 2022 Sun.
General Rule: Positive Directions = Down and Right
Negative Directions = Up and Left
Features to add:
1. Start and End Screen, Sounds
2. Levels
3. Increasing level of difficulty
*/
let canvas_h = 800;
let canvas_w = 1000;
let background_img;
let start_screen_img;
let gameOver_screen_img;
// Game variables
let score = 0;
let gamePhase = "start";
let isGameOver = false;
// Character variable
let my_char;
let my_char_image;
let my_char_size = 50
let x_dir = 1; // 1 means RIGHT & -1 means LEFT
let y_dir = 1; // 1 means DOWN & -1 means UP
// Horizontal velocity set to 0. Positive means RIGHT, Negative velocity means LEFT
// Vertical velocity set to 0. Positive means DOWN, Negative velocity means UP
let vx = 0;
let vy = 0;
// Supplement character related list and variables
let supp_char_list = [];
let num_supp_char = 4;
let supp_char_size = 40;
// Timer Variable
let timer_len_1 = 30;
// Font variables
let assassin_font;
let corleone_font;
// Preload function
function preload()
{
// Font
assassin_font = loadFont("fonts/assassin.ttf");
corleone_font = loadFont("fonts/corleone.ttf");
// Images
background_img = loadImage("images/0_bg.jpg");
start_screen_img = loadImage("images/start_screen_mafia.jpg");
gameOver_screen_img = loadImage("images/gameOver_img.jpg");
my_char_img = loadImage("images/my_char.png");
img_supp_char_1 = loadImage("images/1_thanos.png");
// Sound
background_sound = loadSound("sounds/the_good_bad_and_the_ugly.mp3");
}
function setup()
{
// Background Sound
background_sound.setVolume(0.1);
background_sound.play();
background_sound.loop();
// background(220);
imageMode(CORNER);
createCanvas(canvas_w, canvas_h);
// Creating user character
my_char = new MyCharacter(canvas_w/2 - my_char_size, canvas_h/2 - my_char_size, my_char_size, my_char_img);
// Supplementary characters
add_supp_char();
}
function add_supp_char()
{
// Creating supplement character(s)
for (let i = 0; i < num_supp_char; i++)
{
// Random positions for supplement characters -- random is adjusted so that each character is located
// inside the canvas
let random_x = random(0, (canvas_w - supp_char_size));
let random_y = random(0, (canvas_h - supp_char_size));
// Storing each character temporarily in a variable; then pushing it to the list
let temp_supp_char = new SuppCharacter(random_x, random_y, supp_char_size, img_supp_char_1);
append(supp_char_list, temp_supp_char);
}
}
function draw()
{
if (isGameOver == false)
{
// Start Screen
if (gamePhase === "start")
{
start_screen();
}
else if (gamePhase === "playing")
{
game_screen();
}
}
else
{
gamePhase = "gameover";
gameOver_screen();
}
}
function start_screen()
{
let button_w = 200;
let button_h = 80;
let button_x = canvas_w/4 -150;
let button_y = canvas_h/4 - 40;
image(start_screen_img, 0, 0, canvas_w, canvas_h);
// // Hover Effect
// if (mouseX < (rect_x + rect_w) && mouseX > (rect_w) && mouseY < (rect_y + rect_h) && mouseY > (rect_y))
// {
// fill("red");
// }
// else
// {
// fill("white");
// }
// stroke(5);
// rect(rect_x, rect_y, rect_w, rect_h, 20);
// textFont(corleone_font);
// stroke(5);
// textSize(50);
// text("Play Again", rect_x + 100, rect_y + 30);
s_button = createButton('Start');
s_button.size(button_w, button_h);
s_button.style("font-size", "25px");
s_button.style("color", "white");
s_button.style("background-color", "black");
s_button.style("border", "thick solid red");
s_button.position(button_x, button_y);
s_button.mousePressed(s_button_pressed);
}
function s_button_pressed()
{
gamePhase = "playing";
}
function game_screen()
{
image(background_img, 0, 0, canvas_w, canvas_h); // Background Image
my_char.display(); // Displaying the primary character
timer();
// Looping through the list and displaying each supplement character
for (let supp_char of supp_char_list)
supp_char.display();
}
function gameOver_screen()
{
// Background Image for Gameover screen
image(gameOver_screen_img, 0, 0, canvas_w, canvas_h);
// GameOver Text
textSize(50);
textFont(assassin_font);
fill("white");
stroke(1);
text("GAME OVER", canvas_w/2, canvas_h/2 - 100);
// Your Score Text
textSize(30);
textFont("calibri");
fill("red");
text("Your Score: " + score, canvas_w/2, canvas_h/2 - 10);
// Play Again Button
let rect_x = canvas_w/2 - 100;
let rect_y = canvas_h/2 + 50;
let rect_w = 200;
let rect_h = 80;
// Hover Effect
if (mouseX < (rect_x + rect_w) && mouseX > (rect_w) && mouseY < (rect_y + rect_h) && mouseY > (rect_y))
{
fill("red");
}
else
{
fill("white");
}
stroke(5);
rect(rect_x, rect_y, rect_w, rect_h, 20);
textFont(corleone_font);
stroke(5);
textSize(50);
text("Play Again", rect_x + 100, rect_y + 30);
if (mouseIsPressed)
{
reset_game();
}
}
function reset_game()
{
timer_len_1 = 30;
supp_char_list = [];
isGameOver = false;
gamePhase = "start";
add_supp_char();
}
function keyPressed()
{
// If RIGHT_ARROW is pressed, the character needs to move right, so set horizontal velocity to 10
if (keyCode === RIGHT_ARROW)
{
vx = 10;
}
// If LEFT_ARROW is pressed, the character needs to move left, so set horizontal velocity to -10
if (keyCode === LEFT_ARROW)
{
vx = -10;
}
// If DOWN_ARROW is pressed, the character needs to move DOWN, so set horizontal velocity to 10
if (keyCode === DOWN_ARROW)
{
vy = 10;
}
// If UP_ARROW is pressed, the character needs to move UP, so set horizontal velocity to -10
if (keyCode === UP_ARROW)
{
vy = -10;
}
}
// Function that helps to move character one step at a time
function keyReleased()
{
// After RIGHT or LEFT arrow is released, set horizontal velocity to 0
if (keyCode === RIGHT_ARROW || keyCode === LEFT_ARROW)
{
vx = 0;
}
// After DOWN or UP arrow is released, set vertical velocity to 0
if (keyCode === DOWN_ARROW || keyCode === UP_ARROW)
{
vy = 0;
}
}