-
Notifications
You must be signed in to change notification settings - Fork 0
/
hard.html
172 lines (170 loc) · 5.11 KB
/
hard.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
167
168
169
170
171
172
<!DOCTYPE html>
<html>
<head>
<title>BlocksAway</title>
<link rel="icon" type="image/png" href=images/logo.png />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body onload="startGame()">
<script>
var GamePiece;
var Obstacles = [];
var myScore;
var myBackground;
var mySound;
var myMusic;
var canvas;
var score = 0;
// Game Start Here:
function startGame() {
GamePiece = new component(40, 40, "blue", 10, 160);
myBackground = new component(1355, 440, "images/index3.jpg", 0, 0, "image");
myScore = new component("30px", "Consolas", "black", 30, 40, "text");
mySound = new sound("crash.mp3");
myMusic = new sound("bgsound.mp3");
myMusic.play();
GameArea.start();
}
// My Game Area:
var GameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = window.innerWidth;
this.canvas.height = 379;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
window.addEventListener('keydown', function (e) {
GameArea.key = e.keyCode;
})
window.addEventListener('keyup', function (e) {
GameArea.key = false;
})
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
stop : function() {
clearInterval(this.interval);
}
}
// Creating an Object:
function component(width, height, color, x, y, type) {
this.type = type;
if (type == "image") {
this.image = new Image();
this.image.src = color;
}
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.update = function() {
ctx = GameArea.context;
if (this.type == "text") {// creating score here
ctx.font = this.width + " " + this.height;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
}else if (type == "image") {// background image
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
}else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.x += this.speedX;
this.y += this.speedY;
}
// Controlling the Gamepiece:
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;
}
}
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < Obstacles.length; i += 1) {
// When Gamepiece hit the Obstacles:
if (GamePiece.crashWith(Obstacles[i])) {
mySound.play();
alert("CRASH");
GameArea.stop();
return;
}
}
GameArea.clear();
myBackground.newPos();
myBackground.update();
GameArea.frameNo += 1;
// Creating Random Obstacle:
if (GameArea.frameNo == 1 || everyinterval(70)) {
x = GameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
Obstacles.push(new component(40, height, "red", x, 0));
Obstacles.push(new component(40, x - height - gap, "red", x, height + gap));
score++;
}
// Speed of the Obstacles:
for (i = 0; i < Obstacles.length; i += 1) {
Obstacles[i].x += -6;
Obstacles[i].update();
}
myScore.text="Time Score:" + score;
myScore.update();
GamePiece.speedX = 0;
GamePiece.speedY = 0;
// Speed of the Gamepiece:
if (GameArea.key && GameArea.key == 37) {GamePiece.speedX = -2; }
if (GameArea.key && GameArea.key == 39) {GamePiece.speedX = 2; }
if (GameArea.key && GameArea.key == 38) {GamePiece.speedY = -2; }
if (GameArea.key && GameArea.key == 40) {GamePiece.speedY = 2; }
GamePiece.newPos();
GamePiece.update();
}
// Creating sounds effects:
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
}
// Interval of Obstacles here:
function everyinterval(n) {
if ((GameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
</script>
</body>
</html>