-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
163 lines (135 loc) · 4.91 KB
/
snake.py
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
import pygame, sys, time, random
# Game Over
def gameOver():
text = pygame.font.SysFont('times new roman', 90)
gameOverSurface = text.render('GAME OVER!', True, red)
gameOverRect = gameOverSurface.get_rect()
gameOverRect.midtop = (frameSizeX/2, frameSizeY/4)
gameWindow.fill(black)
gameWindow.blit(gameOverSurface, gameOverRect)
showScore(0, red, 'times', 20)
pygame.display.flip()
time.sleep(3)
pygame.quit()
sys.exit()
# Score
def showScore(choice, color, font, size):
scoreFont = pygame.font.SysFont(font, size)
scoreSurface = scoreFont.render('Score : ' + str(score), True, color)
scoreRect = scoreSurface.get_rect()
if choice == 1:
scoreRect.midtop = (frameSizeX/10, 15)
else:
scoreRect.midtop = (frameSizeX/2, frameSizeY/1.25)
gameWindow.blit(scoreSurface, scoreRect)
# Window size
frameSizeX = 1280
frameSizeY = 720
# Checks for errors encountered
checkErrors = pygame.init()
# pygame.init() example output -> (6, 0)
# second number in tuple gives number of errors
if checkErrors[1] > 0:
print(f'[!] Had {check_errors[1]} errors when initialising game, exiting...')
sys.exit(-1)
else:
print('Game successfully initialised')
# Initialise game window
pygame.display.set_caption('Snake')
gameWindow = pygame.display.set_mode((frameSizeX, frameSizeY))
# Setting up colors (R, G, B)
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
# FPS (frames per second) controller
fpsController = pygame.time.Clock()
# Game variables
snakePos = [100, 50]
snakeBody = [[100, 50], [100-10, 50], [100-(2*10), 50]]
foodPos = [random.randrange(1, (frameSizeX//10)) * 10, random.randrange(1, (frameSizeY//10)) * 10]
foodSpawn = True
direction = 'RIGHT'
changeTo = direction
score = 0
# Difficulty settings
# Easy -> 10
# Medium -> 25
# Hard -> 40
# Harder -> 60
# Insanity -> 100
difficulty = int(input("Please enter a value 10-100 to set the difficulty: "))
# Main logic
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Whenever a key is pressed down
elif event.type == pygame.KEYDOWN:
# W -> Up; S -> Down; A -> Left; D -> Right
if event.key == pygame.K_UP or event.key == ord('w'):
changeTo = 'UP'
if event.key == pygame.K_DOWN or event.key == ord('s'):
changeTo = 'DOWN'
if event.key == pygame.K_LEFT or event.key == ord('a'):
changeTo = 'LEFT'
if event.key == pygame.K_RIGHT or event.key == ord('d'):
changeTo = 'RIGHT'
# Esc -> Create event to quit the game
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
# Making sure the snake cannot move in the opposite direction instantaneously
if changeTo == 'UP' and direction != 'DOWN':
direction = 'UP'
if changeTo == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if changeTo == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if changeTo == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# Moving the snake
if direction == 'UP':
snakePos[1] -= 10
if direction == 'DOWN':
snakePos[1] += 10
if direction == 'LEFT':
snakePos[0] -= 10
if direction == 'RIGHT':
snakePos[0] += 10
# Snake body growing mechanism
snakeBody.insert(0, list(snakePos))
if snakePos[0] == foodPos[0] and snakePos[1] == foodPos[1]:
score += 1
foodSpawn = False
else:
snakeBody.pop()
# Spawning food on the screen
if not foodSpawn:
foodPos = [random.randrange(1, (frameSizeX//10)) * 10, random.randrange(1, (frameSizeY//10)) * 10]
foodSpawn = True
# GFX
gameWindow.fill(black)
for pos in snakeBody:
# Snake body
# .draw.rect(play_surface, color, xy-coordinate)
# xy-coordinate -> .Rect(x, y, size_x, size_y)
pygame.draw.rect(gameWindow, green, pygame.Rect(pos[0], pos[1], 10, 10))
# Snake food
pygame.draw.rect(gameWindow, white, pygame.Rect(foodPos[0], foodPos[1], 10, 10))
# Game Over conditions
# Getting out of bounds
if snakePos[0] < 0 or snakePos[0] > frameSizeX-10:
gameOver()
if snakePos[1] < 0 or snakePos[1] > frameSizeY-10:
gameOver()
# Touching the snake body
for block in snakeBody[1:]:
if snakePos[0] == block[0] and snakePos[1] == block[1]:
gameOver()
showScore(1, white, 'consolas', 20)
# Refresh game screen
pygame.display.update()
# Refresh rate
fpsController.tick(difficulty)