xxxxxxxxxx
316
/*
Midterm Progress
Name: Samyam Lamichhane
Date: October 2, 2022 Sun.
General Rule: Positive Directions = Down and Right
Negative Directions = Up and Left
Objects can freely move and the primary character can be controlled using arrows
Features to add:
1. Start and End Screen, Sounds
2. Levels
3. Increasing level of difficulty
*/
let canvas_h = 900;
let canvas_w = 900;
let background_img;
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 = 1;
let supp_char_size = 40;
// Preload images
function preload()
{
background_img = loadImage("images/0_bg.jpg");
my_char_img = loadImage("images/my_char.png");
img_supp_char_1 = loadImage("images/1_thanos.png");
}
function setup()
{
// 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);
// 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()
{
image(background_img, 0, 0, canvas_w, canvas_h); // Background Image
my_char.display(); // Displaying the primary character
// Looping through the list and displaying each supplement character
for (let supp_char of supp_char_list)
supp_char.display();
}
// Super class for all the characters in the game
class Character
{
constructor(x, y, size, img_name)
{
this.x = x;
this.y = y;
this.size = size;
this.img = img_name
}
// border_check()
// {
// if (this.x <= 0)
// this.x = 0;
// else if (this.x >= (canvas_w - my_char_size))
// this.x = canvas_w - my_char_size;
// if (this.y <= 0)
// this.y = 0;
// else if (this.y >= (canvas_h - my_char_size))
// this.y = canvas_h - my_char_size;
// }
}
// Primary character class -- inherited from Character Class
class MyCharacter extends Character
{
// Constructor inherited from super class
// 4 additional attributes
constructor(x, y, size, img_name)
{
super(x, y, size, img_name);
this.char_w = this.size;
this.char_h = (this.size + 20);
this.collision = false;
}
// Call update before displaying the image
display()
{
this.update();
image(this.img, this.x, this.y, this.char_w, this.char_h);
}
// Check collision with borders of the canvas
border_check()
{
if (this.x <= 0)
this.x = 0;
else if (this.x >= (canvas_w - this.char_w))
this.x = canvas_w - this.char_w;
if (this.y <= 0)
this.y = 0;
else if (this.y >= (canvas_h - this.char_h)) // Modified here
this.y = canvas_h - this.char_h;
}
// Collision Check Function
up_collision_check(other)
{
// X coordinates verification
if ( (this.x < (other.x + other.size)) && (this.x > other.x))
{
// Y coordinates verification
if ((this.y < (other.y + other.size)) && ((this.y + this.char_h) > other.y))
{
print("Collision");
return true;
}
}
}
// Collision check is called inside the movement after iterating through each objects in the supp_char_list
movement()
{
for (let each_char of supp_char_list)
{
if (this.up_collision_check(each_char))
self.collision = true;
}
}
// Update x and y coordinates and check collision with the border
update()
{
this.x += vx;
this.y += vy;
this.border_check();
this.movement();
}
}
// Side characters class -- inherited from Character Class
class SuppCharacter extends Character
{
// Constructor inherited from super class
// Additional three attributes
constructor(x, y, size, img_name)
{
super(x, y, size, img_name);
this.choices = [-1, 1]; // List which determines the positive or negative direction
// Random horizontal and vertical velocities of characters - could be positive or negative
this.supp_vx = random(this.choices) * (random(0, 10));
this.supp_vy = random(this.choices) * (random(0, 10));
}
// Call update before displaying the image
display()
{
this.update();
image(this.img, this.x, this.y, this.size, this.size);
}
// Update x and y coordinates and check collision with the border
update()
{
this.x += this.supp_vx;
this.y += this.supp_vy;
this.border_check();
}
// Check collision with borders of the canvas - reverse the direction when it collides
border_check()
{
if (this.x <= 0 || this.x >= (canvas_w - this.size))
this.supp_vx = -this.supp_vx;
if (this.y <= 0 || this.y >= (canvas_h - this.size))
this.supp_vy = -this.supp_vy;
}
}
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;
}
}