0% found this document useful (0 votes)
15 views10 pages

Game Project Mid Term

The document contains a JavaScript code for a simple game setup using a canvas where a character can move left and right, jump, and interact with collectables and canyons. It includes functions for drawing various elements like trees, clouds, mountains, and handling character animations based on user input. The game character's position and state are updated based on keyboard events and interactions with the environment.

Uploaded by

ahkar3980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views10 pages

Game Project Mid Term

The document contains a JavaScript code for a simple game setup using a canvas where a character can move left and right, jump, and interact with collectables and canyons. It includes functions for drawing various elements like trees, clouds, mountains, and handling character animations based on user input. The game character's position and state are updated based on keyboard events and interactions with the environment.

Uploaded by

ahkar3980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

var gameChar_x;

var gameChar_y;
var gameChar_width;
var floorPos_y;

var isLe7;
var isRight;
var isFalling;
var isPlumme=ng;

var collectables;
var canyons;

var trees;
var clouds;
var mountains;

var cameraPosX;

func=on setup()
{
createCanvas(1024, 576);
floorPos_y = height * 3/4;
gameChar_x = width/2;
gameChar_y = floorPos_y;
gameChar_width = 40;

isLe7 = false;
isRight = false;
isFalling = false;
isPlumme=ng = false;

cameraPosX =0;

collectables= setupCollectables();

canyons= setupCaynons();

trees= setupTrees();

clouds= setupClouds();

mountains= setupMountains();

}
func=on draw()
{
cameraPosX = gameChar_x - width/2;

///////////DRAWING CODE//////////

background(235,199,117); //fill the sky blue

noStroke();
fill(150,75,0);
rect(0, floorPos_y, width, height - floorPos_y); //draw some green ground

push();
translate(-cameraPosX, 0);

//draw mountains
drawMountains();

//draw tree
drawTrees();

//draw the canyon


drawCanyons();

//draw clouds
drawClouds();
animateClouds();

//draw the collectable


drawCollectables();

//the game character


if(isLe7 && isFalling)
{
// jumping-le7 code
fill(200,150,100);
ellipse(gameChar_x,
gameChar_y -60, 30,30);

fill(0,0,255);
rect(gameChar_x -13,
gameChar_y -45, 26,20,10,10);

fill(200,150,100);
rect(gameChar_x -5,
gameChar_y -30, 10,15);
rect(gameChar_x -20,
gameChar_y -45, 20,10);
}
else if(isRight && isFalling)
{
// jumping-right code
fill(200,150,100);
ellipse(gameChar_x,
gameChar_y -60, 30,30);

fill(0,0,255);
rect(gameChar_x -13,
gameChar_y -45, 26,20,10,10);

fill(200,150,100);
rect(gameChar_x -5,
gameChar_y -30, 10,15);
rect(gameChar_x,
gameChar_y -45, 20,10);
}
else if(isLe7)
{
// walking le7 code
fill(200,150,100);
ellipse(gameChar_x,
gameChar_y -50, 30,30);

fill(0,0,255);
rect(gameChar_x -13,
gameChar_y -35,26,30,10,10);

fill(200,150,100);
rect(gameChar_x -5,
gameChar_y -10, 10,15);
rect(gameChar_x -20,
gameChar_y -35, 20,10);
}
else if(isRight)
{
// walking right code
fill(200,150,100);
ellipse(gameChar_x,
gameChar_y -50, 30,30);

fill(0,0,255);
rect(gameChar_x -13,
gameChar_y -35, 26,30,10,10);
fill(200,150,100);
rect(gameChar_x -5,
gameChar_y -10, 10,15);
rect(gameChar_x,
gameChar_y -35, 20,10);
}
else if(isFalling || isPlumme=ng)
{
// jumping facing forwards code
fill(200,150,100);
ellipse(gameChar_x,
gameChar_y -60, 30,30);

fill(0,0,255);
rect(gameChar_x -13,
gameChar_y -45, 26,20,10,10);

fill(200,150,100);
rect(gameChar_x -15,
gameChar_y -30, 10,15);
rect(gameChar_x +5,
gameChar_y -30, 10,15);
rect(gameChar_x +10,
gameChar_y -45, 10,10);
rect(gameChar_x -20,
gameChar_y -45, 10,10);
}
else
{
// standing front facing code
fill(200,150,100);
ellipse(gameChar_x,
gameChar_y -50, 30,30);

fill(0,0,255);
rect(gameChar_x -13,
gameChar_y- 35,26,30,10,10);

fill(200,150,100);
rect(gameChar_x -15,
gameChar_y -10, 10,15);
rect(gameChar_x +5,
gameChar_y -10, 10,15);
rect(gameChar_x +10,
gameChar_y -35,10,10);
rect(gameChar_x -20,
gameChar_y -35,10,10);
}

pop();

if(isPlumme=ng){
gameChar_y += 10;
return;
}

if(gameChar_y<floorPos_y){
gameChar_y += 2;
isFalling = true;
}else{
isFalling = false;
}

if(isLe7==true){
gameChar_x-= 5;
}
else if(isRight==true){
gameChar_x += 5;
}

//check interac=ons with collectables and canyons


checkIfGameCharInAnyCollectableRange();
checkIfGameCharIsOverAnyCanyons();
}

