|
1 | 1 | import random
|
2 | 2 | import curses
|
3 | 3 |
|
| 4 | +# Constants |
| 5 | +INITIAL_SNAKE_LENGTH = 3 |
| 6 | +SNAKE_HEAD_CHAR = curses.ACS_CKBOARD |
| 7 | +FOOD_CHAR = curses.ACS_PI |
| 8 | +TIMEOUT = 100 |
| 9 | + |
| 10 | +def create_initial_snake(screen_width, screen_height): |
| 11 | + snake_x = screen_width / 4 |
| 12 | + snake_y = screen_height / 2 |
| 13 | + return [[snake_y, snake_x - i] for i in range(INITIAL_SNAKE_LENGTH)] |
| 14 | + |
| 15 | +def create_food(snake, screen_width, screen_height): |
| 16 | + food = None |
| 17 | + while food is None: |
| 18 | + new_food = [random.randint(1, screen_height - 1), random.randint(1, screen_width - 1)] |
| 19 | + food = new_food if new_food not in snake else None |
| 20 | + return food |
| 21 | + |
| 22 | +def move_snake(snake, key): |
| 23 | + new_head = [snake[0][0], snake[0][1]] |
| 24 | + |
| 25 | + if key == curses.KEY_DOWN: |
| 26 | + new_head[0] += 1 |
| 27 | + elif key == curses.KEY_UP: |
| 28 | + new_head[0] -= 1 |
| 29 | + elif key == curses.KEY_LEFT: |
| 30 | + new_head[1] -= 1 |
| 31 | + elif key == curses.KEY_RIGHT: |
| 32 | + new_head[1] += 1 |
| 33 | + |
| 34 | + snake.insert(0, new_head) |
| 35 | + |
| 36 | +def check_collision(snake, screen_height, screen_width): |
| 37 | + if snake[0][0] in [0, screen_height] or snake[0][1] in [0, screen_width] or snake[0] in snake[1:]: |
| 38 | + return True |
| 39 | + return False |
| 40 | + |
| 41 | +def update_screen(window, snake, food, ate_food): |
| 42 | + if not ate_food: |
| 43 | + tail = snake.pop() |
| 44 | + window.addch(int(tail[0]), int(tail[1]), " ") |
| 45 | + window.addch(int(snake[0][0]), int(snake[0][1]), SNAKE_HEAD_CHAR) |
| 46 | + if ate_food: |
| 47 | + window.addch(food[0], food[1], FOOD_CHAR) |
4 | 48 |
|
5 | 49 | def play_game():
|
6 |
| - s = curses.initscr() |
| 50 | + screen = curses.initscr() |
7 | 51 | curses.curs_set(0)
|
8 |
| - sh, sw = s.getmaxyx() |
9 |
| - w = curses.newwin(sh, sw, 0, 0) |
10 |
| - w.keypad(1) |
11 |
| - w.timeout(100) |
12 |
| - |
13 |
| - snk_x = sw / 4 |
14 |
| - snk_y = sh / 2 |
15 |
| - snake = [[snk_y, snk_x], [snk_y, snk_x - 1], [snk_y, snk_x - 2]] |
| 52 | + screen_height, screen_width = screen.getmaxyx() |
| 53 | + window = curses.newwin(screen_height, screen_width, 0, 0) |
| 54 | + window.keypad(1) |
| 55 | + window.timeout(TIMEOUT) |
16 | 56 |
|
17 |
| - food = [sh / 2, sw / 2] |
18 |
| - w.addch(int(food[0]), int(food[1]), curses.ACS_PI) |
| 57 | + snake = create_initial_snake(screen_width, screen_height) |
| 58 | + food = create_food(snake, screen_width, screen_height) |
| 59 | + window.addch(int(food[0]), int(food[1]), FOOD_CHAR) |
19 | 60 |
|
20 | 61 | key = curses.KEY_RIGHT
|
21 | 62 |
|
22 | 63 | while True:
|
23 |
| - next_key = w.getch() |
| 64 | + next_key = window.getch() |
24 | 65 | key = key if next_key == -1 else next_key
|
25 | 66 |
|
26 |
| - if snake[0][0] in [0, sh] or snake[0][1] in [0, sw] or snake[0] in snake[1:]: |
| 67 | + if check_collision(snake, screen_height, screen_width): |
27 | 68 | curses.endwin()
|
28 | 69 | quit()
|
29 | 70 |
|
30 |
| - new_head = [snake[0][0], snake[0][1]] |
31 |
| - |
32 |
| - if key == curses.KEY_DOWN: |
33 |
| - new_head[0] += 1 |
34 |
| - if key == curses.KEY_UP: |
35 |
| - new_head[0] -= 1 |
36 |
| - if key == curses.KEY_LEFT: |
37 |
| - new_head[1] -= 1 |
38 |
| - if key == curses.KEY_RIGHT: |
39 |
| - new_head[1] += 1 |
40 |
| - |
41 |
| - snake.insert(0, new_head) |
| 71 | + move_snake(snake, key) |
42 | 72 |
|
43 |
| - if snake[0] == food: |
44 |
| - food = None |
45 |
| - while food is None: |
46 |
| - nf = [random.randint(1, sh - 1), random.randint(1, sw - 1)] |
47 |
| - food = nf if nf not in snake else None |
48 |
| - w.addch(food[0], food[1], curses.ACS_PI) |
49 |
| - else: |
50 |
| - tail = snake.pop() |
51 |
| - w.addch(int(tail[0]), int(tail[1]), " ") |
| 73 | + ate_food = snake[0] == food |
| 74 | + if ate_food: |
| 75 | + food = create_food(snake, screen_width, screen_height) |
52 | 76 |
|
53 |
| - w.addch(int(snake[0][0]), int(snake[0][1]), curses.ACS_CKBOARD) |
| 77 | + update_screen(window, snake, food, ate_food) |
54 | 78 |
|
55 | 79 |
|
56 |
| -user_input = input("Do you want to play the snake game, type 'yes' or 'no': ").lower() |
| 80 | +def main(): |
| 81 | + user_input = input("Do you want to play the snake game, type 'yes' or 'no': ").lower() |
| 82 | + if user_input == "yes": |
| 83 | + play_game() |
| 84 | + else: |
| 85 | + quit() |
57 | 86 |
|
58 |
| -if user_input == "yes": |
59 |
| - play_game() |
60 |
| -else: |
61 |
| - quit() |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments