snake_game.py 3.7 KB

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