-
Notifications
You must be signed in to change notification settings - Fork 8
/
snake_game.py
135 lines (117 loc) · 3.75 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
# SNAKE GAME
# Author : Apaar Gupta (@apaar97)
# Python 3.5.2 Pygame
import pygame
import sys
import time
import random
# Pygame Init
init_status = pygame.init()
if init_status[1] > 0:
print("(!) Had {0} initialising errors, exiting... ".format(init_status[1]))
sys.exit()
else:
print("(+) Pygame initialised successfully ")
# Play Surface
size = width, height = 640, 320
playSurface = pygame.display.set_mode(size)
pygame.display.set_caption("Snake Game")
# Colors
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
brown = pygame.Color(165, 42, 42)
# FPS controller
fpsController = pygame.time.Clock()
# Game settings
delta = 10
snakePos = [100, 50]
snakeBody = [[100, 50], [90, 50], [80, 50]]
foodPos = [400, 50]
foodSpawn = True
direction = 'RIGHT'
changeto = ''
score = 0
# Game Over
def gameOver():
myFont = pygame.font.SysFont('monaco', 72)
GOsurf = myFont.render("Game Over", True, red)
GOrect = GOsurf.get_rect()
GOrect.midtop = (320, 25)
playSurface.blit(GOsurf, GOrect)
showScore(0)
pygame.display.flip()
time.sleep(4)
pygame.quit()
sys.exit()
# Show Score
def showScore(choice=1):
SFont = pygame.font.SysFont('monaco', 32)
Ssurf = SFont.render("Score : {0}".format(score), True, black)
Srect = Ssurf.get_rect()
if choice == 1:
Srect.midtop = (80, 10)
else:
Srect.midtop = (320, 100)
playSurface.blit(Ssurf, Srect)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
changeto = 'RIGHT'
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
changeto = 'LEFT'
if event.key == pygame.K_UP or event.key == pygame.K_w:
changeto = 'UP'
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
changeto = 'DOWN'
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
# Validate direction
if changeto == 'RIGHT' and direction != 'LEFT':
direction = changeto
if changeto == 'LEFT' and direction != 'RIGHT':
direction = changeto
if changeto == 'UP' and direction != 'DOWN':
direction = changeto
if changeto == 'DOWN' and direction != 'UP':
direction = changeto
# Update snake position
if direction == 'RIGHT':
snakePos[0] += delta
if direction == 'LEFT':
snakePos[0] -= delta
if direction == 'DOWN':
snakePos[1] += delta
if direction == 'UP':
snakePos[1] -= delta
# Snake body mechanism
snakeBody.insert(0, list(snakePos))
if snakePos == foodPos:
foodSpawn = False
score += 1
else:
snakeBody.pop()
if foodSpawn == False:
foodPos = [random.randrange(1, width // 10) * delta, random.randrange(1, height // 10) * delta]
foodSpawn = True
playSurface.fill(white)
for pos in snakeBody:
pygame.draw.rect(playSurface, green, pygame.Rect(pos[0], pos[1], delta, delta))
pygame.draw.rect(playSurface, brown, pygame.Rect(foodPos[0], foodPos[1], delta, delta))
# Bounds
if snakePos[0] >= width or snakePos[0] < 0:
gameOver()
if snakePos[1] >= height or snakePos[1] < 0:
gameOver()
# Self hit
for block in snakeBody[1:]:
if snakePos == block:
gameOver()
showScore()
pygame.display.flip()
fpsController.tick(20)