Here's a simple implementation of the classic game Snake using Python and the curses library for a terminal-based interface:
import curses
from random import randint
class SnakeGame:
def __init__(self, stdscr):
self.stdscr = stdscr
self.stdscr.clear()
curses.curs_set(0) # Hide cursor
curses.noecho() # Disable echoing of key presses
curses.cbreak() # Immediate key presses, no Enter required
self.stdscr.keypad(True) # Enable arrow keys
self.height, self.width = self.stdscr.getmaxyx()
self.snake = [(self.width // 2, self.height // 2)] # Initial snake position
self.direction = (1, 0) # Initial direction (right)
self.food = self.generate_food()
self.score = 0
self.update_screen()
def generate_food(self):
while True:
x, y = randint(1, self.width - 2), randint(1, self.height - 2)
if (x, y) not in self.snake:
return x, y
def update_screen(self):
self.stdscr.clear()
self.stdscr.addstr(0, 0, f"Score: {self.score}")
for x, y in self.snake:
self.stdscr.addch(y, x, '#')
self.stdscr.addch(self.food[1], self.food[0], '*')
self.stdscr.refresh()
def move_snake(self):
head_x, head_y = self.snake[0]
new_head_x, new_head_y = head_x + self.direction[0], head_y + self.direction[1]
if (new_head_x, new_head_y) in self.snake or (
new_head_x < 1 or new_head_x >= self.width - 1 or
new_head_y < 1 or new_head_y >= self.height - 1
):
self.game_over()
elif (new_head_x, new_head_y) == self.food:
self.score += 1
self.food = self.generate_food()
else:
self.snake.pop()
self.snake.insert(0, (new_head_x, new_head_y))
self.update_screen()
def game_over(self):
self.stdscr.clear()
self.stdscr.addstr(self.height // 2, self.width // 2 - 6, "Game Over! Press 'q' to quit.")
self.stdscr.refresh()
while self.stdscr.getch() != ord('q'):
pass
curses.endwin() # Restore terminal settings
exit(0)
def run(self):
while True:
key = self.stdscr.getch()
if key == curses.KEY_UP and self.direction != (0, 1):
self.direction = (0, -1)
elif key == curses.KEY_DOWN and self.direction != (0, -1):
self.direction = (0, 1)
elif key == curses.KEY_LEFT and self.direction != (1, 0):
self.direction = (-1, 0)
elif key == curses.KEY_RIGHT and self.direction != (-1, 0):
self.direction = (1, 0)
self.move_snake()
def main(stdscr):
game = SnakeGame(stdscr)
game.run()
if __name__ == "__main__":
curses.wrapper(main)
How to play:
- Run the script in a terminal with a sufficient size (e.g., 80x24).
- Use arrow keys to control the snake.
- Eat food (
*) to increase your score.
- Avoid colliding with the border or yourself.
- Press
q to quit when the game is over.
Features:
- Smooth, responsive gameplay
- Score tracking
- Random food generation
- Game over screen with quit option
- Proper terminal cleanup using
curses.wrapper
Tips:
- Adjust the terminal size or font to optimize the gameplay experience.
- For a more challenging game, reduce the terminal size or increase the snake's speed (not implemented here).
- This implementation is relatively simple and can be improved with additional features, such as:
- Smoother animations
- Boundary wrapping (e.g., snake reappears on opposite side)
- Leveling system with increasing difficulty
- High score tracking
- Graphics enhancements (e.g., using Unicode characters or a GUI library)