-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas.js
executable file
·100 lines (82 loc) · 3 KB
/
canvas.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
/*********************************
* This file creates a canvas and *
* declares some functions which *
* will be used by other files *
**********************************/
let canvas = document.querySelector("canvas");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
let c = canvas.getContext("2d");
let obstacles = []; //to store obstacles like birds and cactus
let firstObstacle = true;
let pseudo_speed = 80; //is a measure of speed
let speed = -findVelocity(findWidth(pseudo_speed));
let noOfGenerations = 1;
let showBestPlayer = false;
let drawIt = true;
let brain_window = {width : findWidth(400), height : findHeight(200), x : innerWidth - findWidth(400)};
let land = new Image();
let land_x = 0;
land.src = "data/land.png";
land.addEventListener("load", function(){
c.drawImage(land, 0, 0);
})
function randint(m, n) { //generates random integers from m to n
//m inclusive and n exclusive
return Math.floor(Math.random() * (n - m) + m);
}
function findHeight(pHeight) { //returns normalized height for different canvas
return innerWidth * pHeight * 0.09 / 88;
}
function findWidth(pWidth) { //returns normalized width for different canvas
return innerWidth * pWidth * 0.09 / 88;
}
function findVelocity(distance){ //returns normalized velocity for different canvas
return Math.floor((-1 + Math.sqrt(1 + 8 * distance))/2);
}
// window.addEventListener("keydown", function (event) { // findHeight(100)*3.5 is the distance of
// if (event.keyCode == 32 || event.keyCode == 38) { // ground from top of the canvas
// if (dino.y + dino.height >= findHeight(100)*3.5) {
// if(dino.gravity==1.4){
// dino.jumping = true;
// dino.velocity = -findVelocity(findHeight(250));}
// }
// } else if (event.keyCode == 40){
// dino.gravity = 14;
// }
// });
// window.addEventListener("mousedown", function () {
// if (dino.y + dino.height == findHeight(100)*3.5) {
// dino.jumping = true;
// dino.velocity = -findVelocity(findHeight(250));
// }
// });
// window.addEventListener("keyup", function(event){
// if(event.keyCode == 40){
// dino.gravity = 1.4;
// }
// });
window.addEventListener("keydown",function(event){
if(event.keyCode == 83){
saveNN();
} else if (event.keyCode == 221){
frameRate++;
} else if(event.keyCode == 219){
frameRate--;
} else if(event.keyCode == 66){
showBestPlayer = !showBestPlayer;
} else if(event.keyCode == 68){
drawIt = !drawIt;
}
});
window.addEventListener("touchstart", function(){
saveNN();
})
function ground() {
if(land_x <= -findWidth(2404)){
land_x = 0;
}
c.drawImage(land, land_x, findHeight(100)*3.5*0.90, findWidth(2404), findHeight(26));
c.drawImage(land, 0, 0, innerWidth, 26, land_x+findWidth(2400), findHeight(100)*3.5*0.90, innerWidth, findHeight(26));
land_x += speed;
}