-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
169 lines (148 loc) · 5.24 KB
/
snake.py
File metadata and controls
169 lines (148 loc) · 5.24 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import pygame
import random
import time
class Game:
def __init__(self):
# init pygame
pygame.init()
# score
self.score = 0
# set the window
self.window = pygame.display.set_mode((500, 400))
# font
self.font = pygame.font.SysFont("consolas", 20)
# important colors
self.blue = pygame.Color(0, 0, 255)
self.green = pygame.Color(149, 212, 122)
self.red = pygame.Color(255, 255, 255)
self.black = pygame.Color(246, 143, 160)
self.aqua = pygame.Color(0, 176, 178)
self.pink = pygame.Color(232, 17, 35)
# snake speed
self.speed = 15
# head
self.head = [100, 50]
self.snake_blocks = [[100, 50], [90, 50], [80, 50], [70, 50]]
self.dir = "RIGHT"
self.next_dir = self.dir
# fruit
self.eaten = False
# initial fruit position
self.fruit_pos = (random.randrange(1, (500 // 10)) * 10, random.randrange(1, (400 // 10)) * 10)
def show_score(self):
"""
Function that shows the score of the player
"""
score_surface = self.font.render(f"Score {self.score}", True, self.black)
# create rect
score_rect = score_surface.get_rect()
# display score
self.window.blit(score_surface, score_rect)
def spawn_fruit(self):
"""
Function that takes care of the fruit spawning
"""
# choose random position
self.fruit_pos = (random.randrange(1, (500 // 10) * 10), random.randrange(1, (400 // 10)) * 10)
pygame.draw.rect(self.window, self.blue, pygame.Rect(self.fruit_pos[0], self.fruit_pos[1]))
def keyboard(self, key):
"""
Function that takes care of the keyboard dynamics
"""
if key == pygame.K_UP:
self.next_dir = "UP"
if key == pygame.K_DOWN:
self.next_dir = "DOWN"
if key == pygame.K_LEFT:
self.next_dir = "LEFT"
if key == pygame.K_RIGHT:
self.next_dir = "RIGHT"
# make sure that the snake doesnot collide with itself weirdly
if self.next_dir == "UP" and self.dir != "DOWN":
self.dir = "UP"
if self.next_dir == "DOWN" and self.dir != "UP":
self.dir = "DOWN"
if self.next_dir == "RIGHT" and self.dir != "LEFT":
self.dir = "RIGHT"
if self.next_dir == "LEFT" and self.dir != "RIGHT":
self.dir = "LEFT"
def snake_mech(self):
"""
Function that takes care of the snake's mech
Since the size of each of the snake's body pard is 10x10
we'll have to add by tens
"""
pygame.draw.rect(self.window, self.pink, pygame.Rect(self.fruit_pos[0], self.fruit_pos[1], 10, 10))
# increment by ten the move
self.snake_blocks.insert(0, list(self.head))
# increment if snake ate
if self.head[0] == self.fruit_pos[0] and self.head[0] == self.fruit_pos[1]:
self.score += 10
self.eaten = True
else:
self.snake_blocks.pop()
for pos in self.snake_blocks:
pygame.draw.rect(self.window, self.aqua, pygame.Rect(pos[0], pos[1], 10, 10))
# move the snake
if self.dir == "UP":
self.head[1] -= 10
if self.dir == "DOWN":
self.head[1] += 10
if self.dir == "RIGHT":
self.head[0] += 10
if self.dir == "LEFT":
self.head[0] -= 10
# check if snake is in bounds
if self.head[0] < 0 or self.head[0] > 500 - 10:
self.game_over()
if self.head[1] < 0 or self.head[0] > 400 - 10:
self.game_over()
# check if we lost
for block in self.snake_blocks[1:]:
if self.head[0] == block[0] and self.head[1] == block[1]:
self.game_over()
def game_over(self):
"""
Function that takes care of losing
"""
# create text
game_over_surface = self.font.render("YOU LOST :(", True, self.red)
# create rectangle to hold text
game_over_rect = game_over_surface.get_rect()
# set position of text
game_over_rect.midtop = (500 / 2, 400 / 2)
# write in screen
self.window.blit(game_over_surface, game_over_rect)
pygame.display.flip()
# after two seconds quit game
time.sleep(2)
# quit
pygame.quit()
def start(self):
"""
Function that takes care of the game
"""
# init window
pygame.display.set_caption("Snake Game :D")
# fps control
fps = pygame.time.Clock()
while True:
self.window.fill(self.green)
# display score
self.show_score()
# check for events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
self.keyboard(event.key)
# snake mech
self.snake_mech()
# check if fruit eaten
if self.eaten:
self.spawn_fruit()
self.eaten = False
# refresh
pygame.display.update()
fps.tick(self.speed)
if __name__ == "__main__":
game = Game()
game.start()