Skip to content

Commit 22c4a47

Browse files
committed
created virus objects using oops concept
1 parent 8cee920 commit 22c4a47

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

game.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,56 @@ function intit() {
33
pen = canvas.getContext('2d')
44
W = canvas.width = 1000
55
H = canvas.height = 500
6+
cw = 70
7+
//fighter
8+
fighter = new Image()
9+
fighter.src = "./assets/superhero.png"
10+
11+
//virus image
12+
virus_img = new Image()
13+
virus_img.src = "./assets/v1.png"
14+
15+
//virus class
16+
class virusclass {
17+
constructor(x, y, direction, speed) {
18+
this.x = x
19+
this.y = y
20+
this.direction = direction
21+
this.speed = speed
22+
this.update_virus = function () {
23+
if (this.y > H - cw) {
24+
this.direction = "up"
25+
this.speed *= -1
26+
} else if (this.y < 0) {
27+
this.direction = "down"
28+
this.speed *= -1
29+
}
30+
this.y += this.speed
31+
}
32+
33+
}
34+
}
35+
virus1 = new virusclass(W / 3 - 100, 0, "down", 30)
36+
virus2 = new virusclass(W / 2, H - cw, "up", 30)
37+
virus3 = new virusclass(W - 200, 0, "down", 30)
638
}
739

840

941
function draw() {
10-
42+
// console.log("in draw")
43+
// console.log(virus.y)
44+
pen.clearRect(0, 0, W, H)
45+
pen.drawImage(fighter, 20, 200, cw, cw)
46+
pen.drawImage(virus_img, virus1.x, virus1.y, cw, cw)
47+
pen.drawImage(virus_img, virus2.x, virus2.y, cw, cw)
48+
pen.drawImage(virus_img, virus3.x, virus3.y, cw, cw)
1149
}
1250

1351
function update() {
14-
52+
// console.log("in update")
53+
virus1.update_virus()
54+
virus2.update_virus()
55+
virus3.update_virus()
1556
}
1657

1758
function gameloop() {

0 commit comments

Comments
 (0)