forked from vershwal/Moving-Square-Game
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.js
More file actions
150 lines (133 loc) · 3.36 KB
/
functions.js
File metadata and controls
150 lines (133 loc) · 3.36 KB
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
var canvas = document.getElementById("myCanvas");
var area = canvas.getContext("2d");
var score = 0;
var finalScore;
var mysquare;
var mywalls = [];
area.fillStyle = "#ffffff";
area.fillRect(0,0,600,400);
area.fillStyle = "#000000";
area.font = "20px Arial";
area.fillText("Score : " + score,450,40);
start();
var flag = 1;
var frameNo = 0;
setInterval(updateArea, 20);
setInterval(updateScore, 200);
function updateScore() {
score += 10;
}
function start() {
mysquare = new component(30, 30, "red", 10, 175);
//mywalls = new component(10, 200, "green", 100, 75);
}
function clear() {
area.clearRect(0, 0, 600, 400);
area.fillStyle = "#ffffff";
area.fillRect(0,0,600,400);
area.fillStyle = "#000000";
area.font = "20px Arial";
area.fillText("Score : " + score,450,40);
}
function stop() {
area.clearRect(0, 0, 600, 400);
area.fillStyle = "red";
area.fillRect(0,0,600,400);
area.fillStyle = "#000000";
area.font = "50px Arial";
area.fillText("GAMEOVER!!",145,160);
area.fillText("Score : " + finalScore,180,230);
}
function component(width, height, color, x, y) {
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.posX = 0;
this.posY = 0;
this.update = function() {
var comp = canvas.getContext("2d");
comp.fillStyle = color;
comp.fillRect(this.x, this.y, this.width, this.height);
}
this.position = function() {
this.x += this.posX;
this.y += this.posY;
var mytop = this.y;
var mybottom = this.y + (this.height);
if(mybottom <= 30) {
this.y = 370;
}
if(mytop >=370) {
this.y = 0;
}
}
this.hit = function(obj2) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = obj2.x;
var otherright = obj2.x + (obj2.width);
var othertop = obj2.y;
var otherbottom = obj2.y + (obj2.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
}
function moveup() {
mysquare.posY -= 1;
}
function movedown() {
mysquare.posY += 1;
}
function moveleft() {
mysquare.posX -= 1;
}
function moveright() {
mysquare.posX += 1;
}
document.onkeydown = function (e) {
var keyCode = e.keyCode;
if(keyCode == 38) {
moveup();
}
if(keyCode == 40) {
movedown();
}
};
function updateArea() {
var x, y;
for (i = 0; i < mywalls.length; i += 1) {
if (mysquare.hit(mywalls[i])) {
if(flag) {
flag = 0;
finalScore = score;
}
stop();
return;
}
}
clear();
frameNo += 1;
if (frameNo == 1 || ((frameNo/150)% 1) == 0) {
x = 600;
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);
mywalls.push(new component(10, height, "green", x, 0));
mywalls.push(new component(10, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < mywalls.length; i += 1) {
mywalls[i].x += -1;
mywalls[i].update();
}
mysquare.position();
mysquare.update();
}