Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.
1937 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import random
... import curses
...
... # Initialize the screen
... stdscr = curses.initscr()
... curses.curs_set(0)
... sh, sw = stdscr.getmaxyx()
... w = curses.newwin(sh, sw, 0, 0)
... w.keypad(1)
... w.timeout(100)
...
... # Initial snake position and direction
... snk_x = sw // 4
... snk_y = sh // 2
... snake = [
... [snk_y, snk_x],
... [snk_y, snk_x - 1],
... [snk_y, snk_x - 2]
... ]
...
... # Initial food position
... food = [sh // 2, sw // 2]
... w.addch(food[0], food[1], curses.ACS_PI)
...
... # Snake initial direction
... key = curses.KEY_RIGHT
...
... # Game logic
... while True:
... next_key = w.getch()
... key = key if next_key == -1 else next_key
...
... # Check if the snake hits the wall or itself
... if (snake[0][0] in [0, sh]) or (snake[0][1] in [0, sw]) or (snake[0] in
snake[1:]):
... curses.endwin()
... quit()
# Determine new head of the snake based on the current direction
new_head = [snake[0][0], snake[0][1]]
# Change direction based on the input key
if key == curses.KEY_DOWN:
new_head[0] += 1
if key == curses.KEY_UP:
new_head[0] -= 1
if key == curses.KEY_LEFT:
new_head[1] -= 1
if key == curses.KEY_RIGHT:
new_head[1] += 1
snake.insert(0, new_head)
# Check if snake has eaten the food
if snake[0] == food:
food = None
while food is None:
nf = [
random.randint(1, sh - 1),
random.randint(1, sw - 1)
]
food = nf if nf not in snake else None
w.addch(food[0], food[1], curses.ACS_PI)
else:
tail = snake.pop()
w.addch(tail[0], tail[1], ' ')
# Draw the snake