-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake_game_(gui).py
More file actions
127 lines (103 loc) · 3.35 KB
/
snake_game_(gui).py
File metadata and controls
127 lines (103 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from turtle import Turtle, Screen
import random
import time
starting_positions = [(0, 0), (-20, 0), (-40, 0)]
snake_parts = []
class Snake:
def __init__(self):
self.snake_parts = []
self.create_snake()
def create_snake(self):
for i in starting_positions:
self.add_segment(i)
def add_segment(self, i):
t1 = Turtle("square")
t1.color("white")
t1.penup()
t1.goto(i)
self.snake_parts.append(t1)
def extend(self):
#this method adds a new segment to the snake
self.add_segment(self.snake_parts[-1].position())
def move(self):
for i in range(len(self.snake_parts) - 1, 0, -1):
self.snake_parts[i].goto(self.snake_parts[i - 1].pos())
self.snake_parts[i].setheading(self.snake_parts[i - 1].heading())
self.snake_parts[0].forward(20)
def up(self):
if self.snake_parts[0].heading() != 270:
self.snake_parts[0].setheading(90)
def down(self):
if self.snake_parts[0].heading() != 90:
self.snake_parts[0].setheading(270)
def right(self):
if self.snake_parts[0].heading() != 180:
self.snake_parts[0].setheading(0)
def left(self):
if self.snake_parts[0].heading() != 0:
self.snake_parts[0].setheading(180)
class Food(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.penup()
self.shapesize(0.5, 0.5)
self.color("blue")
self.speed("fastest")
self.refresh()
def refresh(self):
random_x = random.randint(-280, 280)
random_y = random.randint(-280, 280)
self.goto(random_x, random_y)
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.score = 0
self.color("white")
self.penup()
self.goto(0, 270)
self.speed("fastest")
self.hideturtle()
self.update_score()
def update_score(self):
self.write(f"Score: {self.score}", align="center", font=("Arial", 24, "normal"))
def game_over(self):
self.goto(0, 0)
self.write(f"Game Over!", align="center", font=("Arial", 24, "normal"))
def increase_score(self):
self.score += 1
self.clear()
self.update_score()
screen = Screen()
screen.setup(600, 600)
screen.bgcolor("black")
screen.title("Snake Game")
screen.tracer(0)
snake = Snake()
food = Food()
scoreboard = Scoreboard()
screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
should_continue = True
while should_continue:
screen.update()
time.sleep(0.1)
snake.move()
#collision with the food:
if snake.snake_parts[0].distance(food) < 15:
food.refresh()
snake.extend()
scoreboard.increase_score()
#detecting collision:
if snake.snake_parts[0].xcor() > 280 or snake.snake_parts[0].xcor() < -280 or snake.snake_parts[0].ycor() > 280 or snake.snake_parts[0].ycor() < -280: #this means that snake has hit the wall
should_continue = False
scoreboard.game_over()
#detecting collision with tail:
for i in snake.snake_parts[1:]:
if snake.snake_parts[0].distance(i) < 10:
should_continue = False
scoreboard.game_over()
screen.exitonclick()