Amazing-Python-Scripts
172 строки · 3.7 Кб
1from tkinter import *
2import random
3
4GAME_WIDTH = 600
5GAME_HEIGHT = 600
6SPEED = 100
7SPACE_SIZE = 20
8BODY_PARTS = 3
9SNAKE_COLOUR = "#00FF00"
10FOOD_COLOUR = "#FF0000"
11BACKGROUND_COLOUR = "#000000"
12
13
14class Snake:
15
16def __init__(self):
17self.body_size = BODY_PARTS
18self.coordinates = []
19self.squares = []
20
21for i in range(0, BODY_PARTS):
22self.coordinates.append([0, 0])
23
24for x, y in self.coordinates:
25square = canvas.create_rectangle(
26x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOUR, tag="snake")
27self.squares.append(square)
28
29
30class Food:
31
32def __init__(self):
33
34x = random.randint(0, (GAME_WIDTH/SPACE_SIZE)-1) * SPACE_SIZE
35y = random.randint(0, (GAME_HEIGHT/SPACE_SIZE)-1) * SPACE_SIZE
36
37self.coordinates = [x, y]
38
39canvas.create_oval(x, y, x + SPACE_SIZE, y+SPACE_SIZE,
40fill=FOOD_COLOUR, tag="food")
41
42
43def next_turn(snake, food):
44
45x, y = snake.coordinates[0]
46
47if direction == 'up':
48y -= SPACE_SIZE
49
50elif direction == 'down':
51y += SPACE_SIZE
52
53elif direction == 'left':
54x -= SPACE_SIZE
55
56elif direction == 'right':
57x += SPACE_SIZE
58
59snake.coordinates.insert(0, (x, y))
60
61square = canvas.create_rectangle(
62x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOUR)
63
64snake.squares.insert(0, square)
65
66if x == food.coordinates[0] and y == food.coordinates[1]:
67
68global score
69
70score += 1
71
72label.config(text="Score: {}".format(score))
73
74canvas.delete("food")
75
76food = Food()
77
78else:
79del snake.coordinates[-1]
80
81canvas.delete(snake.squares[-1])
82
83del snake.squares[-1]
84
85if check_collisons(snake):
86game_over()
87
88else:
89window.after(SPEED, next_turn, snake, food)
90
91
92def change_direction(new_direction):
93
94global direction
95
96if new_direction == 'left':
97if direction != 'right':
98direction = new_direction
99
100elif new_direction == 'right':
101if direction != 'left':
102direction = new_direction
103
104if new_direction == 'up':
105if direction != 'down':
106direction = new_direction
107
108if new_direction == 'down':
109if direction != 'up':
110direction = new_direction
111
112
113def check_collisons(snake):
114x, y = snake.coordinates[0]
115
116if x < 0 or x >= GAME_WIDTH:
117return True
118elif y < 0 or y >= GAME_HEIGHT:
119return True
120
121for body_part in snake.coordinates[1:]:
122if x == body_part[0] and y == body_part[1]:
123return True
124
125return False
126
127
128def game_over():
129# canvas.delete(ALL)
130canvas.create_text(canvas.winfo_width()/2, canvas.winfo_height()/2,
131font=('consolas', 70), text="GAME OVER", fill="red", tag="gameover")
132
133reset_button = Button(text="Restart", font=('consolas', 20), command=reset)
134canvas.create_window(300, 400, window=reset_button)
135
136
137def reset():
138score = 0
139label.config(text="Score:{}".format(score))
140canvas.delete(ALL)
141
142snake = Snake()
143food = Food()
144next_turn(snake, food)
145
146
147window = Tk()
148window.title("Snake Game")
149window.resizable(False, False)
150
151score = 0
152direction = 'down'
153
154label = Label(window, text="Score:{}".format(score), font=('consolas', 40))
155label.pack()
156
157canvas = Canvas(window, bg=BACKGROUND_COLOUR,
158height=GAME_HEIGHT, width=GAME_WIDTH)
159canvas.pack()
160
161window.bind('<Left>', lambda event: change_direction('left'))
162window.bind('<Right>', lambda event: change_direction('right'))
163window.bind('<Up>', lambda event: change_direction('up'))
164window.bind('<Down>', lambda event: change_direction('down'))
165
166snake = Snake()
167
168food = Food()
169
170next_turn(snake, food)
171
172window.mainloop()
173