Mystery Game #2

← Back to Games List
// Space Invaders Implementation const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); // Game objects const player = { x: canvas.width / 2, y: canvas.height - 30, width: 50, height: 20, speed: 5 }; const bullet = { width: 3, height: 15, speed: 7, active: false, x: 0, y: 0 }; const invaders = []; const invaderRows = 3; const invaderCols = 8; const invaderWidth = 40; const invaderHeight = 30; const invaderPadding = 10; let invaderDirection = 1; let invaderStepDown = false; let score = 0; let gameLoop = null; // Create invaders grid function createInvaders() { for (let row = 0; row < invaderRows; row++) { for (let col = 0; col < invaderCols; col++) { invaders.push({ x: col * (invaderWidth + invaderPadding) + invaderPadding, y: row * (invaderHeight + invaderPadding) + invaderPadding, width: invaderWidth, height: invaderHeight, alive: true }); } } } // Controls let leftPressed = false; let rightPressed = false; document.addEventListener('keydown', (e) => { if (e.key === 'ArrowLeft') leftPressed = true; if (e.key === 'ArrowRight') rightPressed = true; if (e.key === ' ' && !bullet.active) { bullet.active = true; bullet.x = player.x + player.width / 2; bullet.y = player.y; } }); document.addEventListener('keyup', (e) => { if (e.key === 'ArrowLeft') leftPressed = false; if (e.key === 'ArrowRight') rightPressed = false; }); function movePlayer() { if (leftPressed && player.x > 0) { player.x -= player.speed; } if (rightPressed && player.x < canvas.width - player.width) { player.x += player.speed; } } function moveBullet() { if (bullet.active) { bullet.y -= bullet.speed; if (bullet.y < 0) { bullet.active = false; } } } function moveInvaders() { let touchedEdge = false; invaders.forEach(invader => { if (!invader.alive) return; if (invaderStepDown) { invader.y += invaderHeight; } else { invader.x += 2 * invaderDirection; if (invader.x <= 0 || invader.x + invaderWidth >= canvas.width) { touchedEdge = true; } } }); if (invaderStepDown) { invaderStepDown = false; } else if (touchedEdge) { invaderDirection *= -1; invaderStepDown = true; } } function checkCollisions() { if (!bullet.active) return; invaders.forEach(invader => { if (!invader.alive) return; if (bullet.x > invader.x && bullet.x < invader.x + invader.width && bullet.y > invader.y && bullet.y < invader.y + invader.height) { invader.alive = false; bullet.active = false; score += 10; } }); // Check if invaders reached player invaders.forEach(invader => { if (invader.alive && invader.y + invader.height >= player.y) { gameOver(); } }); } function draw() { // Clear canvas ctx.fillStyle = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); // Draw player ctx.fillStyle = 'lime'; ctx.fillRect(player.x, player.y, player.width, player.height); // Draw bullet if (bullet.active) { ctx.fillStyle = 'white'; ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height); } // Draw invaders ctx.fillStyle = 'white'; invaders.forEach(invader => { if (invader.alive) { ctx.fillRect(invader.x, invader.y, invader.width, invader.height); } }); // Draw score ctx.fillStyle = 'white'; ctx.font = '20px Arial'; ctx.fillText('Score: ' + score, 10, 30); } function gameOver() { clearInterval(gameLoop); ctx.fillStyle = 'white'; ctx.font = '40px Arial'; ctx.fillText('Game Over!', canvas.width/3, canvas.height/2); } function checkWin() { if (invaders.every(invader => !invader.alive)) { clearInterval(gameLoop); ctx.fillStyle = 'white'; ctx.font = '40px Arial'; ctx.fillText('You Win!', canvas.width/3, canvas.height/2); } } function startGame() { // Reset game state invaders.length = 0; createInvaders(); player.x = canvas.width / 2; bullet.active = false; score = 0; invaderDirection = 1; invaderStepDown = false; // Clear any existing game loop if (gameLoop) clearInterval(gameLoop); // Start game loop gameLoop = setInterval(() => { movePlayer(); moveBullet(); moveInvaders(); checkCollisions(); checkWin(); draw(); }, 1000/60); }
# Space Invaders Implementation import pygame from dataclasses import dataclass from typing import List, Optional @dataclass class Player: x: float y: float width: int = 50 height: int = 20 speed: int = 5 @dataclass class Bullet: x: float = 0 y: float = 0 width: int = 3 height: int = 15 speed: int = 7 active: bool = False @dataclass class Invader: x: float y: float width: int = 40 height: int = 30 alive: bool = True class SpaceInvaders: def __init__(self): pygame.init() self.width = 800 self.height = 600 self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption('Space Invaders') # Game objects self.player = Player(self.width / 2, self.height - 30) self.bullet = Bullet() self.invaders: List[Invader] = [] # Game settings self.invader_rows = 3 self.invader_cols = 8 self.invader_padding = 10 self.invader_direction = 1 self.invader_step_down = False self.score = 0 self.game_over = False self.clock = pygame.time.Clock() self.create_invaders() def create_invaders(self): for row in range(self.invader_rows): for col in range(self.invader_cols): self.invaders.append(Invader( x=col * (40 + self.invader_padding) + self.invader_padding, y=row * (30 + self.invader_padding) + self.invader_padding )) def handle_input(self) -> bool: keys = pygame.key.get_pressed() # Move player if keys[pygame.K_LEFT] and self.player.x > 0: self.player.x -= self.player.speed if keys[pygame.K_RIGHT] and self.player.x < self.width - self.player.width: self.player.x += self.player.speed # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and not self.bullet.active: self.bullet.active = True self.bullet.x = self.player.x + self.player.width / 2 self.bullet.y = self.player.y return True def update(self): if self.game_over: return # Move bullet if self.bullet.active: self.bullet.y -= self.bullet.speed if self.bullet.y < 0: self.bullet.active = False # Move invaders touched_edge = False for invader in self.invaders: if not invader.alive: continue if self.invader_step_down: invader.y += invader.height else: invader.x += 2 * self.invader_direction if (invader.x <= 0 or invader.x + invader.width >= self.width): touched_edge = True if self.invader_step_down: self.invader_step_down = False elif touched_edge: self.invader_direction *= -1 self.invader_step_down = True # Check collisions if self.bullet.active: for invader in self.invaders: if not invader.alive: continue if (self.bullet.x > invader.x and self.bullet.x < invader.x + invader.width and self.bullet.y > invader.y and self.bullet.y < invader.y + invader.height): invader.alive = False self.bullet.active = False self.score += 10 # Check if invaders reached player for invader in self.invaders: if (invader.alive and invader.y + invader.height >= self.player.y): self.game_over = True break # Check win condition if all(not invader.alive for invader in self.invaders): self.game_over = True def draw(self): # Clear screen self.screen.fill((0, 0, 0)) # Draw player pygame.draw.rect(self.screen, (0, 255, 0), (self.player.x, self.player.y, self.player.width, self.player.height)) # Draw bullet if self.bullet.active: pygame.draw.rect(self.screen, (255, 255, 255), (self.bullet.x, self.bullet.y, self.bullet.width, self.bullet.height)) # Draw invaders for invader in self.invaders: if invader.alive: pygame.draw.rect(self.screen, (255, 255, 255), (invader.x, invader.y, invader.width, invader.height)) # Draw score font = pygame.font.Font(None, 36) score_text = font.render(f'Score: {self.score}', True, (255, 255, 255)) self.screen.blit(score_text, (10, 10)) # Draw game over/win message if self.game_over: font = pygame.font.Font(None, 72) if all(not invader.alive for invader in self.invaders): text = font.render('You Win!', True, (255, 255, 255)) else: text = font.render('Game Over!', True, (255, 255, 255)) text_rect = text.get_rect(center=(self.width/2, self.height/2)) self.screen.blit(text, text_rect) pygame.display.flip() def run(self): while True: if not self.handle_input(): break self.update() self.draw() self.clock.tick(60) pygame.quit() if __name__ == '__main__': game = SpaceInvaders() game.run()
Play Game