-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake game.py
252 lines (221 loc) · 7.37 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#########################################
# Programmer: Michael Roudnitski
# Date: 21/09/2012
# File Name: snake_template.py
# Description: This program is a template for Snake Game.
# It demonstrates how to move and lengthen the snake.
#########################################
import time
from random import randint
import pygame
pygame.init()
intro = True
inPlay = False
HEIGHT = 600
WIDTH = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
WHITE = (255, 255, 255)
BLACK = (40, 40, 4)
RED = (231, 76, 60)
GREEN = (0, 255, 0)
BLUE = (44, 62, 80)
ORANGE = (241, 196, 15)
outline = 0
score = 1
appleEaten = True
difficulty = 50
font = pygame.font.SysFont("Myriad", 60)
introFont = pygame.font.SysFont("Myriad", 36)
appleTimer = 5000
gameTimer = 10000
#---------------------------------------#
# snake's properties #
#---------------------------------------#
BODY_SIZE = 15
APPLE_SIZE = 15
HSPEED = 20
VSPEED = 20
enemyCLR = (155, 89, 182)
enemyXspeed = 20
enemyX = WIDTH/2 - 15
enemyYspeed = 0
gravity =0.98
enemyY = 300
speedX = 0
speedY = -VSPEED
segx = [int(WIDTH/2.)]*3
segy = [HEIGHT, HEIGHT+VSPEED, HEIGHT+2*VSPEED]
timerColor = WHITE
appleX = [WIDTH]
appleY = [HEIGHT]
#---------------------------------------#
# function that redraws all objects #
#---------------------------------------#
def redraw_screen():
screen.fill(BLUE)
# Score Text
scoreText = font.render(str(score - 1), 1, WHITE)
screen.blit(scoreText, (10, 10))
#Timer Text
timerText = font.render(str(int(gameTimer/1000)), 1, timerColor)
screen.blit(timerText, (750, 10))
for i in range(len(segx)): #draw snake
pygame.draw.rect(screen, ORANGE, (segx[i], segy[i], BODY_SIZE, BODY_SIZE)) #head
pygame.draw.rect(screen, RED, (segx[0], segy[0], BODY_SIZE, BODY_SIZE)) #body
for i in range(len(appleX)): #draw apples
if appleEaten is False:
pygame.draw.rect(screen, GREEN, (appleX[i], appleY[i], APPLE_SIZE, APPLE_SIZE))
pygame.display.update()
#---------------------------------------#
# the main program begins here #
#---------------------------------------#
FPS = 120
appleSound = pygame.mixer.Sound('PickupCoin.wav')
appleSound.set_volume(8)
introScreen = pygame.image.load("introScreen.jpg")
startButton = pygame.image.load("start.jpg")
startButtonHover = pygame.image.load("startHover.jpg")
exitButton = pygame.image.load("exit.jpg")
exitButtonHover = pygame.image.load("exitHover.jpg")
print("Use the arrow keys.")
print("You have 10 seconds to collect an apple")
print("Hit ESC to end the program.")
enemyW = 30
enemyH = 30
exitButtonX = 500
startButtonX = 0
buttonY = HEIGHT - 250
startClicked = False
exitClicked = False
while intro:
pygame.event.get()
(mouseX, mouseY) = pygame.mouse.get_pos()
keys = pygame.key.get_pressed()
screen.blit(introScreen, (0, 0))
screen.blit(startButton, (startButtonX, buttonY))
screen.blit(exitButton, (exitButtonX, buttonY))
# move enemy
if enemyY >= HEIGHT:
enemyYspeed *= -0.95
if enemyY >= HEIGHT + 30:
enemyYspeed = 0
enemyY = -10
if enemyYspeed > 20:
enemyH += 1
enemyY -= 1
enemyW -= 1
enemyX += 1
elif enemyH > 30 and enemyYspeed < 10:
enemyH -= 2
enemyY += 2
elif enemyW < 30 and enemyYspeed < 10:
enemyW += 2
enemyX -= 2
enemyYspeed += gravity
enemyY += enemyYspeed
pygame.draw.rect(screen, GREEN, (enemyX, enemyY, enemyW, enemyH)) #draw enemy
if mouseX > 0 and mouseX < 300 and mouseY > HEIGHT - 250 and mouseY < HEIGHT - 200:
screen.blit(startButtonHover, (startButtonX, buttonY))
if pygame.mouse.get_pressed()[0]:
startButtonX += 20
startClicked = True
if startClicked is True:
startButtonX -= 15
if startButtonX <= -300:
intro = False
inPlay = True
clock = pygame.time.Clock()
if mouseX > 500 and mouseX < WIDTH and mouseY > HEIGHT - 250 and mouseY < HEIGHT - 200:
screen.blit(exitButtonHover, (exitButtonX, buttonY))
if pygame.mouse.get_pressed()[0]:
exitClicked = True
exitButtonX -= 20
if exitClicked is True:
exitButtonX += 15
if exitButtonX >= WIDTH:
intro = False
if keys[pygame.K_ESCAPE]:
intro = False
if keys[pygame.K_SPACE]:
intro = False
inPlay = True
clock = pygame.time.Clock()
pygame.display.update()
while inPlay:
clock.tick(FPS)
# check for events
pygame.event.get()
keys = pygame.key.get_pressed()
if segx[0] <= 0:
inPlay = False
elif segx[0] >= WIDTH:
inPlay = False
elif segy[0] <= 0:
inPlay = False
elif segy[0] >= (HEIGHT + 1):
inPlay = False
# act upon key events
if keys[pygame.K_ESCAPE]:
inPlay = False
if keys[pygame.K_LEFT] and speedX != HSPEED:
speedX = -HSPEED
speedY = 0
if keys[pygame.K_RIGHT] and speedX != -HSPEED:
speedX = HSPEED
speedY = 0
if keys[pygame.K_UP] and speedY != VSPEED:
speedX = 0
speedY = -VSPEED
if keys[pygame.K_DOWN]and speedY != -VSPEED:
speedX = 0
speedY = VSPEED
# move all segments
for i in range(len(segx)-1, 0, -1): # start from the tail, and go backwards:
segx[i] = segx[i-1] # every segment takes the coordinates
segy[i] = segy[i-1] # of the previous one
# move the head
segx[0] += speedX
segy[0] += speedY
# detect if the snake eats itself
for i in range(len(segx)-1, 0, -1):
if segx[0] == segx[i] and segy[0] == segy[i]:
inPlay = False
# apple timer
appleTimer -= clock.get_time()
if appleTimer <= 0:
appleEaten = True
appleTimer = 5000
# apple position generator
for i in range(len(appleX)):
if appleEaten is True:
appleXGrid = (randint(1, 38))
appleX.append(HSPEED*appleXGrid)
appleYGrid = (randint(1, 28))
appleY.append(VSPEED*appleYGrid)
appleEaten = False
# detect if snake eats apple
if segx[0] == appleX[i] and segy[0] == appleY[i]:
appleSound.play()
appleX[i] = (HSPEED*randint(1, 38))
appleY[i] = (HSPEED*randint(1, 28))
segx.append(segx[-1])
segy.append(segy[-1])
score += 1
gameTimer = 10000
difficultyTF = True
timerColor = WHITE
# game timer
gameTimer -= clock.get_time()
if gameTimer <= 4000: # changes timer color to red if there are 3 seconds left
timerColor = RED
if gameTimer <= 1000:
inPlay = False
# increase difficulty
if score % 3 == 0 and difficultyTF is True:
difficulty -= 2
difficultyTF = False
# update the screen
redraw_screen()
pygame.time.delay(difficulty)
print("Your score was", score - 1)
pygame.quit() # always quit pygame when done!