I'll create a Snake game with shooter elements in Python using Pygame. This version will have the snake shoot projectiles to destroy obstacles while collecting food.
import pygame
import random
import math
# Initialize Pygame
pygame.init()
# Constants
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
GRID_SIZE = 20
GRID_WIDTH = WINDOW_WIDTH // GRID_SIZE
GRID_HEIGHT = WINDOW_HEIGHT // GRID_SIZE
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
PURPLE = (128, 0, 128)
ORANGE = (255, 165, 0)
# Directions
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
class Snake:
def __init__(self):
self.positions = [(GRID_WIDTH // 2, GRID_HEIGHT // 2)]
self.direction = RIGHT
self.grow_flag = False
self.shoot_cooldown = 0
def move(self):
head = self.positions[0]
new_head = (head[0] + self.direction[0], head[1] + self.direction[1])
# Wrap around screen
new_head = (new_head[0] % GRID_WIDTH, new_head[1] % GRID_HEIGHT)
self.positions.insert(0, new_head)
if not self.grow_flag:
self.positions.pop()
else:
self.grow_flag = False
if self.shoot_cooldown > 0:
self.shoot_cooldown -= 1
def grow(self):
self.grow_flag = True
def change_direction(self, new_direction):
# Prevent snake from going back into itself
if (new_direction[0] * -1, new_direction[1] * -1) != self.direction:
self.direction = new_direction
def check_collision(self):
head = self.positions[0]
# Check self collision
if head in self.positions[1:]:
return True
return False
def can_shoot(self):
return self.shoot_cooldown == 0
def shoot(self):
if self.can_shoot():
self.shoot_cooldown = 10
return Bullet(self.positions[0], self.direction)
return None
class Bullet:
def __init__(self, position, direction):
self.x = position[0] * GRID_SIZE + GRID_SIZE // 2
self.y = position[1] * GRID_SIZE + GRID_SIZE // 2
self.dx = direction[0] * 10
self.dy = direction[1] * 10
self.active = True
def update(self):
self.x += self.dx
self.y += self.dy
# Check if bullet is out of bounds
if (self.x < 0 or self.x > WINDOW_WIDTH or
self.y < 0 or self.y > WINDOW_HEIGHT):
self.active = False
def get_grid_position(self):
return (int(self.x // GRID_SIZE), int(self.y // GRID_SIZE))
class Food:
def __init__(self):
self.position = self.randomize_position()
def randomize_position(self):
return (random.randint(0, GRID_WIDTH - 1),
random.randint(0, GRID_HEIGHT - 1))
class Enemy:
def __init__(self):
self.position = self.randomize_position()
self.move_counter = 0
def randomize_position(self):
return (random.randint(0, GRID_WIDTH - 1),
random.randint(0, GRID_HEIGHT - 1))
def update(self):
self.move_counter += 1
if self.move_counter >= 30: # Move every 30 frames
self.move_counter = 0
# Random movement
direction = random.choice([UP, DOWN, LEFT, RIGHT])
new_pos = (self.position[0] + direction[0],
self.position[1] + direction[1])
# Keep within bounds
new_pos = (max(0, min(GRID_WIDTH - 1, new_pos[0])),
max(0, min(GRID_HEIGHT - 1, new_pos[1])))
self.position = new_pos
class Game:
def __init__(self):
self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Snake Shooter")
self.clock = pygame.time.Clock()
self.font = pygame.font.Font(None, 36)
self.reset_game()
def reset_game(self):
self.snake = Snake()
self.food = Food()
self.enemies = [Enemy() for _ in range(3)]
self.bullets = []
self.score = 0
self.game_over = False
self.power_ups = []
self.power_up_timer = 0
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN:
if self.game_over and event.key == pygame.K_SPACE:
self.reset_game()
elif not self.game_over:
if event.key == pygame.K_UP:
self.snake.change_direction(UP)
elif event.key == pygame.K_DOWN:
self.snake.change_direction(DOWN)
elif event.key == pygame.K_LEFT:
self.snake.change_direction(LEFT)
elif event.key == pygame.K_RIGHT:
self.snake.change_direction(RIGHT)
elif event.key == pygame.K_SPACE:
bullet = self.snake.shoot()
if bullet:
self.bullets.append(bullet)
return True
def update(self):
if self.game_over:
return
# Update snake
self.snake.move()
# Check snake collision with itself
if self.snake.check_collision():
self.game_over = True
return
# Check food collision
if self.snake.positions[0] == self.food.position:
self.snake.grow()
self.score += 10
# Respawn food avoiding snake and enemies
while True:
self.food.position = self.food.randomize_position()
if (self.food.position not in self.snake.positions and
all(self.food.position != enemy.position for enemy in self.enemies)):
break
# Update enemies
for enemy in self.enemies:
enemy.update()
# Check collision with snake head
if enemy.position == self.snake.positions[0]:
self.game_over = True
return
# Update bullets
for bullet in self.bullets[:]:
bullet.update()
if not bullet.active:
self.bullets.remove(bullet)
continue
# Check bullet collision with enemies
bullet_grid_pos = bullet.get_grid_position()
for enemy in self.enemies[:]:
if bullet_grid_pos == enemy.position:
self.enemies.remove(enemy)
self.bullets.remove(bullet)
self.score += 20
# Spawn new enemy after a delay
if len(self.enemies) < 5: # Max 5 enemies
new_enemy = Enemy()
# Make sure enemy doesn't spawn on snake
while new_enemy.position in self.snake.positions:
new_enemy.position = new_enemy.randomize_position()
self.enemies.append(new_enemy)
break
# Spawn power-ups occasionally
self.power_up_timer += 1
if self.power_up_timer > 300 and len(self.power_ups) < 2: # Every 5 seconds
self.power_up_timer = 0
if random.random() < 0.3: # 30% chance
power_up_pos = (random.randint(0, GRID_WIDTH - 1),
random.randint(0, GRID_HEIGHT - 1))
if (power_up_pos not in self.snake.positions and
all(power_up_pos != enemy.position for enemy in self.enemies)):
self.power_ups.append(power_up_pos)
# Check power-up collision
for power_up in self.power_ups[:]:
if self.snake.positions[0] == power_up:
self.power_ups.remove(power_up)
self.score += 50
# Give rapid fire for a short time
self.snake.shoot_cooldown = 0
def draw(self):
self.screen.fill(BLACK)
# Draw grid lines (optional)
for x in range(0, WINDOW_WIDTH, GRID_SIZE):
pygame.draw.line(self.screen, (20, 20, 20), (x, 0), (x, WINDOW_HEIGHT))
for y in range(0, WINDOW_HEIGHT, GRID_SIZE):
pygame.draw.line(self.screen, (20, 20, 20), (0, y), (WINDOW_WIDTH, y))
# Draw snake
for i, pos in enumerate(self.snake.positions):
color = GREEN if i == 0 else (0, 200, 0) # Head is brighter
pygame.draw.rect(self.screen, color,
(pos[0] * GRID_SIZE, pos[1] * GRID_SIZE,
GRID_SIZE - 2, GRID_SIZE - 2))
# Draw food
pygame.draw.circle(self.screen, RED,
(self.food.position[0] * GRID_SIZE + GRID_SIZE // 2,
self.food.position[1] * GRID_SIZE + GRID_SIZE // 2),
GRID_SIZE // 2 - 2)
# Draw enemies
for enemy in self.enemies:
pygame.draw.rect(self.screen, PURPLE,
(enemy.position[0] * GRID_SIZE + 2,
enemy.position[1] * GRID_SIZE + 2,
GRID_SIZE - 4, GRID_SIZE - 4))
# Draw bullets
for bullet in self.bullets:
pygame.draw.circle(self.screen, YELLOW,
(int(bullet.x), int(bullet.y)), 4)
# Draw power-ups
for power_up in self.power_ups:
pygame.draw.polygon(self.screen, ORANGE,
[(power_up[0] * GRID_SIZE + GRID_SIZE // 2, power_up[1] * GRID_SIZE),
(power_up[0] * GRID_SIZE, power_up[1] * GRID_SIZE + GRID_SIZE),
(power_up[0] * GRID_SIZE + GRID_SIZE, power_up[1] * GRID_SIZE + GRID_SIZE)])
# Draw score
score_text = self.font.render(f"Score: {self.score}", True, WHITE)
self.screen.blit(score_text, (10, 10))
# Draw shoot cooldown indicator
if self.snake.shoot_cooldown > 0:
cooldown_width = int((self.snake.shoot_cooldown / 10) * 100)
pygame.draw.rect(self.screen, BLUE, (10, 50, cooldown_width, 10))
pygame.draw.rect(self.screen, WHITE, (10, 50, 100, 10), 2)
# Draw game over screen
if self.game_over:
game_over_text = self.font.render("GAME OVER", True, RED)
restart_text = self.font.render("Press SPACE to restart", True, WHITE)
text_rect = game_over_text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 - 20))
restart_rect = restart_text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2 + 20))
self.screen.blit(game_over_text, text_rect)
self.screen.blit(restart_text, restart_rect)
pygame.display.flip()
def run(self):
running = True
while running:
running = self.handle_events()
self.update()
self.draw()
self.clock.tick(15) # 15 FPS for classic snake feel
pygame.quit()
if __name__ == "__main__":
game = Game()
game.run()
This Snake game with shooter elements includes:
Game Features:
- Classic snake movement with arrow keys
- Press SPACE to shoot bullets in the direction the snake is facing
- Green snake that grows when eating red food
- Purple enemies that move randomly around the map
- Yellow bullets that destroy enemies for points
- Orange power-ups that appear occasionally for bonus points
- Shooting cooldown system (blue bar indicator)
Scoring:
- Food: 10 points
- Destroying enemies: 20 points
- Power-ups: 50 points
Game Mechanics:
- Snake wraps around screen edges
- Enemies respawn after being destroyed
- Collision with enemies or yourself ends the game
- Maximum of 5 enemies on screen at once
- Power-ups spawn randomly with 30% chance every 5 seconds
To run this game, you'll need to install Pygame:
pip install pygame
The game combines the classic Snake gameplay with shooting mechanics, creating a more action-packed experience where you need to both collect food and defend against enemies.