Game Project Mid Term
Game Project Mid Term
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//////////
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 clouds
drawClouds();
animateClouds();
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;
}
//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}];
}
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;
}
}