-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake_Game.py
204 lines (163 loc) · 5.68 KB
/
Snake_Game.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Snake? Snaaaaaaake!
import pygame
import time
import random
pygame.init()
#colors
background = (250,223,173)
snake = (0,192,163)
food = (205,25,25)
type_color = (255, 170, 200)
#window details
dis_width = 800
dis_height = 600
dis = pygame.display.set_mode((dis_width,dis_height))
pygame.display.set_caption("Snake Game by Griffyboi")
#snake body
def ourSnake(block, snakeList):
for x in snakeList:
pygame.draw.rect(dis, snake, [x[0], x[1], block, block])
#font
font_style = pygame.font.SysFont(None, 50)
#message positions
position = {'loser': [20, 150], 'endGame': [20, 200], 'highScores': [20, 300], 'score1': [20, 350], 'score2': [20, 400], 'score3': [20,450], 'scoreCounter': [10, 10]}
def messages(msg, color, pos):
msg = font_style.render(msg, True, color)
dis.blit(msg, position[pos])
#game loop and variabes
def gameLoop():
game_over = False
game_lose = False
write_score = False
x1 = dis_width/2
y1 = dis_height/2
x1_change = 0
y1_change = 0
block = 10
length = 1
score = 0
snakeList = []
scoreList = []
#food
foodx = round(random.randrange(0, dis_width - block)/10)*10
foody = round(random.randrange(0, dis_height - block)/10)*10
#speeds
clock = pygame.time.Clock()
snake_speed = 30
#main while loops
while not game_over:
while game_lose:
while not write_score:
#write high scores
scoreFile = open("Highscores.txt", "a")
scoreFile.writelines(str(score) + "\n")
write_score = True
#read high scores
scoreFile = open("Highscores.txt", "r")
scoreList = scoreFile.readlines()
for i in range(0, len(scoreList)):
scoreList[i] = int(scoreList[i])
scoreList.sort()
scoreList.reverse()
#message content
text = ['Loser.', 'Press Any Key to try again or ESC to quit.', 'High Scores:', str(scoreList[0]), str(scoreList[1]), str(scoreList[2]), 'Score:']
pygame.display.update()
#message for-loop
for (words, keys) in zip(text, position):
messages(words, type_color, keys)
pygame.display.update()
#game over input
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
game_over = True
game_lose = False
else:
gameLoop()
#keybindings
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
game_lose = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
x1_change = -10
y1_change = 0
elif event.key == pygame.K_d:
x1_change = 10
y1_change = 0
elif event.key == pygame.K_w:
x1_change = 0
y1_change = -10
elif event.key == pygame.K_s:
x1_change = 0
y1_change = 10
elif event.key == pygame.K_ESCAPE:
game_over = True
game_lose = False
#failstate
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
game_lose = True
#location of snake
x1 += x1_change
y1 += y1_change
#draw to display
dis.fill(background)
pygame.draw.rect(dis, food, [foodx, foody, block, block])
pygame.draw.rect(dis, snake,[x1, y1, block, block])
snakeHead = []
snakeHead.append(x1)
snakeHead.append(y1)
snakeList.append(snakeHead)
#snake length
if len(snakeList) > length:
del snakeList[0]
for x in snakeList[:-1]:
if x == snakeHead:
game_lose = True
#snake details
ourSnake(block, snakeList)
#food and score counters
messages("Score: "+str(score), type_color, 'scoreCounter')
if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, dis_width - block)/10)*10
foody = round(random.randrange(0, dis_height - block)/10)*10
length += 1
score += 1
pygame.display.update()
clock.tick(snake_speed)
#playthrough terminus
pygame.quit()
quit()
gameLoop()
scoreFile.close()
# OLD CODE
# scoreList = [scores.strip('\n') for scores in scoreList]
# def scoreboard(msg, color, dis_width, dis_height):
# msg = font_style.render(msg, True, color)
# dis.blit(msg, [10, 10])
# def loser(msg, color):
# msg = font_style.render(msg, True, color)
# dis.blit(msg, [20, 150])
# def endgame(msg, color):
# msg = font_style.render(msg, True, color)
# dis.blit(msg, [20, 200])
# def highScores(msg, color):
# msg = font_style.render(msg, True, color)
# dis.blit(msg, [20, 300])
# def score1(msg, color):
# msg = font_style.render(msg, True, color)
# dis.blit(msg, [20, 350])
# def score2(msg, color):
# msg = font_style.render(msg, True, color)
# dis.blit(msg, [20, 400])
# def score3(msg, color):
# msg = font_style.render(msg, True, color)
# dis.blit(msg, [20, 450])
# display on game over
# messages("Loser.", type_color, 'loser')
# messages("Press Any Key to try again or ESC to quit.", type_color, 'endGame')
# messages("High Scores:", type_color, 'highScores')
# messages(str(scoreList[0]), type_color, 'score1')
# messages(str(scoreList[1]), type_color, 'score2')
# messages(str(scoreList[2]), type_color, 'score3')