Absolutely! Here’s a fun twist on the classic Snake game: Snake Shooter. In this version, the snake can shoot projectiles to destroy obstacles (or just for fun). We'll use pygame for graphics and input handling.
Features:
- Classic snake movement (arrow keys)
- Snake can shoot bullets (spacebar)
- Bullets move forward and disappear after a while
- Obstacles randomly placed on the field (optional: snake can shoot them)
Prerequisites
Install pygame if you haven't:
pip install pygame
Full Code: Snake Shooter
import pygame
import random
import sys
# Initialize pygame
pygame.init()
# Screen settings
WIDTH, HEIGHT = 600, 400
CELL_SIZE = 20
GRID_WIDTH = WIDTH // CELL_SIZE
GRID_HEIGHT = HEIGHT // CELL_SIZE
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Shooter")
# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
GRAY = (100, 100, 100)
# Directions
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
clock = pygame.time.Clock()
# Snake class
class Snake:
def __init__(self):
self.body = [(GRID_WIDTH // 2, GRID_HEIGHT // 2)]
self.direction = RIGHT
self.grow = False
def move(self):
head_x, head_y = self.body[0]
dx, dy = self.direction
new_head = ((head_x + dx) % GRID_WIDTH, (head_y + dy) % GRID_HEIGHT)
if new_head in self.body:
return False # Collision with self
self.body.insert(0, new_head)
if not self.grow:
self.body.pop()
else:
self.grow = False
return True
def change_direction(self, new_dir):
# Prevent reversing
if (new_dir[0] * -1, new_dir[1] * -1) != self.direction:
self.direction = new_dir
def eat(self):
self.grow = True
# Bullet class
class Bullet:
def __init__(self, pos, direction):
self.x, self.y = pos
self.direction = direction
self.lifetime = 20 # frames
def move(self):
dx, dy = self.direction
self.x = (self.x + dx) % GRID_WIDTH
self.y = (self.y + dy) % GRID_HEIGHT
self.lifetime -= 1
def get_pos(self):
return (self.x, self.y)
# Obstacle class
class Obstacle:
def __init__(self, pos):
self.pos = pos
def random_position(exclude):
while True:
pos = (random.randint(0, GRID_WIDTH - 1), random.randint(0, GRID_HEIGHT - 1))
if pos not in exclude:
return pos
def main():
snake = Snake()
bullets = []
obstacles = [Obstacle(random_position(snake.body)) for _ in range(5)]
food = random_position(snake.body + [o.pos for o in obstacles])
score = 0
running = True
while running:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
snake.change_direction(UP)
elif event.key == pygame.K_DOWN:
snake.change_direction(DOWN)
elif event.key == pygame.K_LEFT:
snake.change_direction(LEFT)
elif event.key == pygame.K_RIGHT:
snake.change_direction(RIGHT)
elif event.key == pygame.K_SPACE:
# Shoot bullet from head in current direction
bullets.append(Bullet(snake.body[0], snake.direction))
if not snake.move():
print("Game Over! Score:", score)
running = False
# Check food collision
if snake.body[0] == food:
snake.eat()
score += 1
food = random_position(snake.body + [o.pos for o in obstacles])
# Move bullets
for bullet in bullets[:]:
bullet.move()
# Remove bullet if lifetime is over
if bullet.lifetime <= 0:
bullets.remove(bullet)
continue
# Check collision with obstacles
for obstacle in obstacles[:]:
if bullet.get_pos() == obstacle.pos:
obstacles.remove(obstacle)
if bullet in bullets:
bullets.remove(bullet)
break
# Check collision with obstacles
for obstacle in obstacles:
if snake.body[0] == obstacle.pos:
print("Game Over! Score:", score)
running = False
# Draw everything
screen.fill(BLACK)
# Draw snake
for x, y in snake.body:
pygame.draw.rect(screen, GREEN, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
# Draw food
fx, fy = food
pygame.draw.rect(screen, YELLOW, (fx * CELL_SIZE, fy * CELL_SIZE, CELL_SIZE, CELL_SIZE))
# Draw bullets
for bullet in bullets:
bx, by = bullet.get_pos()
pygame.draw.rect(screen, RED, (bx * CELL_SIZE + 6, by * CELL_SIZE + 6, 8, 8))
# Draw obstacles
for obstacle in obstacles:
ox, oy = obstacle.pos
pygame.draw.rect(screen, GRAY, (ox * CELL_SIZE, oy * CELL_SIZE, CELL_SIZE, CELL_SIZE))
# Draw score
font = pygame.font.SysFont(None, 24)
score_text = font.render(f"Score: {score}", True, WHITE)
screen.blit(score_text, (5, 5))
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
How to Play
- Arrow keys: Move the snake
- Spacebar: Shoot in the current direction
- Eat yellow food to grow and score points
- Avoid gray obstacles (or shoot them!)
- Game ends if you hit yourself or an obstacle
Let me know if you want more features (like enemies, power-ups, or sound)!