Snake Pygame

This is the popular game snake. I specified a block size to build the blocks and make the snake faster.


import pygame import time import random pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,155,0) width = 800 height = 600 gameDisplay = pygame.display.set_mode((width,height)) pygame.display.set_caption('Slither') clock = pygame.time.Clock() blockSize = 23 FPS = 30 font = pygame.font.SysFont(None, 25) def snake(blockSize, snakelist): for XnY in snakelist: pygame.draw.rect(gameDisplay, green, [XnY[0],XnY[1],blockSize,blockSize]) def text_objects(text,color): textSurface = font.render(text, True, color) return textSurface, textSurface.get_rect() def message_to_screen(msg,color): textSurf, textRect = text_objects(msg,color) textRect.center = (width / 2), (height / 2) gameDisplay.blit(textSurf, textRect) def gameLoop(): gameExit = False gameOver = False xCoord = width/2 yCoord = height/2 xChange = 0 yChange = 0 snakeList = [] snakeLength = 1 randAppleX = round(random.randrange(0, width-blockSize)) randAppleY = round(random.randrange(0, height-blockSize)) while not gameExit: while gameOver == True: gameDisplay.fill(white) message_to_screen("Game over, press C to play again or Q to quit", red) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.QUIT: gameOver = False gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_q: gameExit = True gameOver = False if event.key == pygame.K_c: gameLoop() for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: xChange = -blockSize yChange = 0 elif event.key == pygame.K_RIGHT: xChange = blockSize yChange = 0 elif event.key == pygame.K_UP: yChange = -blockSize xChange = 0 elif event.key == pygame.K_DOWN: yChange = blockSize xChange = 0 if xCoord >= width or xCoord < 0 or yCoord >= height or yCoord < 0: gameOver = True xCoord += xChange yCoord += yChange gameDisplay.fill(white) AppleThickness = 30 pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, AppleThickness, AppleThickness]) snakeHead = [] snakeHead.append(xCoord) snakeHead.append(yCoord) snakeList.append(snakeHead) if len(snakeList) > snakeLength: del snakeList[0] for eachSegment in snakeList[:-1]: if eachSegment == snakeHead: gameOver = True snake(blockSize, snakeList) pygame.display.update() if xCoord > randAppleX and xCoord < randAppleX + AppleThickness or xCoord + blockSize > randAppleX and xCoord + blockSize < randAppleX+ AppleThickness: if yCoord > randAppleY and yCoord < randAppleY + AppleThickness: randAppleX = round(random.randrange(0, width-blockSize)) randAppleY = round(random.randrange(0, height-blockSize)) snakeLength += 1 elif yCoord + blockSize > randAppleY and yCoord + blockSize < randAppleY+ AppleThickness: randAppleX = round(random.randrange(0, width-blockSize)) randAppleY = round(random.randrange(0, height-blockSize))#/10.0)*10.0 snakeLength += 1 clock.tick(FPS) pygame.quit() quit() gameLoop()
1