-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
130 lines (105 loc) · 2.41 KB
/
game.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
class wall{
constructor(x){
this.xWall = x;
this.yWall = randY();
}
move(){
this.xWall = this.xWall + dWall;
if(this.xWall + wWall <= 0){
this.xWall = canvas.width;
this.yWall = randY();
}
}
check(){
if(xDoge + sizeDoge < this.xWall || xDoge > this.xWall + wWall){
}
else{
if(this.yWall - hWall < yDoge && yDoge + sizeDoge < this.yWall){
}
else{
gameover();
}
}
}
}
function randY(){
return (200 + Math.floor(Math.random() * (canvas.height - 201)));
}
function init(){
window.canvas = document.getElementById('game');
window.scr = canvas.getContext('2d');
window.imgDoge = new Image();
imgDoge.src = 'doge.jpg';
window.dWall = -4;
window.wWall = 100;
window.hWall = 300;
window.dDoge = 2;
window.yDoge = 200;
window.xDoge = 200;
window.sizeDoge = 100;
imgDoge.onload = function(){
scr.drawImage(imgDoge, xDoge, yDoge, sizeDoge, sizeDoge);
}
window.addEventListener('resize', updateSize);
window.addEventListener('keydown', moveDoge);
updateSize();
window.walls = [new wall(canvas.width), new wall(canvas.width * 1.33), new wall(canvas.width * 1.66)];
play();
}
function updateSize(){
window.canvas.width = document.body.clientWidth;
window.canvas.height = document.body.clientHeight;
}
function play(){
window.playing = setInterval(draw, 10);
}
function gameover(){
clearInterval(playing);
scr.clearRect(0,0,canvas.width,canvas.height);
scr.fillStyle = 'black';
scr.font = '60px consolas';
scr.fillText('GAME OVER!!', canvas.width/3, canvas.height/2);
}
function moveDoge(){
if(dDoge > -11){
dDoge = -8;
}
}
function inCanvas(){
let temp = yDoge + dDoge;
if(temp < 0){
yDoge = 0;
}
else if(temp > canvas.height - sizeDoge){
yDoge = canvas.height - sizeDoge;
}
else{
yDoge = yDoge + dDoge;
}
}
function draw(){
scr.clearRect(0,0,canvas.width,canvas.height);
drawBackground();
drawDoge(200, yDoge);
for(let i=0; i<3; i++){
drawWall(walls[i].xWall, walls[i].yWall, wWall, hWall);
walls[i].move();
walls[i].check();
}
inCanvas();
if(dDoge < 16){
dDoge += 0.3;
}
}
function drawBackground(){
scr.fillStyle = 'rgba(100, 100, 100, 0.7)';
scr.fillRect(0,0,canvas.width,canvas.height);
}
function drawDoge(x, y){
scr.drawImage(imgDoge, x, y, sizeDoge, sizeDoge);
}
function drawWall(x, y, w, h){
scr.fillStyle = 'rgba(0,100,0, 1)';
scr.fillRect(x, 0, wWall, y - hWall);
scr.fillRect(x, y, wWall, canvas.height - y);
}