//check if character is over any canyon


func=on checkIfGameCharIsOverAnyCanyons(){
for(var i=0;i<canyons.length;i++){
checkIfGameCharisOverCanyon(canyons[i]);
}
}

//check if character is over specific canyon


func=on checkIfGameCharisOverCanyon(t_canyon){
//check if game char is on the floor
var cond1 = gameChar_y == floorPos_y
//check if game char is from the le7 of canyon
var cond2 = gameChar_x-gameChar_width/2>(t_canyon.x_pos)
//check if game char is from the right of canyon
var cond3 = gameChar_x+gameChar_width/2<(t_canyon.x_pos+t_canyon.width)

//check if game character is over the canyon


if(cond1 && cond2 && cond3){
isPlumme=ng=true;
}
}

//draw canyons
func=on drawCanyons(){
for(var i=0;i<canyons.length;i++){
drawCanyon(canyons[i]);
}
}

func=on drawCanyon(t_canyon){
fill(50);
rect(t_canyon.x_pos,
floorPos_y,
t_canyon.width,
height-floorPos_y);
}

//ini=alise canyons
func=on setupCaynons(){
return[{x_pos: 1500, width: 100},
{x_pos: 800, width: 100}];
}

//check if character is near any collectable


func=on checkIfGameCharInAnyCollectableRange(){
for(var i=0;i<collectables.length;i++){
checkIfGameCharInCollectableRange(collectables[i]);
}
}

func=on checkIfGameCharInCollectableRange(t_collectable){
var d = dist(gameChar_x,gameChar_y,t_collectable.x_pos,t_collectable.y_pos)
if(d<30){
t_collectable.isFound=true;
}
}

//draw collectables
func=on drawCollectables(){
for(var i=0;i<collectables.length;i++){
if(collectables[i].isFound==false){
fill(47,128,118);
rectMode(CENTER);
rect(collectables[i].x_pos,
collectables[i].y_pos,
collectables[i].size /2,
collectables[i].size +5);
rectMode(CORNER);
fill(220);
rect(collectables[i].x_pos -10,
collectables[i].y_pos +5,
collectables[i].size /2,
collectables[i].size /2)
}
}
}

//ini=alise collectables
func=on setupCollectables(){
return[
{x_pos: 10, y_pos:floorPos_y-20, size: 40, isFound: false},
{x_pos: 200, y_pos:floorPos_y-20, size: 40, isFound: false},
{x_pos: 1000, y_pos:floorPos_y-20, size: 40, isFound: false}];
}

