-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgame.html
192 lines (154 loc) · 5.11 KB
/
game.html
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Raumschiffschlacht</title>
<style>
canvas {
background-color: rgba(0, 0, 0, 0.8);
}
</style>
<script>
let KEY_SPACE = false; // 32
let KEY_UP = false; // 38
let KEY_DOWN = false; // 40
let canvas;
let ctx;
let backgroundImage = new Image();
let rocket = {
x: 50,
y: 200,
width: 100,
height: 50,
src: 'img/rocket.png'
};
let ufos = [];
let shots = [];
document.onkeydown = function(e) {
if (e.keyCode == 32) { // Leertaste gedrückt
KEY_SPACE = true;
}
if (e.keyCode == 38) { // Nach oben gedrückt
KEY_UP = true;
}
if (e.keyCode == 40) { // Nach unten gedrückt
KEY_DOWN = true;
}
}
document.onkeyup = function(e) {
if (e.keyCode == 32) { // Leertaste losgelassen
KEY_SPACE = false;
}
if (e.keyCode == 38) { // Nach oben losgelassen
KEY_UP = false;
}
if (e.keyCode == 40) { // Nach unten losgelassen
KEY_DOWN = false;
}
}
function startGame() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
loadImages();
setInterval(update, 1000 / 25);
setInterval(createUfos, 5000);
setInterval(checkForCollion, 1000 / 25);
setInterval(checkForShoot, 1000 / 10); // Deine Hausaufgabe
draw();
}
function checkForCollion() {
ufos.forEach(function(ufo) {
// Kontrollieren, ob UFO mit Rakete kollidiert
if (rocket.x + rocket.width > ufo.x &&
rocket.y + rocket.height > ufo.y &&
rocket.x < ufo.x &&
rocket.y < ufo.y + ufo.height
) {
rocket.img.src = 'img/boom.png';
console.log('Collion!!!');
ufos = ufos.filter(u => u != ufo);
}
// Deine Hausaufgabe
shots.forEach(function(shot) {
// Kontrollieren, ob Laser mit Rakete kollidiert
if (shot.x + shot.width > ufo.x &&
shot.y + shot.height > ufo.y &&
shot.x < ufo.x &&
shot.y < ufo.y + ufo.height
) {
ufo.hit = true;
ufo.img.src = 'img/boom.png';
console.log('Collion!!!');
setTimeout(() => {
ufos = ufos.filter(u => u != ufo);
}, 2000);
}
});
});
}
function createUfos() {
let ufo = {
x: 800,
y: Math.random() * 500, // Wir platzieren unsere UFOs an einem zufälligen Ort
width: 100,
height: 40,
src: 'img/ufo.png',
img: new Image()
};
ufo.img.src = ufo.src; // Ufo-Bild wird geladen.
ufos.push(ufo);
}
// Deine Hausaufgabe
function checkForShoot() {
if (KEY_SPACE) {
let shot = {
x: rocket.x + 110,
y: rocket.y + 22,
width: 20,
height: 4,
src: 'img/shot.png',
img: new Image()
};
shot.img.src = shot.src; // Laser-Bild wird geladen.
shots.push(shot);
}
}
function update() {
if (KEY_UP) {
rocket.y -= 5;
}
if (KEY_DOWN) {
rocket.y += 5;
}
ufos.forEach(function(ufo) {
if (!ufo.hit) {
ufo.x -= 5;
}
});
shots.forEach(function(shot) {
shot.x += 15;
});
}
function loadImages() {
backgroundImage.src = 'img/background.png';
rocket.img = new Image();
rocket.img.src = rocket.src;
}
function draw() {
ctx.drawImage(backgroundImage, 0, 0);
ctx.drawImage(rocket.img, rocket.x, rocket.y, rocket.width, rocket.height);
ufos.forEach(function(ufo) {
ctx.drawImage(ufo.img, ufo.x, ufo.y, ufo.width, ufo.height);
});
shots.forEach(function(shot) {
ctx.drawImage(shot.img, shot.x, shot.y, shot.width, shot.height);
});
requestAnimationFrame(draw);
}
</script>
</head>
<body onload="startGame()">
<canvas id="canvas" width="720" height="480"></canvas>
</body>
</html>