-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
166 lines (156 loc) · 3.63 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<html>
<head>
<style>
canvas{
background-image:url("dinobackground.png");
animation: animatedBackground 4s linear infinite;
}
@keyframes animatedBackground {
from {
background-position: 0 0;
}
to {
background-position: -600px 0px;
}
}
</style>
</head>
<body onload="startGame()">
<script type="text/javascript">
gap=randGap();
var myObstacles=[];
var enemytypes=["cactus","cactuss","flyingdinosaur"];
var gamescore=0;
//inserting images
var standimage=new Image();
standimage.src="dinosaur.png";
var cactus=new Image();
cactus.src="cactus.png";
var cactus2image=new Image();
cactus2image.src="cactus2.png";
var flyingdinosaur=new Image();
flyingdinosaur.src="flyingdinosaur.PNG";
function startGame(){
gamearea.start();
}
function everyinterval(n){
if(gamearea.frame%n==0) return true;
return false;
}
function jump(){
player.speedY=-2;
}
function randGap(){
return Math.floor(200+Math.random()*(500-200+1));
}
var scoreText={
x:220,
y:200,
update:function(text){
gamearea.context.fillStyle="gray";
gamearea.context.font="30px Consolas";
gamearea.context.fillText(text,220,200);
}
}
var player={
x:20,
y:470,
speedY:0,
update:function(){
if(this.y<470)
{
gamearea.context.drawImage(standimage,this.x,this.y-40,60,60);
}
else
{
gamearea.context.drawImage(standimage,this.x,this.y-40,50,60);
}
},
newPos:function(){
if(this.y<280){
this.speedY=2;
}
this.y=this.y+this.speedY;
if(this.speedY==2 && this.y==470){
this.speedY=0;
}
},
crashWith:function(obs){
if(this.x+30>obs.x && this.x<obs.x+obs.width && this.y+30>obs.y){
return true;
}
return false;
}
}
function obstacle(){
this.height=Math.floor(20+Math.random()*(50-20+1));
this.width=Math.floor(10+Math.random()*(20-10+1));
this.x=1000;
this.y=gamearea.canvas.height-this.height;
this.index=Math.floor(Math.random()*enemytypes.length);
this.enemytype=enemytypes[this.index];
this.draw=function(){
if(this.enemytype=="cactus" )
{
gamearea.context.drawImage(cactus,this.x,this.y-23,40,this.height+5);
}
else if(this.enemytype=="cactuss")
{
gamearea.context.drawImage(cactus2image,this.x,this.y-23,40,this.height+5);
}
else
{
gamearea.context.drawImage(flyingdinosaur,this.x,this.y-55,60,this.height+10);
}
}
}
var gamearea={
canvas:document.createElement("canvas"),
start:function(){
this.canvas.height=500;
this.canvas.width=600;
document.body.insertBefore(this.canvas,document.body.childNodes[0]);
this.context=this.canvas.getContext("2d");
this.frame=0;
this.score=0;
scoreText.update("Score: 0");
this.interval=setInterval(this.updateGameArea,5);
window.addEventListener("keydown",jump);
},
updateGameArea:function(){
for(i=0;i<myObstacles.length;i++){
if(player.crashWith(myObstacles[i])){
gamearea.stop();
return;
}
}
gamearea.clear();
if(everyinterval(gap)){
myObstacles.push(new obstacle());
gap=randGap();
gamearea.frame=0;
}
for(i=0;i<myObstacles.length;i++){
myObstacles[i].x-=2;
myObstacles[i].draw();
}
player.newPos();
player.update();
gamearea.frame+=1;
gamearea.score+=0.01;
gamescore=Math.floor(gamearea.score);
scoreText.update("Score: "+gamescore);
},
clear:function(){
gamearea.context.clearRect(0,0,this.canvas.width,this.canvas.width);
},
stop:function(){
clearInterval(this.interval);
gamearea.context.fillStyle="gray";
gamearea.context.font="25px Consolas";
gamearea.context.fillText("GAME OVER!!!",200,250);
},
}
</script>
</body>
</html>