Tank Trouble Remake

Tank Trouble game remake

目前为 2025-03-24 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Tank Trouble Remake
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Tank Trouble game remake
  6. // @author You
  7. // @match https://tanktrouble.com
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. // Vector2D class
  14. class Vector2D {
  15. constructor(x = 0, y = 0) {
  16. this.x = x;
  17. this.y = y;
  18. }
  19. add(v) {
  20. return new Vector2D(this.x + v.x, this.y + v.y);
  21. }
  22. multiply(scalar) {
  23. return new Vector2D(this.x * scalar, this.y * scalar);
  24. }
  25. }
  26.  
  27. // Entity class
  28. class Entity {
  29. constructor(position) {
  30. this.position = position;
  31. this.velocity = new Vector2D();
  32. }
  33. update(deltaTime) {
  34. this.position = this.position.add(this.velocity.multiply(deltaTime));
  35. }
  36. draw(ctx) {}
  37. }
  38.  
  39. // Tank class
  40. class Tank extends Entity {
  41. constructor(x, y) {
  42. super(new Vector2D(x, y));
  43. this.direction = 0;
  44. this.speed = 100;
  45. }
  46. rotate(amount) {
  47. this.direction += amount * Math.PI / 180;
  48. }
  49. fire() {
  50. return new Bullet(
  51. this.position.x,
  52. this.position.y,
  53. this.direction
  54. );
  55. }
  56. draw(ctx) {
  57. ctx.save();
  58. ctx.translate(this.position.x, this.position.y);
  59. ctx.rotate(this.direction);
  60. // Draw tank body
  61. ctx.fillStyle = 'blue';
  62. ctx.fillRect(-20, -15, 40, 30);
  63. // Draw turret
  64. ctx.fillStyle = 'gray';
  65. ctx.fillRect(-10, -10, 20, 20);
  66. ctx.restore();
  67. }
  68. }
  69.  
  70. // Bullet class
  71. class Bullet extends Entity {
  72. constructor(x, y, direction) {
  73. super(new Vector2D(x, y));
  74. this.velocity = new Vector2D(
  75. Math.cos(direction) * 500,
  76. Math.sin(direction) * 500
  77. );
  78. }
  79. draw(ctx) {
  80. ctx.fillStyle = 'red';
  81. ctx.beginPath();
  82. ctx.arc(this.position.x, this.position.y, 5, 0, Math.PI * 2);
  83. ctx.fill();
  84. }
  85. isOffScreen(canvas) {
  86. return (
  87. this.position.x < 0 ||
  88. this.position.x > canvas.width ||
  89. this.position.y < 0 ||
  90. this.position.y > canvas.height
  91. );
  92. }
  93. }
  94.  
  95. // Game class
  96. class Game {
  97. constructor() {
  98. this.canvas = document.createElement('canvas');
  99. this.canvas.width = 800;
  100. this.canvas.height = 600;
  101. this.ctx = this.canvas.getContext('2d');
  102. document.body.appendChild(this.canvas);
  103. this.tanks = [new Tank(400, 300)];
  104. this.bullets = [];
  105. this.setupControls();
  106. this.gameLoop();
  107. }
  108. setupControls() {
  109. document.addEventListener('keydown', (event) => {
  110. const tank = this.tanks[0];
  111. switch(event.key) {
  112. case 'ArrowLeft':
  113. tank.rotate(-5);
  114. break;
  115. case 'ArrowRight':
  116. tank.rotate(5);
  117. break;
  118. case ' ':
  119. this.bullets.push(tank.fire());
  120. break;
  121. }
  122. });
  123. }
  124. gameLoop() {
  125. const deltaTime = 1/60;
  126. this.update(deltaTime);
  127. this.render();
  128. requestAnimationFrame(() => this.gameLoop());
  129. }
  130. update(deltaTime) {
  131. this.tanks.forEach(tank => tank.update(deltaTime));
  132. this.bullets.forEach(bullet => bullet.update(deltaTime));
  133. this.bullets = this.bullets.filter(bullet => !bullet.isOffScreen(this.canvas));
  134. }
  135. render() {
  136. this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
  137. this.tanks.forEach(tank => tank.draw(this.ctx));
  138. this.bullets.forEach(bullet => bullet.draw(this.ctx));
  139. }
  140. }
  141.  
  142. // Start the game
  143. new Game();
  144. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址