Advertisement
muu

QwenCoder Bullet Hell game

muu
Nov 24th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Bullet Hell Shooter</title>
  7. <style>
  8. body { margin: 0; display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #000; }
  9. canvas { border: 1px solid #fff; touch-action: none; }
  10. .menu, .leaderboard {
  11. position: absolute;
  12. width: 80%;
  13. max-width: 400px;
  14. padding: 20px;
  15. background-color: rgba(0, 0, 0, 0.7);
  16. color: #fff;
  17. border-radius: 10px;
  18. text-align: center;
  19. }
  20. .leaderboard table {
  21. width: 100%;
  22. margin-top: 20px;
  23. }
  24. .leaderboard td, .leaderboard th {
  25. padding: 5px;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30.  
  31. <canvas id="gameCanvas"></canvas>
  32. <div class="menu" id="introMenu">
  33. <h1>Bullet Hell Shooter</h1>
  34. <button onclick="startGame()">Start Game</button>
  35. <br><br>
  36. <a href="#" onclick="showLeaderboard(); return false;">View Leaderboard</a>
  37. </div>
  38. <div class="leaderboard" id="leaderboardMenu" style="display:none;">
  39. <h2>High Scores</h2>
  40. <table>
  41. <thead>
  42. <tr>
  43. <th>Rank</th>
  44. <th>Name</th>
  45. <th>Score</th>
  46. </tr>
  47. </thead>
  48. <tbody id="scoreTable">
  49. </tbody>
  50. </table>
  51. <br><br>
  52. <a href="#" onclick="hideLeaderboard(); return false;">Back to Menu</a>
  53. </div>
  54.  
  55. <script>
  56. const canvas = document.getElementById('gameCanvas');
  57. const ctx = canvas.getContext('2d');
  58.  
  59. let width = Math.min(window.innerWidth, 600);
  60. let height = Math.min(window.innerHeight, 800);
  61.  
  62. canvas.width = width;
  63. canvas.height = height;
  64.  
  65. const introMenu = document.getElementById('introMenu');
  66. const leaderboardMenu = document.getElementById('leaderboardMenu');
  67. const scoreTable = document.getElementById('scoreTable');
  68.  
  69. let playerX = width / 2;
  70. let playerY = height - 50;
  71. const playerRadius = 10;
  72. const bullets = [];
  73. const enemies = [];
  74. const enemyBullets = [];
  75. let score = 0;
  76. let highScores = JSON.parse(localStorage.getItem('highScores')) || [];
  77.  
  78. function resizeCanvas() {
  79. width = Math.min(window.innerWidth, 600);
  80. height = Math.min(window.innerHeight, 800);
  81. canvas.width = width;
  82. canvas.height = height;
  83. playerX = width / 2;
  84. playerY = height - 50;
  85. }
  86.  
  87. window.addEventListener('resize', resizeCanvas);
  88.  
  89. document.addEventListener('touchstart', function(e) {
  90. const touch = e.touches[0];
  91. if (playerX < touch.clientX) {
  92. moveRight();
  93. } else {
  94. moveLeft();
  95. }
  96. });
  97.  
  98. document.addEventListener('keydown', function(e) {
  99. if (e.key === 'ArrowRight') {
  100. moveRight();
  101. } else if (e.key === 'ArrowLeft') {
  102. moveLeft();
  103. } else if (e.key === ' ') {
  104. shootBullet();
  105. }
  106. });
  107.  
  108. function moveRight() {
  109. playerX = Math.min(playerX + 10, width - playerRadius);
  110. }
  111.  
  112. function moveLeft() {
  113. playerX = Math.max(playerX - 10, playerRadius);
  114. }
  115.  
  116. function shootBullet() {
  117. bullets.push({ x: playerX, y: playerY, dy: -10 });
  118. }
  119.  
  120. function spawnEnemy() {
  121. const enemySize = 20;
  122. enemies.push({
  123. x: Math.random() * (width - enemySize),
  124. y: -enemySize,
  125. size: enemySize,
  126. dx: Math.random() * 4 - 2,
  127. dy: 3 + Math.random() * 2
  128. });
  129. }
  130.  
  131. function spawnEnemyBullet(enemy) {
  132. enemyBullets.push({ x: enemy.x + enemy.size / 2, y: enemy.y + enemy.size, dy: 5 });
  133. }
  134.  
  135. function drawPlayer() {
  136. ctx.beginPath();
  137. ctx.arc(playerX, playerY, playerRadius, 0, Math.PI * 2);
  138. ctx.fillStyle = 'blue';
  139. ctx.fill();
  140. ctx.closePath();
  141. }
  142.  
  143. function updateBullets() {
  144. bullets.forEach((bullet, index) => {
  145. bullet.y += bullet.dy;
  146. if (bullet.y < 0) {
  147. bullets.splice(index, 1);
  148. }
  149. });
  150. }
  151.  
  152. function drawBullets() {
  153. bullets.forEach(bullet => {
  154. ctx.beginPath();
  155. ctx.arc(bullet.x, bullet.y, 2, 0, Math.PI * 2);
  156. ctx.fillStyle = 'white';
  157. ctx.fill();
  158. ctx.closePath();
  159. });
  160. }
  161.  
  162. function updateEnemies() {
  163. enemies.forEach((enemy, index) => {
  164. enemy.x += enemy.dx;
  165. enemy.y += enemy.dy;
  166. if (Math.random() < 0.01) {
  167. spawnEnemyBullet(enemy);
  168. }
  169. if (enemy.x < 0 || enemy.x > width - enemy.size) {
  170. enemy.dx = -enemy.dx;
  171. }
  172. if (enemy.y > height) {
  173. enemies.splice(index, 1);
  174. }
  175. });
  176. }
  177.  
  178. function drawEnemies() {
  179. enemies.forEach(enemy => {
  180. ctx.fillRect(enemy.x, enemy.y, enemy.size, enemy.size);
  181. ctx.fillStyle = 'red';
  182. });
  183. }
  184.  
  185. function updateEnemyBullets() {
  186. enemyBullets.forEach((bullet, index) => {
  187. bullet.y += bullet.dy;
  188. if (bullet.y > height) {
  189. enemyBullets.splice(index, 1);
  190. }
  191. });
  192. }
  193.  
  194. function drawEnemyBullets() {
  195. enemyBullets.forEach(bullet => {
  196. ctx.beginPath();
  197. ctx.arc(bullet.x, bullet.y, 2, 0, Math.PI * 2);
  198. ctx.fillStyle = 'orange';
  199. ctx.fill();
  200. ctx.closePath();
  201. });
  202. }
  203.  
  204. function checkCollisions() {
  205. bullets.forEach((bullet, bIndex) => {
  206. enemies.forEach((enemy, eIndex) => {
  207. if (bullet.x > enemy.x && bullet.x < enemy.x + enemy.size &&
  208. bullet.y > enemy.y && bullet.y < enemy.y + enemy.size) {
  209. score += 10;
  210. enemies.splice(eIndex, 1);
  211. bullets.splice(bIndex, 1);
  212. return;
  213. }
  214. });
  215. });
  216.  
  217. enemyBullets.forEach((bullet, bIndex) => {
  218. if (bullet.x > playerX - playerRadius && bullet.x < playerX + playerRadius &&
  219. bullet.y > playerY - playerRadius && bullet.y < playerY + playerRadius) {
  220. endGame();
  221. }
  222. });
  223. }
  224.  
  225. function drawScore() {
  226. ctx.font = '20px Arial';
  227. ctx.fillStyle = 'white';
  228. ctx.fillText(`Score: ${score}`, 10, 30);
  229. }
  230.  
  231. function updateHighScores() {
  232. highScores.push({ name: prompt("Enter your name:"), score: score });
  233. highScores.sort((a, b) => b.score - a.score);
  234. highScores = highScores.slice(0, 5); // Keep top 5 scores
  235. localStorage.setItem('highScores', JSON.stringify(highScores));
  236. }
  237.  
  238. function showLeaderboard() {
  239. introMenu.style.display = 'none';
  240. leaderboardMenu.style.display = 'block';
  241.  
  242. scoreTable.innerHTML = '';
  243. highScores.forEach((scoreEntry, index) => {
  244. const row = document.createElement('tr');
  245. const rankCell = document.createElement('td');
  246. rankCell.textContent = index + 1;
  247. const nameCell = document.createElement('td');
  248. nameCell.textContent = scoreEntry.name;
  249. const scoreCell = document.createElement('td');
  250. scoreCell.textContent = scoreEntry.score;
  251.  
  252. row.appendChild(rankCell);
  253. row.appendChild(nameCell);
  254. row.appendChild(scoreCell);
  255.  
  256. scoreTable.appendChild(row);
  257. });
  258. }
  259.  
  260. function hideLeaderboard() {
  261. leaderboardMenu.style.display = 'none';
  262. introMenu.style.display = 'block';
  263. }
  264.  
  265. function endGame() {
  266. updateHighScores();
  267. alert(`Game Over! Your score: ${score}`);
  268. bullets.length = 0;
  269. enemies.length = 0;
  270. enemyBullets.length = 0;
  271. playerX = width / 2;
  272. playerY = height - 50;
  273. score = 0;
  274. introMenu.style.display = 'block';
  275. }
  276.  
  277. function gameLoop() {
  278. ctx.clearRect(0, 0, width, height);
  279.  
  280. drawPlayer();
  281. updateBullets();
  282. drawBullets();
  283. updateEnemies();
  284. drawEnemies();
  285. updateEnemyBullets();
  286. drawEnemyBullets();
  287. checkCollisions();
  288. drawScore();
  289.  
  290. requestAnimationFrame(gameLoop);
  291. }
  292.  
  293. function startGame() {
  294. introMenu.style.display = 'none';
  295. spawnInterval = setInterval(spawnEnemy, 1000);
  296. gameLoop();
  297. }
  298.  
  299. document.getElementById('startButton').addEventListener('click', startGame);
  300.  
  301. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement