Created
February 2, 2026 05:57
-
-
Save kas940k/9b223a2277f7741e18cecf3340579fcf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tkinter as tk | |
| import random as r | |
| WIDTH = 400 | |
| HEIGHT = 400 | |
| DIRECTIONS = ['Up', 'Down', 'Left', 'Right'] | |
| CELL_SIZE = 10 #size of pixel | |
| DELAY = 100 #speed of game | |
| root = tk.Tk() | |
| root.title('Snake | Core: 0') | |
| root.resizable(False, False) | |
| #create window | |
| canvas = tk.Canvas( | |
| root, #parent_window | |
| width=WIDTH, | |
| height=HEIGHT, | |
| bg='black', #background color | |
| highlightthickness=0 #border | |
| ) | |
| canvas.pack() | |
| #create snake | |
| snake = [(100, 100), (90, 100), (80, 100)] #start_pos | |
| direction = 'Right' #start_direction | |
| food = None | |
| score = 0 | |
| game_over = False | |
| #create food | |
| def create_food(): | |
| while True: | |
| x = r.randint(0, (WIDTH - CELL_SIZE) // CELL_SIZE) * CELL_SIZE | |
| y = r.randint(0, (HEIGHT - CELL_SIZE) // CELL_SIZE) * CELL_SIZE | |
| if (x, y) not in snake: | |
| return x, y | |
| food = create_food() | |
| #draw food | |
| def draw_food(): | |
| canvas.create_rectangle( | |
| food[0], food[1], #x1, y1 | |
| food[0] + CELL_SIZE, #x_right | |
| food[1] + CELL_SIZE, #y_down | |
| fill='red', | |
| ) | |
| #score_on_title | |
| def update_title(): | |
| root.title(f'Snake | Score: {score}') | |
| #draw snake | |
| def draw_snake(): | |
| for segment in snake: | |
| canvas.create_rectangle( | |
| segment[0], segment[1], #up_left | |
| segment[0] + CELL_SIZE, #down_right x | |
| segment[1] + CELL_SIZE, #down_right y | |
| fill='green', #snake_color | |
| outline='darkgreen' #snake_board_color | |
| ) | |
| def restart_game(): | |
| global snake, direction, food, score, game_over | |
| # Начальное положение змейки | |
| snake = [(100, 100), (90, 100), (80, 100)] | |
| direction = "Right" | |
| # Новая еда | |
| food = create_food() | |
| # Сброс счёта и статуса | |
| score = 0 | |
| game_over = False | |
| # Очистим холст и обновим | |
| canvas.delete("all") | |
| draw_food() | |
| draw_snake() | |
| update_title() | |
| # Перезапускаем игровой цикл | |
| root.after(DELAY, game_loop) | |
| def on_key_press(event): | |
| global direction | |
| key = event.keysym | |
| if key in DIRECTIONS: | |
| if (key == 'Up' and direction != 'Down' or | |
| key == 'Down' and direction != 'Up' or | |
| key == 'Left' and direction != 'Right' or | |
| key == 'Right' and direction != 'Left'): | |
| direction = key | |
| elif key == 'space' and game_over: | |
| restart_game() | |
| #move_snake forward | |
| def move_snake(): | |
| head_x, head_y = snake[0] | |
| if direction == "Up": | |
| new_head = (head_x, head_y - CELL_SIZE) | |
| elif direction == "Down": | |
| new_head = (head_x, head_y + CELL_SIZE) | |
| elif direction == "Left": | |
| new_head = (head_x - CELL_SIZE, head_y) | |
| elif direction == "Right": | |
| new_head = (head_x + CELL_SIZE, head_y) | |
| snake.insert(0, new_head) | |
| if not check_food_collision(): | |
| snake.pop() | |
| #head on food? | |
| def check_food_collision(): | |
| global food, score | |
| if snake[0] == food: | |
| score += 1 | |
| food = create_food() | |
| return True | |
| return False | |
| #head with wall? | |
| def check_wall_collision(): | |
| head_x, head_y = snake[0] | |
| return (head_x < 0 or head_x >= WIDTH or | |
| head_y < 0 or head_y >= HEIGHT) | |
| #head in body? | |
| def check_self_collision(): | |
| return snake[0] in snake[1:] | |
| #end of game | |
| def end_game(): | |
| global game_over | |
| game_over = True | |
| canvas.create_text( | |
| WIDTH // 2, HEIGHT // 2, | |
| text=f'Game over!\nYour score: {score}\nPress "SPACE" to restart', | |
| fill='white', | |
| font=('Arial', 24) | |
| ) | |
| #game cycle | |
| def game_loop(): | |
| global snake, food, score | |
| if game_over: | |
| return | |
| move_snake() | |
| if check_wall_collision() or check_self_collision(): | |
| end_game() | |
| return | |
| canvas.delete('all') #clear_field before draw | |
| draw_food() | |
| draw_snake() | |
| update_title() | |
| root.after(DELAY, game_loop) #repeat_after | |
| # first_draw | |
| root.bind('<KeyPress>', on_key_press) #key_to_screen | |
| draw_food() | |
| draw_snake() | |
| root.after(DELAY, game_loop) | |
| root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment