<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
body {background-color: #88aa78;}
#up_down1 {
width:80%
height:80%;
}
#the_heart {
border:3px solid #fff733;
border-radius:25px;
font-size:20px;
margin-right:150px;
margin-left:150px;
padding-top:3px;
padding-bottom:3px;
padding-left:30px;
background-color:rgba(250,130,30,1);
}
</style>
</head>
<body onload="startGame()">
<canvas id="myCanvas" width="400" height="250" style="border:1px solid grey"></canvas>
<img id="img" src="../images/plane.png" alt="plane" width="30" height="10">
<script type='text/javascript'>
var hero;
var myObstacles = [];
let heart=1000;
var frameNo=0;
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = 270;
var FPS = 50;
setInterval(function(){
update();
}, 1000/FPS);
function startGame() {
hero = new hero(73, 25 ,"img1", 10,200);
}
function component(width, height, color, x, y) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.update = function() {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
} // component
// ---------- hero class --------------
function hero(width, height, img, x, y) {
this.img=img;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.elevation = 0;
this.update = function() {
ctx.drawImage( document.getElementById("img") ,this.x,this.y,this.width,this.height);
}
this.newPos = function() {
this.y += this.elevation;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = canvas.height - this.height-35;
if (this.y > rockbottom) {
this.y = rockbottom;
this.elevation=0;
}
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
} // hero class
// ========================================
// ------- update function --------------
function update()
{
for (i = 0; i < myObstacles.length; i += 1) {
if (hero.crashWith(myObstacles[i])) {
heart--;
if (heart<0)
return;
}// if
} // for
frameNo += 1;
ctx.fillStyle = "#ccaaaa"; // rang kardane safeh
ctx.fillRect( 0, 0, canvas.width, canvas.height);
if (frameNo == 1 || everyinterval(150)) {
x = canvas.width;
myObstacles.push(new component(40, 40, "red", x, 80));
} // if frameno 150
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].update();
}// for loop for move objects
hero.newPos();
hero.update();
document.getElementById("the_heart").innerHTML="heart= " + JSON.stringify(heart);
text="SCORE: " + frameNo;
ctx.font = "20px" + " "+"Consolas" ;
ctx.fillStyle = "black";
ctx.fillText(text, 240, 30);
}// uodate function
function everyinterval(n) {
if ((frameNo / n) % 1 == 0) {return true;}
return false;
}
function accelerate(n) {
hero.elevation = n;
} //accelerate
</script>
<center>
<table><tr><td>
<button type="button" id="up_down1" style=" font-size:20px; " onclick="accelerate(-1)">up</button>
<button type="button" id="middle" style=" font-size:12px; " onclick="accelerate(0)">stop</button>
<button type="button" id="up_down2" style=" font-size:20px; " onclick="accelerate(1)">down</button>
</td></tr><tr><td>
</td></tr>
</table>
</center>
<script>
document.getElementById('up_down1').style.width = window.innerWidth/ 3;
document.getElementById('up_down1').style.height = window.innerHeight/ 8;
document.getElementById('up_down2').style.width = window.innerWidth/ 3;
document.getElementById('up_down2').style.height = window.innerHeight/ 8;
document.getElementById('middle').style.width = window.innerWidth/ 5;
document.getElementById('middle').style.height = window.innerHeight/ 10;
</script>
<p id="the_heart"></p>
<p>Use the up and down button to stay in the air</p>
<p>Good luck!</p>
</body>
</html>