-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomar.js
255 lines (163 loc) · 4.1 KB
/
omar.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
function loadImages(){
// To load custom images as enemy and omar picture
enemyImage = new Image();
omarImage = new Image();
green_enemy= new Image();
enemyImage.src = "Images/enemy.png";
omarImage.src = "Images/player.png";
green_enemy.src = "Images/green enemy.png";
}
function init(){
// document.getElementById('mycanvas') retrieves the canvas element defined in the html file by using its id.
canvas = document.getElementById('mycanvas');
//canvas.width = window.innerWidth;
//canvas.height = window.innerHeight;
console.log(canvas);
gameover = false;
// pen is an object created using the getContext() function.
pen = canvas.getContext('2d'); // 2d is passed to make 2d games in html
W = canvas.width;
H = canvas.height;
prev_counter = 0;
counter = 0;
loadImages();
// ship is the omar picture we are creating.
ship = {
x : 300,
y : H-50,
w : 50,
h : 50,
speed : 25,
update : function(){
//this.x = this.x + this.speed;
// To test the boundary conditions
//if(this.x >= W-this.w || this.x<=0){
// this.speed *= -1;
//}
},
draw : function(){
// pen.drawImage() is used to load a custom image
pen.drawImage(omarImage,ship.x,ship.y,ship.w,ship.h)
},
};
// Listener for events
function buttonGotPressed(e){
if(e.key=="ArrowUp"){
ship.y = ship.y - 40; //move up
if(ship.y<=0){ //for not going out
ship.y= 0;
}
}
if(e.key=="ArrowDown"){
ship.y = ship.y + 40; //move down
if(ship.y > H-ship.h){ // for not going out
ship.y = H-ship.h;
}
}
if(e.key=="ArrowLeft"){
ship.x = ship.x - 40;
if(ship.x<=0){
ship.x= 0;
}
}
if(e.key=="ArrowRight"){
ship.x = ship.x + 40; //40 represent movement speed
if(ship.x >= W-ship.w){
ship.x = W-ship.w;
}
}
}
document.addEventListener('keydown', buttonGotPressed);
enemies = [];
var e = new enemy(10,20,5,enemyImage);
var d = new enemy(5,20,10,green_enemy);
enemies.push(e);
enemies.push(d);
}
// Class defined for an enemy
function enemy(x,y,speed,draw){
this.x = x;
this.y = y;
this.w = 50;
this.h = 50;
this.state = "active"
this.speed = speed;
this.draw = function(){
pen.drawImage(draw,this.x,this.y,this.w,this.h);
}
this.update = function(){
this.x = this.x + this.speed;
// To test the boundary conditions
if(this.x >= W-this.w || this.x<=0){
this.speed *= -1;
}
this.y++;
if(this.y<=0){
this.state = "inactive"
}
}
}
function draw(){
// In the canvas, towards the right, it is +ve x axis and towards bottom, it is +ve y axis.
//to erase the old screen. Here, we erase the whole screen and redraw it again.
pen.clearRect(0,0,W,H);
//Drawing the ship
ship.draw()
//Drawing the enemy
enemies.forEach(function(enemy){
enemy.draw();
});
}
function update(){
ship.update()
enemies.forEach(function(enemy){
enemy.update();
});
// Math.random() generates a random number between 0 and 1.
var no = Math.random();
if(no<0.01){
var x = Math.floor(Math.random()*(W-10));
var y = Math.floor(Math.random()+1);
var speed = Math.random()*2 -1;
var negative = Math.random();
if(negative<0.5){
speed = -speed;
}
var e = new enemy(x,y,speed,enemyImage);
enemies.push(e);
var d = new enemy(x+5,y+5,speed-2,green_enemy);
enemies.push(d);
}
enemies.forEach(function(enemy){
if(isColliding(ship,enemy)){
alert("game over, click ok to play a new game ");
gameover = true;
}
});
}
function isColliding(r1,r2){
var x_axis = Math.abs(r1.x - r2.x)<= Math.max(r1.w,r2.w) ;
var y_axis = Math.abs(r1.y - r2.y)<= Math.max(r1.h,r2.h);
return x_axis && y_axis;
}
// a function to call update() and draw()
function render(){
draw();
update();
console.log("in render");
counter++;
// similar to setInterval()
if(gameover == false){
// similar to setInterval()
window.requestAnimationFrame(render);
}
else{
startGame();
}
}
function startGame(){
init();
render();
var x = 0 ;
}
startGame();