Oto rozszerzona wersja gry z poruszaniem się prawym przyciskiem myszy, generatorem przedmiotów i sklepem:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title>Bed Wars 2D - Rozszerzona</title>
<style>
body {
margin: 0;
overflow: hidden;
background: #87ceeb;
font-family: sans-serif;
}
canvas {
display: block;
margin: 0 auto;
background: #81d38b;
border: 4px solid #333;
}
#shop {
position: absolute;
right: 20px;
top: 20px;
background: rgba(0,0,0,0.7);
color: white;
padding: 20px;
border-radius: 10px;
}
.shop-item {
margin: 10px 0;
cursor: pointer;
padding: 5px;
border: 1px solid #fff;
}
.shop-item:hover {
background: rgba(255,255,255,0.2);
}
</style>
</head>
<body>
<canvas id="game" width="800" height="600"></canvas>
<div id="shop">
<h3>Sklep</h3>
<div class="shop-item" onclick="buyItem('sword')">Miecz (10 złota)</div>
<div class="shop-item" onclick="buyItem('armor')">Zbroja (15 złota)</div>
<div class="shop-item" onclick="buyItem('blocks')">Bloki (5 złota)</div>
</div>
<script>
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
// Gracz
const player = {
x: 400,
y: 500,
width: 40,
height: 40,
color: "blue",
speed: 5,
targetX: 400,
targetY: 500,
inventory: {
gold: 0,
sword: false,
armor: false,
blocks: 0
}
};
// Generatory przedmiotów
const generators = [
{x: 200, y: 300, type: 'gold', timer: 0, spawnRate: 100},
{x: 600, y: 300, type: 'gold', timer: 0, spawnRate: 100}
];
// Przedmioty na mapie
let items = [];
// Łóżko
const bed = {
x: 380,
y: 550,
width: 60,
height: 20,
hp: 3
};
// Obsługa prawego przycisku myszy
canvas.addEventListener('contextmenu', (e) => {
e.preventDefault();
const rect = canvas.getBoundingClientRect();
player.targetX = e.clientX - rect.left;
player.targetY = e.clientY - rect.top;
});
function buyItem(item) {
switch(item) {
case 'sword':
if (player.inventory.gold >= 10) {
player.inventory.gold -= 10;
player.inventory.sword = true;
}
break;
case 'armor':
if (player.inventory.gold >= 15) {
player.inventory.gold -= 15;
player.inventory.armor = true;
}
break;
case 'blocks':
if (player.inventory.gold >= 5) {
player.inventory.gold -= 5;
player.inventory.blocks += 5;
}
break;
}
}
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
// Rysuj ekwipunek gracza
if (player.inventory.sword) {
ctx.fillStyle = "gold";
ctx.fillRect(player.x + 40, player.y, 10, 40);
}
if (player.inventory.armor) {
ctx.strokeStyle = "silver";
ctx.strokeRect(player.x - 2, player.y - 2, player.width + 4, player.height + 4);
}
}
function drawGenerators() {
generators.forEach(gen => {
ctx.fillStyle = "yellow";
ctx.beginPath();
ctx.arc(gen.x, gen.y, 20, 0, Math.PI * 2);
ctx.fill();
});
}
function drawItems() {
items.forEach(item => {
ctx.fillStyle = item.color;
ctx.beginPath();
ctx.arc(item.x, item.y, 10, 0, Math.PI * 2);
ctx.fill();
});
}
function drawBed() {
ctx.fillStyle = "brown";
ctx.fillRect(bed.x, bed.y, bed.width, bed.height);
ctx.fillStyle = "white";
ctx.font = "16px Arial";
ctx.fillText(`Bed HP: ${bed.hp}`, bed.x - 10, bed.y - 10);
}
function drawInventory() {
ctx.fillStyle = "white";
ctx.font = "20px Arial";
ctx.fillText(`Złoto: ${player.inventory.gold}`, 20, 30);
ctx.fillText(`Bloki: ${player.inventory.blocks}`, 20, 60);
}
function updateGenerators() {
generators.forEach(gen => {
gen.timer++;
if (gen.timer >= gen.spawnRate) {
items.push({
x: gen.x,
y: gen.y,
type: gen.type,
color: "gold"
});
gen.timer = 0;
}
});
}
function movePlayer() {
const dx = player.targetX - player.x;
const dy = player.targetY - player.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
player.x += (dx / distance) * player.speed;
player.y += (dy / distance) * player.speed;
}
}
function collectItems() {
items = items.filter(item => {
const dx = player.x - item.x;
const dy = player.y - item.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 30) {
if (item.type === 'gold') {
player.inventory.gold += 1;
}
return false;
}
return true;
});
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGenerators();
drawItems();
drawPlayer();
drawBed();
drawInventory();
}
function update() {
movePlayer();
updateGenerators();
collectItems();
}
function gameLoop() {
update();
draw();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>
Ta wersja zawiera następujące nowe funkcje:
-
Poruszanie prawym przyciskiem myszy:
- Kliknij prawym przyciskiem myszy, aby przemieścić postać w dane miejsce
- Postać płynnie porusza się do wyznaczonego celu
-
Generator przedmiotów:
- Żółte kółka na mapie to generatory złota
- Co jakiś czas wypuszczają złote monety
- Zbierz je, przechodząc blisko nich
-
System ekwipunku:
- Zbierane złoto jest zliczane
- Wyświetlana jest liczba posiadanego złota i bloków
-
Sklep:
- Menu po prawej stronie ekranu
- Możliwość kupowania miecza, zbroi i bloków
- Przedmioty są widoczne na postaci gracza
Aby rozbudować grę dalej, można dodać:
- Możliwość stawiania bloków (z kupionych w sklepie)
- System walki z wykorzystaniem miecza
- Efekt zbroi (zmniejszenie otrzymywanych obrażeń)
- Więcej typów generatorów (np. diamenty, emeraldy)
- System poziomów ulepszeń w sklepie
Daj znać, jeśli chcesz rozbudować któryś z tych elementów! 😊