-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
163 lines (148 loc) · 4.51 KB
/
index.html
File metadata and controls
163 lines (148 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Black Square Game</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
}
canvas {
background-color: #eee;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<div id="menu" style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); text-align: center;">
<h2>Choose Your Attack Style:</h2>
<button onclick="startGame('ranged')">Ranged</button>
<button onclick="startGame('swordplay')">Swordplay</button>
</div>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const playerSize = 20;
const enemySize = 20;
let playerX = canvas.width / 2;
let playerY = canvas.height / 2;
let bullets = [];
let enemies = [];
let level = 1;
let score = 0;
let attackStyle = "";
function startGame(style) {
attackStyle = style;
document.getElementById("menu").style.display = "none";
init();
gameLoop();
}
function init() {
playerX = canvas.width / 2;
playerY = canvas.height / 2;
bullets = [];
enemies = [];
for (let i = 0; i < level * 5; i++) {
spawnEnemy();
}
}
function spawnEnemy() {
const enemyX = Math.random() * (canvas.width - enemySize);
const enemyY = Math.random() * (canvas.height - enemySize);
enemies.push({ x: enemyX, y: enemyY });
}
function drawPlayer() {
ctx.fillStyle = "black";
ctx.fillRect(playerX, playerY, playerSize, playerSize);
}
function drawEnemies() {
ctx.fillStyle = "red";
enemies.forEach(enemy => {
ctx.fillRect(enemy.x, enemy.y, enemySize, enemySize);
});
}
function drawBullets() {
ctx.fillStyle = "blue";
bullets.forEach(bullet => {
ctx.fillRect(bullet.x, bullet.y, 5, 5);
});
}
function moveBullets() {
bullets.forEach(bullet => {
bullet.x += 5;
});
}
function moveEnemies() {
enemies.forEach(enemy => {
if (playerX < enemy.x) enemy.x--;
if (playerX > enemy.x) enemy.x++;
if (playerY < enemy.y) enemy.y--;
if (playerY > enemy.y) enemy.y++;
});
}
function checkCollisions() {
enemies.forEach((enemy, enemyIndex) => {
bullets.forEach((bullet, bulletIndex) => {
if (bullet.x < enemy.x + enemySize &&
bullet.x + 5 > enemy.x &&
bullet.y < enemy.y + enemySize &&
bullet.y + 5 > enemy.y) {
bullets.splice(bulletIndex, 1);
enemies.splice(enemyIndex, 1);
score++;
}
});
if (playerX < enemy.x + enemySize &&
playerX + playerSize > enemy.x &&
playerY < enemy.y + enemySize &&
playerY + playerSize > enemy.y) {
// Player hit by enemy
// For simplicity, let's reset the game here
alert("Game Over! Your Score: " + score);
level = 1;
score = 0;
init();
}
});
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
drawEnemies();
drawBullets();
moveBullets();
moveEnemies();
checkCollisions();
requestAnimationFrame(gameLoop);
}
document.addEventListener("keydown", function(event) {
switch(event.key) {
case "ArrowUp":
playerY -= 5;
break;
case "ArrowDown":
playerY += 5;
break;
case "ArrowLeft":
playerX -= 5;
break;
case "ArrowRight":
playerX += 5;
break;
case " ":
if (attackStyle === "ranged") {
bullets.push({ x: playerX + playerSize, y: playerY + (playerSize / 2) });
} else if (attackStyle === "swordplay") {
// Sword attack logic goes here (for example, check if enemy is nearby and remove it)
}
break;
}
});
</script>
</body>
</html>