|
| 1 | +from tkinter import * |
| 2 | +import random |
| 3 | + |
| 4 | +GAME_WIDTH = 700 |
| 5 | +GAME_HEIGHT = 700 |
| 6 | +SPEED = 100 |
| 7 | +SPACE_SIZE = 50 |
| 8 | +BODY_PARTS = 3 |
| 9 | +SNAKE_COLOR = "#00FF00" |
| 10 | +FOOD_COLOR = "#FF0000" |
| 11 | +BACKGROUND_COLOR = "#000000" |
| 12 | + |
| 13 | +class Snake: |
| 14 | + def __init__(self): |
| 15 | + self.body_size = BODY_PARTS |
| 16 | + self.coordinates = [] |
| 17 | + self.squares = [] |
| 18 | + |
| 19 | + for i in range(0, BODY_PARTS): |
| 20 | + self.coordinates.append([0, 0]) |
| 21 | + |
| 22 | + for x, y in self.coordinates: |
| 23 | + square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR, tag="snake") |
| 24 | + self.squares.append(square) |
| 25 | + |
| 26 | +class Food: |
| 27 | + def __init__(self): |
| 28 | + x = random.randint(0, (GAME_WIDTH/SPACE_SIZE)-1) * SPACE_SIZE |
| 29 | + y = random.randint(0, (GAME_HEIGHT/SPACE_SIZE)-1) * SPACE_SIZE |
| 30 | + self.coordinates = [x, y] |
| 31 | + canvas.create_oval(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOR, tag="food") |
| 32 | + |
| 33 | +def next_turn(snake, food): |
| 34 | + x, y = snake.coordinates[0] |
| 35 | + |
| 36 | + if direction == "up": |
| 37 | + y -= SPACE_SIZE |
| 38 | + elif direction == "down": |
| 39 | + y += SPACE_SIZE |
| 40 | + elif direction == "left": |
| 41 | + x -= SPACE_SIZE |
| 42 | + elif direction == "right": |
| 43 | + x += SPACE_SIZE |
| 44 | + |
| 45 | + snake.coordinates.insert(0, (x, y)) |
| 46 | + |
| 47 | + square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR) |
| 48 | + |
| 49 | + snake.squares.insert(0, square) |
| 50 | + |
| 51 | + if x == food.coordinates[0] and y == food.coordinates[1]: |
| 52 | + global score |
| 53 | + score += 1 |
| 54 | + label.config(text="Score:{}".format(score)) |
| 55 | + canvas.delete("food") |
| 56 | + food = Food() |
| 57 | + else: |
| 58 | + del snake.coordinates[-1] |
| 59 | + canvas.delete(snake.squares[-1]) |
| 60 | + del snake.squares[-1] |
| 61 | + |
| 62 | + if check_collisions(snake): |
| 63 | + game_over() |
| 64 | + else: |
| 65 | + window.after(SPEED, next_turn, snake, food) |
| 66 | + |
| 67 | +def change_direction(new_direction): |
| 68 | + global direction |
| 69 | + if new_direction == 'left': |
| 70 | + if direction != 'right': |
| 71 | + direction = new_direction |
| 72 | + elif new_direction == 'right': |
| 73 | + if direction != 'left': |
| 74 | + direction = new_direction |
| 75 | + elif new_direction == 'up': |
| 76 | + if direction != 'down': |
| 77 | + direction = new_direction |
| 78 | + elif new_direction == 'down': |
| 79 | + if direction != 'up': |
| 80 | + direction = new_direction |
| 81 | + |
| 82 | +def check_collisions(snake): |
| 83 | + x, y = snake.coordinates[0] |
| 84 | + if x < 0 or x >= GAME_WIDTH: |
| 85 | + return True |
| 86 | + elif y < 0 or y >= GAME_HEIGHT: |
| 87 | + return True |
| 88 | + for body_part in snake.coordinates[1:]: |
| 89 | + if x == body_part[0] and y == body_part[1]: |
| 90 | + return True |
| 91 | + return False |
| 92 | + |
| 93 | +def game_over(): |
| 94 | + canvas.delete(ALL) |
| 95 | + canvas.create_text(canvas.winfo_width() / 2, canvas.winfo_height() / 2, font=('consolas', 70), |
| 96 | + text="GAME OVER", fill="red", tag="gameover") |
| 97 | + canvas.create_text(canvas.winfo_width() / 2, canvas.winfo_height() / 2 + 100, font=('consolas', 30), |
| 98 | + text="Press R to restart", fill="white", tag="restart") |
| 99 | + window.bind('r', restart_game) |
| 100 | + |
| 101 | +def restart_game(event=None): |
| 102 | + global snake, food, score, direction |
| 103 | + canvas.delete(ALL) |
| 104 | + snake = Snake() |
| 105 | + food = Food() |
| 106 | + score = 0 |
| 107 | + direction = 'down' |
| 108 | + label.config(text="Score:{}".format(score)) |
| 109 | + window.after(SPEED, next_turn, snake, food) |
| 110 | + |
| 111 | +window = Tk() |
| 112 | +window.title("Snake Game") |
| 113 | +window.resizable(False, False) |
| 114 | + |
| 115 | +score = 0 |
| 116 | +direction = 'down' |
| 117 | + |
| 118 | +label = Label(window, text="Score:{}".format(score), font=('consolas', 40)) |
| 119 | +label.pack() |
| 120 | + |
| 121 | +canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH) |
| 122 | +canvas.pack() |
| 123 | + |
| 124 | +window.update() |
| 125 | + |
| 126 | +window_width = window.winfo_width() |
| 127 | +window_height = window.winfo_height() |
| 128 | +screen_width = window.winfo_screenwidth() |
| 129 | +screen_height = window.winfo_screenheight() |
| 130 | + |
| 131 | +x = int((screen_width/2) - (window_width/2)) |
| 132 | +y = int((screen_height/2) - (window_height/2)) |
| 133 | + |
| 134 | +window.geometry(f"{window_width}x{window_height}+{x}+{y}") |
| 135 | + |
| 136 | +window.bind('<Left>', lambda event: change_direction('left')) |
| 137 | +window.bind('<Right>', lambda event: change_direction('right')) |
| 138 | +window.bind('<Up>', lambda event: change_direction('up')) |
| 139 | +window.bind('<Down>', lambda event: change_direction('down')) |
| 140 | + |
| 141 | +snake = Snake() |
| 142 | +food = Food() |
| 143 | + |
| 144 | +next_turn(snake, food) |
| 145 | + |
| 146 | +window.mainloop() |
0 commit comments