Erstelle ein HTML-basiertes Weihnachtsmann-Geschicklichkeitsspiel mit folgenden Funktionen:
Nutze dabei folgende URLs für die Bilder: sled: https://www.clipartmax.com/png/small/2-20400_santa-and-reindeer-gif-images-santa-sleigh-and-reindeer.png gift: https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png house: https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background-color: #87CEEB;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
background: url('https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png') repeat-x bottom;
position: relative;
}
#santa {
position: absolute;
top: 20px;
left: 0;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="score">Highscore: 0</div>
<audio id="backgroundMusic" loop>
<source src="your-christmas-music-url-here.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'https://www.clipartmax.com/png/small/2-20400_santa-and-reindeer-gif-images-santa-sleigh-and-reindeer.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let santa = { x: 0, y: 50, width: 100, height: 50 };
let gifts = [];
let score = 0;
let lives = 20;
let gameRunning = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = 0;
gifts = [];
score = 0;
lives = 20;
gameRunning = true;
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Move Santa
santa.x += 2;
if (santa.x > canvas.width) santa.x = -santa.width;
ctx.drawImage(santaImg, santa.x, santa.y, santa.width, santa.height);
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with ground (houses)
if (gift.y + gift.height > canvas.height - 50) {
if (gift.x > santa.x && gift.x < santa.x + santa.width) {
score++;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 20, height: 20 });
}
});
</script>
</body>
</html>
Erläuterungen:
div-Elementen realisiert, die bei Spielstart bzw. Spielende eingeblendet werden.canvas-Element realisiert, auf dem der Weihnachtsmann und die Geschenke gezeichnet werden.Bitte ersetze your-christmas-music-url-here.mp3 durch die tatsächliche URL der gewünschten Hintergrundmusik.
Nutze als Hintergrundmusik: https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3 Ändere die Spielmechanik so, dass Der Weihnachtsmann in der Mitte des Bildschirms bleibt und sich die Häuser von links nach rechts unter ihm bewegen. Fülle den gesamten Bildschirm aus.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background-color: #87CEEB;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
background: url('https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png') repeat-x bottom;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
transform: translateY(-50%);
left: 50%;
transform: translateX(-50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'https://www.clipartmax.com/png/small/2-20400_santa-and-reindeer-gif-images-santa-sleigh-and-reindeer.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: canvas.width / 2 - 50, y: canvas.height / 2 - 25, width: 100, height: 50 };
let gifts = [];
let houses = [];
let score = 0;
let lives = 20;
let gameRunning = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
gifts = [];
houses = [];
score = 0;
lives = 20;
gameRunning = true;
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Santa
ctx.drawImage(santaImg, santa.x, santa.y, santa.width, santa.height);
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= 2;
ctx.drawImage(houseImg, house.x, canvas.height - 100, 100, 100);
// Remove off-screen houses
if (house.x + 100 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - 200) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 100) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 100);
if (hitHouse) {
score++;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 20, height: 20 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
nutze als hintergrundbild, das den gesamten Bildschirm ausfüllt: "background.jpg".
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.jpg') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'https://www.clipartmax.com/png/small/2-20400_santa-and-reindeer-gif-images-santa-sleigh-and-reindeer.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: canvas.width / 2 - 50, y: canvas.height / 2 - 25, width: 100, height: 50 };
let gifts = [];
let houses = [];
let score = 0;
let lives = 20;
let gameRunning = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
gifts = [];
houses = [];
score = 0;
lives = 20;
gameRunning = true;
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Santa
ctx.drawImage(santaImg, santa.x, santa.y, santa.width, santa.height);
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= 2;
ctx.drawImage(houseImg, house.x, canvas.height - 100, 100, 100);
// Remove off-screen houses
if (house.x + 100 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - 200) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 100) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 100);
if (hitHouse) {
score++;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 20, height: 20 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.jpg gefüllt und deckt den gesamten Bildschirm ab.nutze als hintergrundbild, das den gesamten Bildschirm ausfüllt: "background.png". Vergrößere die Spielelemente um das doppelte. Der Abstand der Häuser soll zufällig sein. Die Geschwindigkeit soll sich bei jedem Punkt etwas erhöhen.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'https://www.clipartmax.com/png/small/2-20400_santa-and-reindeer-gif-images-santa-sleigh-and-reindeer.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 200, height: 100 };
let gifts = [];
let houses = [];
let score = 0;
let lives = 20;
let gameRunning = false;
let speed = 2;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
gifts = [];
houses = [];
score = 0;
lives = 20;
speed = 2;
gameRunning = true;
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Santa
ctx.drawImage(santaImg, santa.x, santa.y, santa.width, santa.height);
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - Math.random() * 200 - 200) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.1;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.vergrößere den Abstand zwischen den Häusern auf 800-3000px erhöhe die Geschwindigkeit stärker bei jedem erzielten Punkt
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'https://www.clipartmax.com/png/small/2-20400_santa-and-reindeer-gif-images-santa-sleigh-and-reindeer.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 200, height: 100 };
let gifts = [];
let houses = [];
let score = 0;
let lives = 20;
let gameRunning = false;
let speed = 2;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
gifts = [];
houses = [];
score = 0;
lives = 20;
speed = 2;
gameRunning = true;
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Santa
ctx.drawImage(santaImg, santa.x, santa.y, santa.width, santa.height);
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score
scoreElement.textContent = 'Highscore: ' + score;
requestAnimationFrame(gameLoop);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.Zeige neben dem Highscore die Anzahl an Leben an. Nutze dafür Herz-Emoticons. Verringere die Anzahl an Leben auf 10.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'https://www.clipartmax.com/png/small/2-20400_santa-and-reindeer-gif-images-santa-sleigh-and-reindeer.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 200, height: 100 };
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Santa
ctx.drawImage(santaImg, santa.x, santa.y, santa.width, santa.height);
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.Ändere die URL für das Bild von Santa zu "santa.png"
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 200, height: 100 };
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Santa
ctx.drawImage(santaImg, santa.x, santa.y, santa.width, santa.height);
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.Vergrößere das Bild von Santa auf 200%
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 400, height: 200 }; // 200% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Santa
ctx.drawImage(santaImg, santa.x, santa.y, santa.width, santa.height);
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.vergrößere das Bild von santa auf 400% und spiegele es horizontal
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 2 - santa.height / 2;
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.santa soll sich im oberen drittel des bildschirms befinden und um 20% nach oben und unten schwanken
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 0.5; // Geschwindigkeit der Schwankung
const oscillationRange = 0.2; // 20% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // oberes Drittel
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
if (santa.y > canvas.height / 3 + canvas.height * oscillationRange / 2 || santa.y < canvas.height / 3 - canvas.height * oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // oberes Drittel
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.santa bewegt sich noch nicht. behebe den fehler.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 0.5; // Geschwindigkeit der Schwankung
const oscillationRange = 0.2; // 20% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // oberes Drittel
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
if (santa.y > canvas.height / 3 + canvas.height * oscillationRange / 2 || santa.y < canvas.height / 3 - canvas.height * oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // oberes Drittel
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.santa soll sich um die höhe des santa-bildes auf und ab bewegen. die pakete sollen immer direkt unter dem schlitten von santa (unten links vom santa-bild) herausfallen
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = santa.height; // Schwankungsbereich entspricht der Höhe von Santa
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // oberes Drittel
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
if (santa.y > canvas.height / 3 + oscillationRange / 2 || santa.y < canvas.height / 3 - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // oberes Drittel
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.die Bewegung von santa sollte um 10% über und 10% unter der startposition sein.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.1; // 10% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // oberes Drittel
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
if (santa.y > canvas.height / 3 + oscillationRange / 2 || santa.y < canvas.height / 3 - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // oberes Drittel
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.die bewegung von santa soll zwischen 2/3 des Bildschirms um 10% nach oben und 10% nach unten schwanken
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.1; // 10% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // oberes Drittel
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height * 2 / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height * 2 / 3 - santa.height / 2; // Startposition 2/3 des Bildschirms
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.die berechnung der anfangsposition ist falsch. diese sollte sein: "santa.x =canvas.width / 2 - canvas.width / 2" bzw. "santa.y = canvas.height / 3 - canvas.height / 2; "
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.1; // 10% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height * 2 / 3 - santa.height / 2; // Startposition 2/3 des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height * 2 / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height * 2 / 3 - santa.height / 2; // Startposition 2/3 des Bildschirms
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.die position von santa ist nun im unteren Drittel. Verschiebe santa wieder in das obere drittel
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.1; // 10% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.erhöhe die oscillation range auf 80%
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x, y: santa.y + santa.height, width: 40, height: 40 });
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.die start-position der geschenke sollte die aktuelle position und größe von santa beachten und direkt unter santa beginnen
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 800, height: 400 }; // 400% vergrößert
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40 }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).das bild von santa scheint gestaucht zu sein. korrigiere das und stelle das bild im korrekten seitenverhältnis dar
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 200, height: 200 }; // Korrektes Seitenverhältnis
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40 }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).verdoppele die breite von santa
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 400, height: 200 }; // Breite verdoppelt, Höhe beibehalten
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40 }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).Entferne die explizite Angabe der Größe von Santa und mache santa immer 1/5 des Bildschirms breit.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#santa {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 5; // Santa ist 1/5 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40 }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
santa.width = canvas.width / 5; // Santa ist 1/5 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
});
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).füge eine schnee-animation ein. Über den Bildschirm sollen leichte Schneeflocken tanzen.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 5; // Santa ist 1/5 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40 }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 5; // Santa ist 1/5 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.vergrößere Santa, sodass er immer 1/4 der Bildschirmbreite einnimmt
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'https://www.clipartmax.com/png/small/271-2716603_album-christmas-lights-house-clip-art.png';
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40 }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.ändere die url für das bild vom haus zu "villa.png"
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
let giftImg = new Image();
giftImg.src = 'https://www.clipartmax.com/png/small/1-12143_clipart-info-christmas-gift-clipart.png';
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(giftImg, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40 }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.villa.png geändert.nutze als url für jedes Geschenk zufällig einen von den Werten: "geschenk_01.png", "geschenk_02.png", "geschenk_03.png"
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 1.5em;
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 1.5em;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Highscore: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Highscore: ' + score;
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.Ändere den Text "Highscore" in "Punkte". Vergrößere die Anzeige von Leben und Punkten.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
Drücke eine beliebige Taste, um das Spiel zu starten!
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.Füge beim Startbildschirm einen erklärenden Text ein: ""Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen. Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt. Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?"
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p style="font-size: 0.5em; margin-top: 20px;">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.Füge eine Animation ein, die ein kleines Feuerwerk an der Stelle des Geschenks macht, wenn das Geschenk ein Haus getroffen hat.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p style="font-size: 0.5em; margin-top: 20px;">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
createFirework(gift.x, gift.y);
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const particles = [];
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1
});
}
function animateFirework() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `rgba(255, 255, 0, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
if (particles.length > 0) {
requestAnimationFrame(animateFirework);
}
}
animateFirework();
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.Verschiebe die Funktion "animateFirework" außerhalb der Funktion "createFirework". Rufe die Funktion "animateFirework" in jedem loop auf, wenn die Anzahl der Partikel größer 0 ist, sonst wird die Animation nicht angezeigt.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p style="font-size: 0.5em; margin-top: 20px;">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
createFirework(gift.x, gift.y);
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks
if (particles.length > 0) {
animateFirework();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1
});
}
}
function animateFirework() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `rgba(255, 255, 0, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.animateFirework wurde aus der Funktion createFirework herausgelöst und wird im gameLoop aufgerufen, wenn die Anzahl der Partikel größer 0 ist.jeder Partikel der Firework-Animation soll eine zufällige Farbe haben.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p style="font-size: 0.5em; margin-top: 20px;">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
createFirework(gift.x, gift.y);
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks
if (particles.length > 0) {
animateFirework();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
color: `hsl(${Math.random() * 360}, 100%, 50%)` // Zufällige Farbe
});
}
}
function animateFirework() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `rgba(${p.color}, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.hsl(${Math.random() * 360}, 100%, 50%)).animateFirework wurde aus der Funktion createFirework herausgelöst und wird im gameLoop aufgerufen, wenn die Anzahl der Partikel größer 0 ist.weise in createFirework der farbe nur einen int für hue zu. In AnimateFireworks nutze die Funktion hsl und den alpha-Wert, um den Wert für ctx.fillStyle zu setzen.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p style="font-size: 0.5em; margin-top: 20px;">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
createFirework(gift.x, gift.y);
} else {
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
gifts.splice(i, 1);
i--;
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks
if (particles.length > 0) {
animateFirework();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function animateFirework() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.hue), der in der Funktion createFirework zugewiesen wird.animateFirework wird der Farbton hue zusammen mit dem Alpha-Wert verwendet, um den Wert für ctx.fillStyle zu setzen (hsla(${p.hue}, 100%, 50%, ${p.alpha})).animateFirework wurde aus der Funktion createFirework herausgelöst und wird im gameLoop aufgerufen, wenn die Anzahl der Partikel größer 0 ist.Wenn ein Geschenk neben ein Haus fällt, soll es bis zum unteren Bildschirmrand fallen und dort in einer Animation zerschellen.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p style="font-size: 0.5em; margin-top: 20px;">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createShatter(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and shatters
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createShatter(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.hue), der in den Funktionen createFirework und createShatter zugewiesen wird.animateParticles wird der Farbton hue zusammen mit dem Alpha-Wert verwendet, um den Wert für ctx.fillStyle zu setzen (hsla(${p.hue}, 100%, 50%, ${p.alpha})).animateParticles wurde aus den Funktionen createFirework und createShatter herausgelöst und wird im gameLoop aufgerufen, wenn die Anzahl der Partikel größer 0 ist.Ändere die Animation für ein zerschellendes Geschenk in eine Explosions-Animation.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p style="font-size: 0.5em; margin-top: 20px;">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.hue), der in den Funktionen createFirework und createExplosion zugewiesen wird.animateParticles wird der Farbton hue zusammen mit dem Alpha-Wert verwendet, um den Wert für ctx.fillStyle zu setzen (hsla(${p.hue}, 100%, 50%, ${p.alpha})).animateParticles wurde aus den Funktionen createFirework und createExplosion herausgelöst und wird im gameLoop aufgerufen, wenn die Anzahl der Partikel größer 0 ist.Nutze für die Farben bei einer Explosion nur Hue-Werte zwischen 0-60
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p style="font-size: 0.5em; margin-top: 20px;">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.hue), der in der Funktion createFirework zugewiesen wird.hue zwischen 0 und 60), der in der Funktion createExplosion zugewiesen wird.animateParticles wird der Farbton hue zusammen mit dem Alpha-Wert verwendet, um den Wert für ctx.fillStyle zu setzen (hsla(${p.hue}, 100%, 50%, ${p.alpha})).animateParticles wurde aus den Funktionen createFirework und createExplosion herausgelöst und wird im gameLoop aufgerufen, wenn die Anzahl der Partikel größer 0 ist.ändere die Breite des Hilfetextes auf 1/3 der Bildschirmbreite
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed += 0.5;
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.hue), der in der Funktion createFirework zugewiesen wird.hue zwischen 0 und 60), der in der Funktion createExplosion zugewiesen wird.animateParticles wird der Farbton hue zusammen mit dem Alpha-Wert verwendet, um den Wert für ctx.fillStyle zu setzen (hsla(${p.hue}, 100%, 50%, ${p.alpha})).animateParticles wurde aus den Funktionen createFirework und createExplosion herausgelöst und wird im gameLoop aufgerufen, wenn die Anzahl der Partikel größer 0 ist.Limitiere die maximale Geschwindigkeit auf 12.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
const maxSpeed = 12; // Maximale Geschwindigkeit
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed = Math.min(speed + 0.5, maxSpeed); // Geschwindigkeit erhöhen, aber auf maxSpeed begrenzen
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.hue), der in der Funktion createFirework zugewiesen wird.hue zwischen 0 und 60), der in der Funktion createExplosion zugewiesen wird.animateParticles wird der Farbton hue zusammen mit dem Alpha-Wert verwendet, um den Wert für ctx.fillStyle zu setzen (hsla(${p.hue}, 100%, 50%, ${p.alpha})).animateParticles wurde aus den Funktionen createFirework und createExplosion herausgelöst und wird im gameLoop aufgerufen, wenn die Anzahl der Partikel größer 0 ist.Das Spiel hat noch einen Bug: Wenn ein Geschenk gleichzeitig den Boden und ein Haus berührt, wird der counter i zwei mal dekrementiert. Dadurch wird der Zähler -1 und es gibt einen Fehler beim Zugriff auf das Array "gifts". Stelle sicher, dass dieser Fehler nicht auftreten kann.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
Spiel vorbei! Drücke eine beliebige Taste, um neu zu starten.
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
const maxSpeed = 12; // Maximale Geschwindigkeit
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
gameOverScreen.style.display = 'flex';
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed = Math.min(speed + 0.5, maxSpeed); // Geschwindigkeit erhöhen, aber auf maxSpeed begrenzen
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
continue; // Verhindert, dass ein Geschenk sowohl ein Haus trifft als auch den Boden berührt
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
startGame();
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.hue), der in der Funktion createFirework zugewiesen wird.hue zwischen 0 und 60), der in der Funktion createExplosion zugewiesen wird.animateParticles wird der Farbton hue zusammen mit dem Alpha-Wert verwendet, um den Wert für ctx.fillStyle zu setzen (hsla(${p.hue}, 100%, 50%, ${p.alpha})).animateParticles wurde aus den Funktionen createFirework und createExplosion herausgelöst und wird im gameLoop aufgerufen, wenn die Anzahl der Partikel größer 0 ist.Zeige beim Ende des Spiels den erreichten Punktestand groß in der Mitte des Bildschirms an. Man soll mindestens 5 Sekunden warten, bevor man mit einem keypress ein neues Spiel starten kann. Zeige diese Wartezeit mit einem Fortschittsbalken an.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
#gameOverScreen p {
margin: 10px 0;
}
#progressBarContainer {
width: 50%;
background-color: #ddd;
border-radius: 25px;
overflow: hidden;
}
#progressBar {
height: 30px;
width: 0;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
<p id="finalScore"></p>
<p>Warte 5 Sekunden, bevor du neu starten kannst.</p>
<div id="progressBarContainer">
<div id="progressBar"></div>
</div>
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
const progressBar = document.getElementById('progressBar');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
const maxSpeed = 12; // Maximale Geschwindigkeit
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
let canRestart = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
canRestart = false;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
finalScoreElement.textContent = `Dein Punktestand: ${score}`;
gameOverScreen.style.display = 'flex';
startProgressBar();
}
function startProgressBar() {
let width = 0;
progressBar.style.width = width + '%';
const interval = setInterval(() => {
if (width >= 100) {
clearInterval(interval);
canRestart = true;
} else {
width += 2;
progressBar.style.width = width + '%';
}
}, 100);
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed = Math.min(speed + 0.5, maxSpeed); // Geschwindigkeit erhöhen, aber auf maxSpeed begrenzen
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
continue; // Verhindert, dass ein Geschenk sowohl ein Haus trifft als auch den Boden berührt
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning && canRestart) {
startGame();
} else if (gameRunning && e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.Jetzt kann man das Spiel nicht mehr starten. Am Anfang soll man sofort starten können. Der Countdown kommt erst am ende des Spiels.
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
#gameOverScreen p {
margin: 10px 0;
}
#progressBarContainer {
width: 50%;
background-color: #ddd;
border-radius: 25px;
overflow: hidden;
}
#progressBar {
height: 30px;
width: 0;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
<p id="finalScore"></p>
<p>Warte 5 Sekunden, bevor du neu starten kannst.</p>
<div id="progressBarContainer">
<div id="progressBar"></div>
</div>
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
const progressBar = document.getElementById('progressBar');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
const maxSpeed = 12; // Maximale Geschwindigkeit
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
let canRestart = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
canRestart = false;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
finalScoreElement.textContent = `Dein Punktestand: ${score}`;
gameOverScreen.style.display = 'flex';
startProgressBar();
}
function startProgressBar() {
let width = 0;
progressBar.style.width = width + '%';
const interval = setInterval(() => {
if (width >= 100) {
clearInterval(interval);
canRestart = true;
} else {
width += 2;
progressBar.style.width = width + '%';
}
}, 100);
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed = Math.min(speed + 0.5, maxSpeed); // Geschwindigkeit erhöhen, aber auf maxSpeed begrenzen
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
continue; // Verhindert, dass ein Geschenk sowohl ein Haus trifft als auch den Boden berührt
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
if (canRestart) {
startGame();
} else if (startScreen.style.display !== 'none') {
startGame();
}
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.Vergrößere die Schrift des Punktestands in der Mitte des Bildschirms am Spielende auf 10em
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
#gameOverScreen p {
margin: 10px 0;
}
#finalScore {
font-size: 10em; /* Vergrößerte Schriftgröße für den Punktestand */
}
#progressBarContainer {
width: 50%;
background-color: #ddd;
border-radius: 25px;
overflow: hidden;
}
#progressBar {
height: 30px;
width: 0;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
<p id="finalScore"></p>
<p>Warte 5 Sekunden, bevor du neu starten kannst.</p>
<div id="progressBarContainer">
<div id="progressBar"></div>
</div>
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
const progressBar = document.getElementById('progressBar');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
const maxSpeed = 12; // Maximale Geschwindigkeit
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
let canRestart = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
canRestart = false;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
finalScoreElement.textContent = `Dein Punktestand: ${score}`;
gameOverScreen.style.display = 'flex';
startProgressBar();
}
function startProgressBar() {
let width = 0;
progressBar.style.width = width + '%';
const interval = setInterval(() => {
if (width >= 100) {
clearInterval(interval);
canRestart = true;
} else {
width += 2;
progressBar.style.width = width + '%';
}
}, 100);
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed = Math.min(speed + 0.5, maxSpeed); // Geschwindigkeit erhöhen, aber auf maxSpeed begrenzen
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
continue; // Verhindert, dass ein Geschenk sowohl ein Haus trifft als auch den Boden berührt
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
if (canRestart) {
startGame();
} else if (startScreen.style.display !== 'none') {
startGame();
}
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.Verringere die Schrift des Punktestands in der Mitte des Bildschirms am Spielende auf 5em
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
#gameOverScreen p {
margin: 10px 0;
}
#finalScore {
font-size: 5em; /* Vergrößerte Schriftgröße für den Punktestand */
}
#progressBarContainer {
width: 50%;
background-color: #ddd;
border-radius: 25px;
overflow: hidden;
}
#progressBar {
height: 30px;
width: 0;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: white;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
<p id="finalScore"></p>
<p>Warte 5 Sekunden, bevor du neu starten kannst.</p>
<div id="progressBarContainer">
<div id="progressBar"></div>
</div>
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
const progressBar = document.getElementById('progressBar');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
const maxSpeed = 12; // Maximale Geschwindigkeit
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
let canRestart = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
canRestart = false;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
finalScoreElement.textContent = `Dein Punktestand: ${score}`;
gameOverScreen.style.display = 'flex';
startProgressBar();
}
function startProgressBar() {
let width = 0;
progressBar.style.width = width + '%';
const interval = setInterval(() => {
if (width >= 100) {
clearInterval(interval);
canRestart = true;
} else {
width += 2;
progressBar.style.width = width + '%';
}
}, 100);
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed = Math.min(speed + 0.5, maxSpeed); // Geschwindigkeit erhöhen, aber auf maxSpeed begrenzen
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
continue; // Verhindert, dass ein Geschenk sowohl ein Haus trifft als auch den Boden berührt
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
if (canRestart) {
startGame();
} else if (startScreen.style.display !== 'none') {
startGame();
}
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.füge am unteren Rand des Bildschirms rechts einen Link zum Making-Of hinzu. Nutze als URL für den Link "chat.html"
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
#gameOverScreen p {
margin: 10px 0;
}
#finalScore {
font-size: 5em; /* Vergrößerte Schriftgröße für den Punktestand */
}
#progressBarContainer {
width: 50%;
background-color: #ddd;
border-radius: 25px;
overflow: hidden;
}
#progressBar {
height: 30px;
width: 0;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: white;
}
#makingOfLink {
position: absolute;
bottom: 10px;
right: 10px;
font-size: 1em;
color: white;
text-decoration: none;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
<p id="finalScore"></p>
<p>Warte 5 Sekunden, bevor du neu starten kannst.</p>
<div id="progressBarContainer">
<div id="progressBar"></div>
</div>
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<a id="makingOfLink" href="chat.html">Making-Of</a>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
const progressBar = document.getElementById('progressBar');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
const maxSpeed = 12; // Maximale Geschwindigkeit
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
let canRestart = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
canRestart = false;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
finalScoreElement.textContent = `Dein Punktestand: ${score}`;
gameOverScreen.style.display = 'flex';
startProgressBar();
}
function startProgressBar() {
let width = 0;
progressBar.style.width = width + '%';
const interval = setInterval(() => {
if (width >= 100) {
clearInterval(interval);
canRestart = true;
} else {
width += 2;
progressBar.style.width = width + '%';
}
}, 100);
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed = Math.min(speed + 0.5, maxSpeed); // Geschwindigkeit erhöhen, aber auf maxSpeed begrenzen
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
continue; // Verhindert, dass ein Geschenk sowohl ein Haus trifft als auch den Boden berührt
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
if (canRestart) {
startGame();
} else if (startScreen.style.display !== 'none') {
startGame();
}
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.Füge den Link zum Making-of auch zum End-Bildschirm unter den Fortschrittsbalken hinzu
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
#gameOverScreen p {
margin: 10px 0;
}
#finalScore {
font-size: 5em; /* Vergrößerte Schriftgröße für den Punktestand */
}
#progressBarContainer {
width: 50%;
background-color: #ddd;
border-radius: 25px;
overflow: hidden;
}
#progressBar {
height: 30px;
width: 0;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: white;
}
#makingOfLink {
position: absolute;
bottom: 10px;
right: 10px;
font-size: 1em;
color: white;
text-decoration: none;
}
#makingOfLinkEnd {
font-size: 1em;
color: white;
text-decoration: none;
margin-top: 20px;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
<p id="finalScore"></p>
<p>Warte 5 Sekunden, bevor du neu starten kannst.</p>
<div id="progressBarContainer">
<div id="progressBar"></div>
</div>
<a id="makingOfLinkEnd" href="chat.html">Making-Of</a>
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<a id="makingOfLink" href="chat.html">Making-Of</a>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
const progressBar = document.getElementById('progressBar');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
const maxSpeed = 12; // Maximale Geschwindigkeit
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
let canRestart = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
canRestart = false;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
finalScoreElement.textContent = `Dein Punktestand: ${score}`;
gameOverScreen.style.display = 'flex';
startProgressBar();
}
function startProgressBar() {
let width = 0;
progressBar.style.width = width + '%';
const interval = setInterval(() => {
if (width >= 100) {
clearInterval(interval);
canRestart = true;
} else {
width += 2;
progressBar.style.width = width + '%';
}
}, 100);
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed = Math.min(speed + 0.5, maxSpeed); // Geschwindigkeit erhöhen, aber auf maxSpeed begrenzen
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
continue; // Verhindert, dass ein Geschenk sowohl ein Haus trifft als auch den Boden berührt
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
if (canRestart) {
startGame();
} else if (startScreen.style.display !== 'none') {
startGame();
}
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.snowCanvas) dargestellt, der über dem Spiel-Canvas liegt.Füge links und rechts vom making-of-link emoticons von fingern, die auf den Link zeigen hinzu
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Weihnachtsmann-Geschicklichkeitsspiel</title>
<style>
body {
background: url('background.png') no-repeat center center fixed;
background-size: cover;
font-family: 'Arial', sans-serif;
overflow: hidden;
margin: 0;
}
#startScreen, #gameOverScreen {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
color: white;
font-size: 2em;
text-align: center;
flex-direction: column;
}
#helpText {
font-size: 0.5em;
margin-top: 20px;
width: 33.33%; /* 1/3 der Bildschirmbreite */
}
#gameCanvas {
display: none;
position: relative;
width: 100vw;
height: 100vh;
}
#snowCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
.gift {
position: absolute;
}
#score {
position: absolute;
top: 10px;
right: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Punkte */
color: white;
}
#lives {
position: absolute;
top: 10px;
left: 10px;
font-size: 2em; /* Vergrößerte Schriftgröße für Leben */
color: white;
}
#gameOverScreen p {
margin: 10px 0;
}
#finalScore {
font-size: 5em; /* Vergrößerte Schriftgröße für den Punktestand */
}
#progressBarContainer {
width: 50%;
background-color: #ddd;
border-radius: 25px;
overflow: hidden;
}
#progressBar {
height: 30px;
width: 0;
background-color: #4caf50;
text-align: center;
line-height: 30px;
color: white;
}
#makingOfLink {
position: absolute;
bottom: 10px;
right: 10px;
font-size: 1em;
color: white;
text-decoration: none;
}
#makingOfLinkEnd {
font-size: 1em;
color: white;
text-decoration: none;
margin-top: 20px;
}
.emoji {
font-size: 1.5em;
vertical-align: middle;
}
audio {
display: none;
}
</style>
</head>
<body>
<div id="startScreen">
<p>Drücke eine beliebige Taste, um das Spiel zu starten!</p>
<p id="helpText">
Hilf Santa die Geschenke in die Villa zu bringen. Mit der Leertaste kannst du Geschenke abwerfen.
Für jedes verlorene Geschenk verlierst du ein Leben. Für jedes korrekt abgeworfene Geschenk erhältst du einen Punkt.
Mit jedem Punkt fliegt Santa etwas schneller. Wie viele Geschenke kannst du ausliefern?
</p>
</div>
<div id="gameOverScreen" style="display: none;">
<p id="finalScore"></p>
<p>Warte 5 Sekunden, bevor du neu starten kannst.</p>
<div id="progressBarContainer">
<div id="progressBar"></div>
</div>
<a id="makingOfLinkEnd" href="chat.html">
<span class="emoji">👉</span> Making-Of <span class="emoji">👈</span>
</a>
</div>
<canvas id="gameCanvas"></canvas>
<canvas id="snowCanvas"></canvas>
<div id="score">Punkte: 0</div>
<div id="lives">❤️❤️❤️❤️❤️❤️❤️❤️❤️❤️</div>
<a id="makingOfLink" href="chat.html">
<span class="emoji">👉</span> Making-Of <span class="emoji">👈</span>
</a>
<audio id="backgroundMusic" loop>
<source src="https://archive.org/download/300-christmas-songs_202305/%27Zat%20You%2C%20Santa%20Claus%20-%20LOUIS%20ARMSTRONG.mp3" type="audio/mpeg">
</audio>
<script>
const canvas = document.getElementById('gameCanvas');
const snowCanvas = document.getElementById('snowCanvas');
const ctx = canvas.getContext('2d');
const snowCtx = snowCanvas.getContext('2d');
const scoreElement = document.getElementById('score');
const livesElement = document.getElementById('lives');
const startScreen = document.getElementById('startScreen');
const gameOverScreen = document.getElementById('gameOverScreen');
const finalScoreElement = document.getElementById('finalScore');
const progressBar = document.getElementById('progressBar');
const backgroundMusic = document.getElementById('backgroundMusic');
let santaImg = new Image();
santaImg.src = 'santa.png';
const giftImages = ['geschenk_01.png', 'geschenk_02.png', 'geschenk_03.png'];
let houseImg = new Image();
houseImg.src = 'villa.png'; // URL für das Bild des Hauses geändert
let santa = { x: 0, y: 0, width: 0, height: 0 }; // Größe wird dynamisch berechnet
let gifts = [];
let houses = [];
let score = 0;
let lives = 10;
let gameRunning = false;
let speed = 2;
const maxSpeed = 12; // Maximale Geschwindigkeit
let direction = 1; // 1 für nach unten, -1 für nach oben
const oscillationSpeed = 1; // Geschwindigkeit der Schwankung
const oscillationRange = canvas.height * 0.8; // 80% Schwankungsbereich
const snowflakes = [];
const numSnowflakes = 100;
let particles = [];
let canRestart = false;
function startGame() {
startScreen.style.display = 'none';
gameOverScreen.style.display = 'none';
canvas.style.display = 'block';
snowCanvas.style.display = 'block';
backgroundMusic.play();
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
gifts = [];
houses = [];
score = 0;
lives = 10;
speed = 2;
gameRunning = true;
canRestart = false;
updateScoreAndLives();
initSnowflakes();
requestAnimationFrame(gameLoop);
}
function gameOver() {
gameRunning = false;
backgroundMusic.pause();
backgroundMusic.currentTime = 0;
canvas.style.display = 'none';
snowCanvas.style.display = 'none';
finalScoreElement.textContent = `Dein Punktestand: ${score}`;
gameOverScreen.style.display = 'flex';
startProgressBar();
}
function startProgressBar() {
let width = 0;
progressBar.style.width = width + '%';
const interval = setInterval(() => {
if (width >= 100) {
clearInterval(interval);
canRestart = true;
} else {
width += 2;
progressBar.style.width = width + '%';
}
}, 100);
}
function gameLoop() {
if (!gameRunning) return;
ctx.clearRect(0, 0, canvas.width, canvas.height);
snowCtx.clearRect(0, 0, snowCanvas.width, snowCanvas.height);
// Update Santa's vertical position for oscillation
santa.y += direction * oscillationSpeed;
const startY = canvas.height / 3 - santa.height / 2;
if (santa.y > startY + oscillationRange / 2 || santa.y < startY - oscillationRange / 2) {
direction *= -1;
}
// Draw Santa
ctx.save();
ctx.scale(-1, 1);
ctx.drawImage(santaImg, -santa.x - santa.width, santa.y, santa.width, santa.height);
ctx.restore();
// Move and draw houses
for (let i = 0; i < houses.length; i++) {
let house = houses[i];
house.x -= speed;
ctx.drawImage(houseImg, house.x, canvas.height - 200, 200, 200);
// Remove off-screen houses
if (house.x + 200 < 0) {
houses.splice(i, 1);
i--;
}
}
// Add new houses
if (houses.length === 0 || houses[houses.length - 1].x < canvas.width - (Math.random() * 2200 + 800)) {
houses.push({ x: canvas.width });
}
// Move and draw gifts
for (let i = 0; i < gifts.length; i++) {
let gift = gifts[i];
gift.y += 5;
ctx.drawImage(gift.image, gift.x, gift.y, gift.width, gift.height);
// Check collision with houses
if (gift.y + gift.height > canvas.height - 200) {
let hitHouse = houses.some(house => gift.x > house.x && gift.x < house.x + 200);
if (hitHouse) {
score++;
speed = Math.min(speed + 0.5, maxSpeed); // Geschwindigkeit erhöhen, aber auf maxSpeed begrenzen
createFirework(gift.x, gift.y);
gifts.splice(i, 1);
i--;
continue; // Verhindert, dass ein Geschenk sowohl ein Haus trifft als auch den Boden berührt
} else {
gift.hitGround = true;
}
}
// Check if gift hits the ground
if (gift.hitGround && gift.y + gift.height > canvas.height) {
createExplosion(gift.x, canvas.height - gift.height / 2);
gifts.splice(i, 1);
i--;
lives--;
if (lives <= 0) {
gameOver();
return;
}
}
}
// Update score and lives
updateScoreAndLives();
// Draw snowflakes
drawSnowflakes();
// Animate fireworks and explosions
if (particles.length > 0) {
animateParticles();
}
requestAnimationFrame(gameLoop);
}
function updateScoreAndLives() {
scoreElement.textContent = 'Punkte: ' + score; // Text von "Highscore" in "Punkte" geändert
livesElement.innerHTML = '❤️'.repeat(lives);
}
document.addEventListener('keydown', (e) => {
if (!gameRunning) {
if (canRestart) {
startGame();
} else if (startScreen.style.display !== 'none') {
startGame();
}
} else if (e.code === 'Space') {
let randomGiftImage = giftImages[Math.floor(Math.random() * giftImages.length)];
let giftImg = new Image();
giftImg.src = randomGiftImage;
gifts.push({ x: santa.x + santa.width / 2 - 20, y: santa.y + santa.height, width: 40, height: 40, image: giftImg }); // Geschenk direkt unter Santa
}
});
window.addEventListener('resize', () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
snowCanvas.width = window.innerWidth;
snowCanvas.height = window.innerHeight;
santa.width = canvas.width / 4; // Santa ist 1/4 der Bildschirmbreite
santa.height = santa.width * (santaImg.height / santaImg.width); // Höhe basierend auf dem Seitenverhältnis des Bildes
santa.x = canvas.width / 2 - santa.width / 2;
santa.y = canvas.height / 3 - santa.height / 2; // Startposition im oberen Drittel des Bildschirms
initSnowflakes();
});
function initSnowflakes() {
snowflakes.length = 0; // Clear existing snowflakes
for (let i = 0; i < numSnowflakes; i++) {
snowflakes.push({
x: Math.random() * snowCanvas.width,
y: Math.random() * snowCanvas.height,
radius: Math.random() * 3 + 1,
speed: Math.random() * 1 + 0.5,
drift: Math.random() * 0.5 - 0.25
});
}
}
function drawSnowflakes() {
snowCtx.fillStyle = 'white';
snowCtx.beginPath();
for (let flake of snowflakes) {
snowCtx.moveTo(flake.x, flake.y);
snowCtx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
flake.y += flake.speed;
flake.x += flake.drift;
if (flake.y > snowCanvas.height) {
flake.y = -flake.radius;
}
if (flake.x > snowCanvas.width) {
flake.x = -flake.radius;
}
if (flake.x < -flake.radius) {
flake.x = snowCanvas.width + flake.radius;
}
}
snowCtx.fill();
}
function createFirework(x, y) {
const numParticles = 20;
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 3 + 1,
speedX: (Math.random() - 0.5) * 4,
speedY: (Math.random() - 0.5) * 4,
alpha: 1,
hue: Math.random() * 360 // Zufälliger Farbton
});
}
}
function createExplosion(x, y) {
const numParticles = 50; // Mehr Partikel für eine größere Explosion
for (let i = 0; i < numParticles; i++) {
particles.push({
x: x,
y: y,
radius: Math.random() * 5 + 2, // Größere Partikel für die Explosion
speedX: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
speedY: (Math.random() - 0.5) * 8, // Schnellere Partikelbewegung
alpha: 1,
hue: Math.random() * 60 // Zufälliger Farbton zwischen 0 und 60
});
}
}
function animateParticles() {
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.x += p.speedX;
p.y += p.speedY;
p.alpha -= 0.02;
if (p.alpha <= 0) {
particles.splice(i, 1);
i--;
continue;
}
ctx.fillStyle = `hsla(${p.hue}, 100%, 50%, ${p.alpha})`;
ctx.beginPath();
ctx.arc(p.x, p.y, p.radius, 0, Math.PI * 2);
ctx.fill();
}
}
window.dispatchEvent(new Event('resize'));
</script>
</body>
</html>
Erläuterungen:
background.png gefüllt und deckt den gesamten Bildschirm ab.santa.width * (santaImg.height / santaImg.width)).santa.png geändert.santa.x + santa.width / 2 - 20 und santa.y + santa.height).villa.png geändert.geschenk_01.png, geschenk_02.png oder geschenk_03.png.