So I am currently making a snake game similar to Slither.io, but much simpler, you command the snake with WASD keys. But I have a problem with the gameOver()
function that runs me into an error when I die. Can anyone help me?
import pygame, sys
from pygame.locals import *
class Snake():
def __init__(self):
pygame.init()
self.fpsClock = pygame.time.Clock()
self.playSurface = pygame.display.set_mode((620, 480))
pygame.display.set_caption('Snake Game')
self.redColor = pygame.Color(250, 0, 0)
self.blackColor = pygame.Color(0, 0, 0)
self.whiteColor = pygame.Color(250, 255, 255)
self.snakeSegments = [[100, 100],[80, 100],[60, 100]]
self.foodPosition = [300, 300]
self.food = 1
self.direction = 'right'
self.changeDirection = self.direction
def gameOver(self):
self.gameOverFont = pygame.font.SysFont('san-serif', 72)
self.gameOverSurf = self.gameOVerFont.render('Game Over', True, self.greyColor)
self.playSurface.blit(self.gameOverSurf, [200,10])
pygame.display.flip()
time.sleep(5)
pygame.quit()
sys.exit()
def keyPressEvent(self, event):
if event.key == K_RIGHT or event.key == K_d:
self.changeDirection = 'right'
if event.key == K_RIGHT or event.key == K_a:
self.changeDirection = 'left'
if event.key == K_RIGHT or event.key == K_w:
self.changeDirection = 'up'
if event.key == K_RIGHT or event.key == K_s:
self.changeDirection = 'down'
def move(self):
if self.changeDirection == 'right' and self.direction != 'left':
self.direction = self.changeDirection
if self.changeDirection == 'left' and self.direction != 'right':
self.direction = self.changeDirection
if self.changeDirection == 'up' and self.direction != 'down':
self.direction = self.changeDirection
if self.changeDirection == 'down' and self.direction != 'up':
self.direction = self.changeDirection
if self.direction == 'right':
self.snakePosition[0] += 20
if self.direction == 'left':
self.snakePosition[0] -= 20
if self.direction == 'up':
self.snakePosition[0] -= 20
if self.direction == 'down':
self.snakePosition[0] += 20
def playMusic(self):
music = pygame.mixer.music
music.load('./sucess.mp3')
music.play()
def main(self):
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == KEYDOWN:
self.keyPressEvent(event)
self.move()
self.snakeSegments.insert(0, list(self.snakePosition))
if self.snakePosition[0] == self.foodPosition[0] and self.snakePosition[1] == self.foodPosition[1]:
self.playMusic()
else:
self.snakeSegments.pop()
if self.food == 0:
while True:
x = random.randint(1, 31)
y = random.randint(1, 23)
self.foodPosition = [int(x * 20),int(y * 20)]
if self.foodPosition not in self.snakeSegments:
break
self.food = 1
self.playSurface.fill(self.blackColor)
for position in snakeSegments:
pygame.draw.rect(playSurface, whiteColor, Rect(position[0], position[1], 20, 20))
pygame.draw.rect(playSurface, self.redColor, Rect(foodPosition[0], foodPosition[1], 20, 20))
if self.snakePosition[0] > 620 or self.snakePosition[0] < 0:
self.gameOver()
if self.snakePosition[1] > 620 or self.snakePosition[1] < 0:
self.gameOver()
if self.snakePosition in self.snakeSegments[1]:
self.gameOver()
self.fpsClock.tick(10)
pygame.display.update()
if __name__ == '__main__':
game = Snake()
game.main()