//draw trees
func=on drawTrees(){
for(var i=0;i<trees.length;i++){
drawTree(trees[i]);
}
}

func=on drawTree(t_tree){
fill(82,63,49);
rectMode(CENTER);
rect(t_tree.x_pos,
t_tree.y_pos, 30,90);
rectMode(CORNER);
fill(60,94,49);
ellipse(t_tree.x_pos +20,
t_tree.y_pos -40, 80,80);
ellipse(t_tree.x_pos -20,
t_tree.y_pos -40, 80,80);
ellipse(t_tree.x_pos,
t_tree.y_pos -80, 80,80);}

//ini=alise trees
func=on setupTrees(){
return[{x_pos: 100, y_pos: height/2 +100},
{x_pos: 500, y_pos: height/2 +100},
{x_pos: 1200, y_pos: height/2 +100}];
}

//animate clouds
func=on animateClouds(){
for(var i=0;i<clouds.length;i++){
var cloud = clouds[i];
cloud.pos_x = cloud.pos_x +1;
if(cloud.pos_x > width +500){
cloud.pos_x = -500;
}
}
}

//draw clouds
func=on drawClouds(){
for(var i=0;i<clouds.length;i++){
var cloud = clouds[i];
drawCloud(cloud);
}
}

func=on drawCloud(t_cloud){
fill(100);
ellipse(t_cloud.pos_x,
t_cloud.pos_y,
t_cloud.size *1.2,
t_cloud.size *1.2);
ellipse(t_cloud.pos_x-40,
t_cloud.pos_y,
t_cloud.size,
t_cloud.size);
ellipse(t_cloud.pos_x+40,
t_cloud.pos_y,
t_cloud.size,
t_cloud.size);
}

//ini=alise clouds
func=on setupClouds(){
var cloudsArrays= [];
for(var i=0;i<8;i++){
var cloud={pos_x:random(-100,width),
pos_y:random(20,100),
size:random(50,80)};
cloudsArrays.push(cloud);
}
return cloudsArrays;
}

//draw mountains
func=on drawMountains(){
for(var i=0;i<mountains.length;i++){
var mountain = mountains[i];
drawMountain(mountain);
}
}

func=on drawMountain(t_mountain){
fill(200);
triangle(t_mountain.pos_x,
t_mountain.pos_y - 176,
t_mountain.pos_x - 50,
t_mountain.pos_y,
t_mountain.pos_x + 50,
t_mountain.pos_y);
triangle(t_mountain.pos_x + 50,
t_mountain.pos_y - 257,
t_mountain.pos_x,
t_mountain.pos_y,
t_mountain.pos_x + 100,
t_mountain.pos_y);
triangle(t_mountain.pos_x + 100,
t_mountain.pos_y - 132,
t_mountain.pos_x + 50,
t_mountain.pos_y,
t_mountain.pos_x + 150,
t_mountain.pos_y);
}

//ini=alise mountains
func=on setupMountains(){
return[{pos_x: 20, pos_y: floorPos_y},
{pos_x: 500, pos_y: floorPos_y},
{pos_x: 1000, pos_y: floorPos_y}];
}

func=on keyPressed()
{
console.log("keyPressed: " + key);
console.log("keyPressed: " + keyCode);

if(keyCode == 37){
console.log("le7 arrow");
isLe7 = true;
}
else if(keyCode == 39){
console.log("right arrow");
isRight = true;
}
else if(keyCode == 38){
if(gameChar_y>=floorPos_y){
console.log("up arrow");
gameChar_y -= 100;
}
}
}

func=on keyReleased()
{
console.log("keyReleased: " + key);
console.log("keyReleased: " + keyCode);

if(keyCode == 37){
console.log("le7 arrow");
isLe7 = false;
}else if(keyCode == 39){
console.log("right arrow");
isRight = false;
}
}

You might also like