Mystery Game #1

← Back to Games List
// Snake Game Implementation const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const gridSize = 20; const tileCount = canvas.width / gridSize; let snake = [ {x: 10, y: 10}, ]; let food = {x: 15, y: 15}; let dx = 1; // Start moving right let dy = 0; let score = 0; let gameLoop = null; // Handle keyboard controls document.addEventListener('keydown', (e) => { switch(e.key) { case 'ArrowUp': if (dy === 0) { dx = 0; dy = -1; } break; case 'ArrowDown': if (dy === 0) { dx = 0; dy = 1; } break; case 'ArrowLeft': if (dx === 0) { dx = -1; dy = 0; } break; case 'ArrowRight': if (dx === 0) { dx = 1; dy = 0; } break; } }); function drawGame() { // Clear canvas ctx.fillStyle = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); // Draw food ctx.fillStyle = 'red'; ctx.fillRect(food.x * gridSize, food.y * gridSize, gridSize - 2, gridSize - 2); // Draw snake ctx.fillStyle = 'lime'; snake.forEach(segment => { ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize - 2, gridSize - 2); }); // Draw score ctx.fillStyle = 'white'; ctx.font = '20px Arial'; ctx.fillText('Score: ' + score, 10, 30); } function moveSnake() { // Calculate new head position const head = {x: snake[0].x + dx, y: snake[0].y + dy}; // Check for wall collision if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount) { gameOver(); return; } // Check for self collision if (snake.some(segment => segment.x === head.x && segment.y === head.y)) { gameOver(); return; } // Add new head snake.unshift(head); // Check for food collision if (head.x === food.x && head.y === food.y) { score += 10; generateFood(); } else { // Remove tail if no food eaten snake.pop(); } } function generateFood() { while (true) { food = { x: Math.floor(Math.random() * tileCount), y: Math.floor(Math.random() * tileCount) }; // Make sure food doesn't spawn on snake if (!snake.some(segment => segment.x === food.x && segment.y === food.y)) { break; } } } function gameOver() { clearInterval(gameLoop); ctx.fillStyle = 'white'; ctx.font = '40px Arial'; ctx.fillText('Game Over!', canvas.width/4, canvas.height/2); } function startGame() { // Reset game state snake = [{x: 10, y: 10}]; dx = 1; // Start moving right dy = 0; score = 0; generateFood(); // Clear any existing game loop if (gameLoop) clearInterval(gameLoop); // Start game loop gameLoop = setInterval(() => { moveSnake(); drawGame(); }, 100); }
# Snake Game Implementation import pygame import random from dataclasses import dataclass from typing import List @dataclass class Point: x: int y: int class SnakeGame: def __init__(self): pygame.init() self.width = 800 self.height = 600 self.grid_size = 20 self.tile_count = self.width // self.grid_size self.screen = pygame.display.set_mode((self.width, self.height)) pygame.display.set_caption('Snake Game') self.clock = pygame.time.Clock() self.snake = [Point(10, 10)] self.food = Point(15, 15) self.dx = 1 # Start moving right self.dy = 0 self.score = 0 self.game_over = False def handle_input(self): for event in pygame.event.get(): if event.type == pygame.QUIT: return False if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and self.dy == 0: self.dx, self.dy = 0, -1 elif event.key == pygame.K_DOWN and self.dy == 0: self.dx, self.dy = 0, 1 elif event.key == pygame.K_LEFT and self.dx == 0: self.dx, self.dy = -1, 0 elif event.key == pygame.K_RIGHT and self.dx == 0: self.dx, self.dy = 1, 0 return True def move_snake(self): if self.game_over: return # Calculate new head position head = Point( self.snake[0].x + self.dx, self.snake[0].y + self.dy ) # Check for wall collision if (head.x < 0 or head.x >= self.tile_count or head.y < 0 or head.y >= self.tile_count): self.game_over = True return # Check for self collision if any(segment.x == head.x and segment.y == head.y for segment in self.snake): self.game_over = True return # Add new head self.snake.insert(0, head) # Check for food collision if head.x == self.food.x and head.y == self.food.y: self.score += 10 self.generate_food() else: self.snake.pop() def generate_food(self): while True: food = Point( random.randint(0, self.tile_count - 1), random.randint(0, self.tile_count - 1) ) # Make sure food doesn't spawn on snake if not any(segment.x == food.x and segment.y == food.y for segment in self.snake): self.food = food break def draw(self): # Clear screen self.screen.fill((0, 0, 0)) # Draw food pygame.draw.rect(self.screen, (255, 0, 0), (self.food.x * self.grid_size, self.food.y * self.grid_size, self.grid_size - 2, self.grid_size - 2)) # Draw snake for segment in self.snake: pygame.draw.rect(self.screen, (0, 255, 0), (segment.x * self.grid_size, segment.y * self.grid_size, self.grid_size - 2, self.grid_size - 2)) # 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)) if self.game_over: font = pygame.font.Font(None, 72) game_over_text = font.render('Game Over!', True, (255, 255, 255)) text_rect = game_over_text.get_rect(center=(self.width/2, self.height/2)) self.screen.blit(game_over_text, text_rect) pygame.display.flip() def run(self): while True: if not self.handle_input(): break self.move_snake() self.draw() self.clock.tick(10) pygame.quit() if __name__ == '__main__': game = SnakeGame() game.run()
Play Game