Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- import random
- # Initialize the game interface
- screen = curses.initscr()
- curses.curs_set(0)
- screen_height, screen_width = screen.getmaxyx()
- # Initialize the game board
- game_board = [[' ' for x in range(screen_width)] for y in range(screen_height)]
- snake = [(screen_height // 2, screen_width // 2)]
- food = (random.randint(1, screen_height - 2), random.randint(1, screen_width - 2))
- # Define the game loop
- while True:
- # Handle user input
- key = screen.getch()
- if key == curses.KEY_UP:
- snake.insert(0, (snake[0][0] - 1, snake[0][1]))
- elif key == curses.KEY_DOWN:
- snake.insert(0, (snake[0][0] + 1, snake[0][1]))
- elif key == curses.KEY_LEFT:
- snake.insert(0, (snake[0][0], snake[0][1] - 1))
- elif key == curses.KEY_RIGHT:
- snake.insert(0, (snake[0][0], snake[0][1] + 1))
- # Update the game state
- if snake[0] == food:
- food = (random.randint(1, screen_height - 2), random.randint(1, screen_width - 2))
- else:
- snake.pop()
- # Check for collisions
- if (snake[0][0] == 0 or snake[0][0] == screen_height - 1 or
- snake[0][1] == 0 or snake[0][1] == screen_width - 1 or
- snake[0] in snake[1:]):
- curses.endwin()
- quit()
- # Redraw the game board
- screen.clear()
- for y in range(screen_height):
- for x in range(screen_width):
- if (y, x) in snake:
- screen.addch(y, x, 'O')
- elif (y, x) == food:
- screen.addch(y, x, 'X')
- else:
- screen.addch(y, x, game_board[y][x])
- screen.refresh()
- curses.napms(100)
- # Clean up the game interface
- curses.endwin()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement