-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
92 lines (72 loc) · 1.52 KB
/
sketch.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
let dino;
let dImg;
let cImg;
let bImg;
let cacti = [];
let graceModeActive = false;
let life = 5;
let lifeCounter;
function preload() {
dImg = loadImage('dino.png');
cImg = loadImage('cactus.png');
bImg = loadImage('desert.jpg');
}
function setup() {
createCanvas(windowWidth, windowHeight);
x = random(windowWidth);
y = random(windowHeight)
dino = new Dino();
lifeCounter = createDiv('Life = ' + life);
lifeCounter.position(20,20);
lifeCounter.id = 'life';
lifeCounter.style('color','purple');
}
function keyPressed() {
if (key == ' ') {
dino.jump();
}
}
this.hits = function(dino){
if (graceModeActive){
return;
}
if (dino.hits(c)){
this.highlight = true;
life--;
graceModeActive = true;
setTimeout(function(){
graceModeActive = false;
},2000);
return true;
}
this.highlight = false;
return false;
}
function draw() {
if (random(1) < 0.005) {
cacti.push(new Cactus());
}
background(bImg);
for (let c of cacti){
c.move();
c.show();
if (dino.hits(c)&&!c.hit) {
c.hit=true;
--life;
lifeCounter.elt.innerHTML = `Life = ${life}`;
console.log('Game Over!');
}
//noLoop();
dino.show();
dino.move();
//noLoop();
//{
//background(220);
//fill(255, 0, 255);
//noStroke();
//textAlign(LEFT);
//ellipse(x, y, radius*2, radius*2);
//text("Life: " + life, 20, 20);
//}
}
}