Created
March 2, 2016 05:23
-
-
Save ifd3f/580603b0a27281540664 to your computer and use it in GitHub Desktop.
Simple snake game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import sys | |
import time | |
import pygame | |
BACKGROUND_COLOR = (0, 0, 0) | |
SNAKE_COLOR = (0, 255, 0) | |
FOOD_COLOR = (255, 0, 0) | |
BORDER_COLOR = (0, 0, 255) | |
WIDTH = 800 | |
HEIGHT = 600 | |
GRID_WIDTH = 80 | |
GRID_HEIGHT = 50 | |
GRID_CELL_WIDTH = int(WIDTH / GRID_WIDTH) | |
GRID_CELL_HEIGHT = int(HEIGHT / GRID_HEIGHT) | |
EXISTING_FOOD = 100 | |
GROW_LENGTH = 5 | |
# Update speeds | |
EASY = .1 | |
MEDIUM = .05 | |
HARD = .025 | |
LUNATIC = .001 | |
LEVEL = MEDIUM | |
def main(): | |
pygame.init() | |
display = pygame.display.set_mode((WIDTH, HEIGHT)) | |
snakeCells = [(random.randint(0, WIDTH), random.randint(0, HEIGHT))] # [0] is the tail, end is the head | |
food = [] | |
direction = (0, 0) | |
nextDirection = (0, 0) | |
lastUpdate = 0 | |
snakeGrowLength = 0 | |
while True: | |
currentTime = time.time() | |
# Event handling | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
terminate() | |
elif event.type == pygame.KEYDOWN: | |
if event.key in (pygame.K_UP, pygame.K_w) and direction != (0, 1): | |
nextDirection = (0, -1) | |
elif event.key in (pygame.K_LEFT, pygame.K_a) and direction != (1, 0): | |
nextDirection = (-1, 0) | |
elif event.key in (pygame.K_DOWN, pygame.K_s) and direction != (0, -1): | |
nextDirection = (0, 1) | |
elif event.key in (pygame.K_RIGHT, pygame.K_d) and direction != (-1, 0): | |
nextDirection = (1, 0) | |
if len(food) < EXISTING_FOOD: | |
food.append(newFoodLocation(snakeCells)) | |
if currentTime - lastUpdate >= LEVEL: | |
lastUpdate = currentTime | |
snakeGrowLength = moveSnake(snakeCells, food, direction, snakeGrowLength) | |
direction = nextDirection | |
# Rendering | |
display.fill(BACKGROUND_COLOR) | |
for x, y in food: | |
cellRect = (x * GRID_CELL_WIDTH, y * GRID_CELL_HEIGHT, GRID_CELL_WIDTH, GRID_CELL_HEIGHT) | |
pygame.draw.rect(display, FOOD_COLOR, cellRect) | |
for x, y in snakeCells: | |
cellRect = (x * GRID_CELL_WIDTH, y * GRID_CELL_HEIGHT, GRID_CELL_WIDTH, GRID_CELL_HEIGHT) | |
pygame.draw.rect(display, SNAKE_COLOR, cellRect) | |
pygame.display.update() | |
def terminate(): | |
pygame.quit() | |
sys.exit() | |
def newFoodLocation(snakeCells): | |
x = random.randint(0, GRID_WIDTH - 1) | |
y = random.randint(0, GRID_HEIGHT - 1) | |
while (x, y) in snakeCells: | |
x = random.randint(0, GRID_WIDTH - 1) | |
y = random.randint(0, GRID_HEIGHT - 1) | |
return (x, y) | |
def moveSnake(snakeCells, food, direction, lengthToGrow): | |
currentHead = snakeCells[len(snakeCells) - 1] | |
if lengthToGrow == 0: | |
del snakeCells[0] | |
else: | |
lengthToGrow -= 1 | |
x = (currentHead[0] + direction[0]) % GRID_WIDTH | |
y = (currentHead[1] + direction[1]) % GRID_HEIGHT | |
newHead = (x, y) | |
if newHead in snakeCells: | |
terminate() | |
return | |
snakeCells.append(newHead) | |
if newHead in food: | |
food.remove(newHead) | |
lengthToGrow += GROW_LENGTH | |
return lengthToGrow | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment