-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
178 lines (155 loc) · 4.6 KB
/
Copy pathindex.html
File metadata and controls
178 lines (155 loc) · 4.6 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
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
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="google-adsense-account" content="ca-pub-4465607676285389">
<title>Snake.io Style Game</title>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4465607676285389" crossorigin="anonymous"></script>
<style>
body {
margin: 0;
background: #111;
display: flex;
flex-direction: column;
align-items: center;
font-family: Arial, sans-serif;
color: white;
overflow-x: hidden;
}
#container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
margin-top: 20px;
gap: 10px;
}
canvas {
background: #000;
border: 2px solid #444;
max-width: 80vw;
}
.side-ad {
width: 120px;
height: 600px;
}
#description {
max-width: 600px;
padding: 20px;
line-height: 1.5;
color: #aaa;
text-align: center;
}
/* Styling für den neuen Fluid-Anzeigenblock */
.fluid-ad-container {
width: 100%;
max-width: 600px;
margin: 20px 0;
}
</style>
</head>
<body>
<h1>Snake Game</h1>
<div id="container">
<ins class="adsbygoogle side-ad"
style="display:block"
data-ad-client="ca-pub-4465607676285389"
data-ad-slot="1111111111"
data-ad-format="auto"></ins>
<canvas id="game" width="600" height="600"></canvas>
<ins class="adsbygoogle side-ad"
style="display:block"
data-ad-client="ca-pub-4465607676285389"
data-ad-slot="2222222222"
data-ad-format="auto"></ins>
</div>
<div class="fluid-ad-container">
<ins class="adsbygoogle"
style="display:block"
data-ad-format="fluid"
data-ad-layout-key="-gw-3+1f-3d+2z"
data-ad-client="ca-pub-4465607676285389"
data-ad-slot="4674321377"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>
<div id="description">
<h2>Spielanleitung & Info</h2>
<p>Steuere die Schlange mit den Pfeiltasten deiner Tastatur. Ziel ist es, so viel Futter wie möglich zu fressen, ohne die Wand oder dich selbst zu berühren. Dieses Spiel ist ein Open-Source Projekt auf GitHub Pages.</p>
</div>
<script>
// Initialisierung der restlichen Anzeigenblöcke
(adsbygoogle = window.adsbygoogle || []).push({});
(adsbygoogle = window.adsbygoogle || []).push({});
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const grid = 20;
let snake = [{ x: 10, y: 10 }];
let dx = 1;
let dy = 0;
let food = { x: 15, y: 15 };
let score = 0;
window.addEventListener("keydown", function(e) {
if(["Space","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) {
e.preventDefault();
}
}, false);
function gameLoop() {
update();
draw();
}
function update() {
const head = { x: snake[0].x + dx, y: snake[0].y + dy };
if (head.x < 0 || head.y < 0 || head.x >= canvas.width / grid || head.y >= canvas.height / grid) {
resetGame();
return;
}
for (let part of snake) {
if (part.x === head.x && part.y === head.y) {
resetGame();
return;
}
}
snake.unshift(head);
if (head.x === food.x && head.y === food.y) {
score++;
spawnFood();
} else {
snake.pop();
}
}
function draw() {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "lime";
for (let part of snake) {
ctx.fillRect(part.x * grid, part.y * grid, grid - 2, grid - 2);
}
ctx.fillStyle = "red";
ctx.fillRect(food.x * grid, food.y * grid, grid - 2, grid - 2);
ctx.fillStyle = "white";
ctx.font = "16px Arial";
ctx.fillText("Score: " + score, 10, 20);
}
function spawnFood() {
food.x = Math.floor(Math.random() * (canvas.width / grid));
food.y = Math.floor(Math.random() * (canvas.height / grid));
}
function resetGame() {
snake = [{ x: 10, y: 10 }];
dx = 1;
dy = 0;
score = 0;
spawnFood();
}
document.addEventListener("keydown", e => {
if (e.key === "ArrowUp" && dy === 0) { dx = 0; dy = -1; }
if (e.key === "ArrowDown" && dy === 0) { dx = 0; dy = 1; }
if (e.key === "ArrowLeft" && dx === 0) { dx = -1; dy = 0; }
if (e.key === "ArrowRight" && dx === 0) { dx = 1; dy = 0; }
});
setInterval(gameLoop, 100);
</script>
</body>
</html>