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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| class Game { private ctx: CanvasRenderingContext2D; private lastTime: number = 0; private state: GameState = GameState.MENU; private player: Player; private enemies: Enemy[] = []; private effects: Effect[] = []; private readonly CANVAS_WIDTH = 800; private readonly CANVAS_HEIGHT = 600; private readonly MAX_ENEMIES = 5; private readonly ENEMY_SPAWN_INTERVAL = 3000; private lastEnemySpawn: number = 0;
constructor(canvas: HTMLCanvasElement) { this.ctx = canvas.getContext('2d')!; this.player = new Player(400, 300); this.setupEventListeners(); }
private setupEventListeners(): void { document.addEventListener('keydown', (e) => this.handleKeyDown(e)); document.addEventListener('keyup', (e) => this.handleKeyUp(e)); document.addEventListener('mousemove', (e) => this.handleMouseMove(e)); document.addEventListener('mousedown', (e) => this.handleMouseDown(e)); document.addEventListener('mouseup', (e) => this.handleMouseUp(e)); }
start(): void { this.state = GameState.PLAYING; this.lastTime = performance.now(); this.gameLoop(); }
private gameLoop(): void { if (this.state !== GameState.PLAYING) return;
const currentTime = performance.now(); const deltaTime = (currentTime - this.lastTime) / 1000; this.lastTime = currentTime;
this.update(deltaTime); this.render();
requestAnimationFrame(() => this.gameLoop()); }
private update(deltaTime: number): void { this.player.update(deltaTime);
for (const enemy of this.enemies) { enemy.update(deltaTime); }
this.spawnEnemies();
this.enemies = this.enemies.filter(enemy => enemy.health > 0);
this.updateEffects(deltaTime);
this.checkGameOver(); }
private render(): void { this.ctx.clearRect(0, 0, this.CANVAS_WIDTH, this.CANVAS_HEIGHT);
this.renderBackground();
this.player.render(this.ctx); this.enemies.forEach(enemy => enemy.render(this.ctx)); this.effects.forEach(effect => effect.render(this.ctx));
this.renderUI(); }
private spawnEnemies(): void { const currentTime = performance.now(); if (currentTime - this.lastEnemySpawn >= this.ENEMY_SPAWN_INTERVAL && this.enemies.length < this.MAX_ENEMIES) { const spawnPoint = this.getRandomSpawnPoint(); const enemy = new Enemy(spawnPoint.x, spawnPoint.y, this.player); this.enemies.push(enemy); this.lastEnemySpawn = currentTime; } }
private getRandomSpawnPoint(): Point { const side = Math.floor(Math.random() * 4); let x = 0, y = 0;
switch (side) { case 0: x = Math.random() * this.CANVAS_WIDTH; y = -20; break; case 1: x = this.CANVAS_WIDTH + 20; y = Math.random() * this.CANVAS_HEIGHT; break; case 2: x = Math.random() * this.CANVAS_WIDTH; y = this.CANVAS_HEIGHT + 20; break; case 3: x = -20; y = Math.random() * this.CANVAS_HEIGHT; break; }
return { x, y }; }
private updateEffects(deltaTime: number): void { this.effects = this.effects.filter(effect => !effect.isFinished()); this.effects.forEach(effect => effect.update(deltaTime)); }
private checkGameOver(): void { if (this.player.health <= 0) { this.state = GameState.GAME_OVER; console.log('Game Over!'); } }
private renderBackground(): void { this.ctx.fillStyle = '#000'; this.ctx.fillRect(0, 0, this.CANVAS_WIDTH, this.CANVAS_HEIGHT); }
private renderUI(): void { this.ctx.fillStyle = '#fff'; this.ctx.font = '20px Arial'; this.ctx.fillText(`Health: ${this.player.health}`, 10, 30); this.ctx.fillText(`Score: ${this.player.score}`, 10, 60); if (this.state === GameState.GAME_OVER) { this.ctx.fillStyle = 'rgba(0, 0, 0, 0.7)'; this.ctx.fillRect(0, 0, this.CANVAS_WIDTH, this.CANVAS_HEIGHT); this.ctx.fillStyle = '#fff'; this.ctx.font = '48px Arial'; this.ctx.fillText('GAME OVER', this.CANVAS_WIDTH / 2 - 100, this.CANVAS_HEIGHT / 2); this.ctx.font = '24px Arial'; this.ctx.fillText(`Final Score: ${this.player.score}`, this.CANVAS_WIDTH / 2 - 70, this.CANVAS_HEIGHT / 2 + 40); } }
private handleKeyDown(e: KeyboardEvent): void { if (this.state === GameState.PLAYING) { this.player.handleKeyDown(e); } }
private handleKeyUp(e: KeyboardEvent): void { if (this.state === GameState.PLAYING) { this.player.handleKeyUp(e); } }
private handleMouseMove(e: MouseEvent): void { if (this.state === GameState.PLAYING) { this.player.handleMouseMove(e); } }
private handleMouseDown(e: MouseEvent): void { if (this.state === GameState.PLAYING) { this.player.handleMouseDown(e); } }
private handleMouseUp(e: MouseEvent): void { if (this.state === GameState.PLAYING) { this.player.handleMouseUp(e); } } }
|