-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
84 lines (74 loc) · 1.68 KB
/
script.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
var canvas = document.getElementById('canvas').getContext("2d");
var bg = new Bg(0,0,500,900, "assets/bg.png")
var flower = new Flower(0,0,50,50, "assets/flower1.png")
var bg2 = new Bg(0,-900,500,900, "assets/bg.png")
var bee = new Bee(200,800,100,80, "assets/bee1.png");
var spider = new Spider(100,100,50,50, "assets/spider1.png");
var text_points = new Text();
var text_lifes = new Text();
var gameovertxt = new Text();
var play = true;
document.addEventListener("keydown", function(event){
if (event.key === "a"){
bee.dir = -4;
}
if (event.key === "d"){
bee.dir = 4;
}
});
document.addEventListener("keyup", function(event){
if (event.key === "a"){
bee.dir = 0;
}
if (event.key === "d"){
bee.dir = 0;
}
});
function collides() {
if (bee.collide(spider)){
spider.respaw();
bee.lifes -= 1;
}
if (bee.collide(flower)){
flower.respaw();
bee.points += 1;
}
}
function gameover(){
if(bee.lifes <= 0){
play = false;
}
}
function draw() {
bg.draw();
bg2.draw();
if(play){
bee.draw();
spider.draw();
flower.draw();
text_points.draw(bee.points, 450, 50, "white");
text_lifes.draw(bee.lifes, 30, 50, "red");
}else{
gameovertxt.draw("GAME OVER", 120, 450, "black");
}
}
function update() {
bg.move(3, 900, 0);
bg2.move(3, 0, -900);
if(play){
bee.move();
bee.animation("bee", 4);
spider.move();
spider.animation("spider", 4);
flower.move();
flower.animation("flower", 2);
collides();
gameover();
}
}
function main() {
canvas.clearRect(0,0,500,900);
update();
draw();
}
setInterval(main, 10);