-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
141 lines (99 loc) · 2.88 KB
/
code.js
File metadata and controls
141 lines (99 loc) · 2.88 KB
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
131
132
133
134
135
136
137
138
139
140
141
const canvas = document.querySelector('canvas');
const c = canvas.getContext('2d');
const gameOver = document.querySelector('h1');
const altitude = document.querySelector('h3');
const title = document.getElementById('titleScreen');
const retryBtn = document.getElementById('retry');
canvas.width = innerWidth;
canvas.height = innerHeight + 100;
const player = new Player({
position : {
x : (canvas.width / 2) - 50,
y : (canvas.height / 2) - 50,
},
resize : 0.25,
imageSrc : 'Hot Air balloon.png',
});
let enmeyArray = [];
const keys = {
a : false,
d : false
}
const enemyImages = [
'Droplet.png',
'Cloud1.png', 'Cloud2.png',
'Cloud3.png', 'Cloud4.png',
'Cloud5.png', 'Cloud6.png',
'Cloud7.png', 'Cloud8.png',
];
let time = 0;
let height = Math.floor(Math.random() * 500 + 100);
function gameLoop() {
let runner = requestAnimationFrame(gameLoop);
time++
c.clearRect(0, 0, canvas.width, canvas.height);
player.update()
height += -Math.floor(player.velcoity.y)
player.velcoity.x = 0;
altitude.style.display = 'block';
altitude.textContent = height.toString().padStart(4, 0);
if(keys.a) {
player.velcoity.x = -1;
} else if(keys.d) {
player.velcoity.x = 1;
}
if(time % 22 === 0) {
enmeyArray.push(new Enemy({
position : {
x : Math.random() * (canvas.width + 1000) + -1000,
y : -40,
},
resize : 2,
imageSrc : enemyImages[Math.round(Math.random() * (enemyImages.length - 1))],
}))
}
enmeyArray.forEach(enmey => {
enmey.update()
enmey.velcoity.x = -player.velcoity.x;
enmey.velcoity.y += -player.velcoity.y
if(enmey.position.x + enmey.width > player.position.x &&
enmey.position.x < player.position.x + player.width &&
enmey.position.y + enmey.height > player.position.y - 1 &&
enmey.position.y < player.position.y + player.height) {
gameOver.style.display = 'block';
retryBtn.style.display = 'block';
cancelAnimationFrame(runner);
}
})
};
function startGame() {
title.style.display = 'none';
gameLoop();
}
function retry() {
location.reload();
}
addEventListener('keydown', e => {
switch(e.code) {
case 'KeyW': player.velcoity.y = -0.5;
break;
case 'KeyS': player.velcoity.y = 0.9
break;
case 'KeyA': keys.a = true;
break;
case 'KeyD': keys.d = true;
break;
}
})
addEventListener('keyup', e => {
switch(e.code) {
case 'KeyA': keys.a = false;
break;
case 'KeyD': keys.d = false;
break;
}
})
addEventListener('resize', () => {
canvas.width = innerWidth;
canvas.height = innerHeight + 100;
})