Created
February 8, 2014 23:46
-
-
Save andycandrea/8892104 to your computer and use it in GitHub Desktop.
A simple clone of the popular game "Snake" I made shortly after getting my Raspberry Pi and diving into Linux for the first time several years ago.
This file contains hidden or 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
#!/usr/bin/env python | |
""" | |
A simple clone of the game Snake made to expose myself to Python/Pygame. Inspired by the guide in | |
the Raspberry Pi User Guide by Eben Upton and Gareth Halfacree, though I did make some changes | |
based on my preferences and coding style. | |
""" | |
import pygame, sys, time, random | |
from pygame.locals import * | |
pygame.init() | |
fpsClock = pygame.time.Clock() | |
playSurface = pygame.display.set_mode((640, 480)) | |
pygame.display.set_caption('Raspberry Snake') | |
red = pygame.Color(255, 0, 0) | |
black = pygame.Color(0, 0, 0) | |
white = pygame.Color(255, 255, 255) | |
grey = pygame.Color(150, 150, 150) | |
# Initialization | |
snakePosition = [100,100] | |
snakeSegments = [[100,100],[80,100],[60,100]] | |
raspberryPosition = [300,300] | |
raspberrySpawned = 1 | |
direction = 'right' | |
changeDirection = direction | |
# Game Over definition; called whenever the snake hits a boundary or collides with itself. | |
def gameOver(): | |
gameOverFont = pygame.font.Font('freesansbold.ttf', 72) | |
gameOverSurf = gameOverFont.render('Game Over', True, grey) | |
gameOverRect = gameOverSurf.get_rect() | |
gameOverRect.midtop = (320, 10) | |
playSurface.blit(gameOverSurf, gameOverRect) | |
pygame.display.flip() | |
time.sleep(5) | |
pygame.quit() | |
sys.exit() | |
# Main game loop | |
while True: | |
# Event handler loop | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
pygame.quit() | |
sys.exit() | |
elif event.type == KEYDOWN: | |
if event.key == K_RIGHT or event.key == ord('d'): | |
changeDirection = 'right' | |
if event.key == K_LEFT or event.key == ord('a'): | |
changeDirection = 'left' | |
if event.key == K_UP or event.key == ord('w'): | |
changeDirection = 'up' | |
if event.key == K_DOWN or event.key == ord('s'): | |
changeDirection = 'down' | |
if event.key == K_ESCAPE: | |
pygame.event.post(pygame.event.Event(QUIT)) | |
# The following conditionals ensure that the snake does not commit suicide by moving | |
# backwards and colliding with itself. | |
if changeDirection == 'right' and not direction == 'left': | |
direction = changeDirection | |
if changeDirection == 'left' and not direction == 'right': | |
direction = changeDirection | |
if changeDirection == 'up' and not direction == 'down': | |
direction = changeDirection | |
if changeDirection == 'down' and not direction == 'up': | |
direction = changeDirection | |
if direction == 'right': | |
snakePosition[0] += 20 | |
if direction == 'left': | |
snakePosition[0] -= 20 | |
if direction == 'up': | |
snakePosition[1] -= 20 | |
if direction == 'down': | |
snakePosition[1] += 20 | |
# The following five lines of code allow the snake to move and grow. A new segment is added in front | |
# of the snake whenever it moves; if it does not collide with the raspberry/food, the last snake | |
# segment is popped, meaning the snake is actually constantly created and recreated, rather than just | |
# having positional data rewritten. | |
snakeSegments.insert(0,list(snakePosition)) | |
if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]: | |
raspberrySpawned = 0 | |
else: | |
snakeSegments.pop() | |
# Spawn the raspberry in a random location if it has been eaten. | |
if raspberrySpawned == 0: | |
x = random.randrange(1,32) | |
y = random.randrange(1,24) | |
raspberryPosition = [int(x*20),int(y*20)] | |
raspberrySpawned = 1 | |
# Draw everything to screen | |
playSurface.fill(black) | |
for position in snakeSegments: | |
pygame.draw.rect(playSurface,white,Rect(position[0], position[1], 20, 20)) | |
pygame.draw.rect(playSurface,red,Rect(raspberryPosition[0], raspberryPosition[1], 20, 20)) | |
pygame.display.flip() | |
if snakePosition[0] > 620 or snakePosition[0] < 0: | |
gameOver() | |
if snakePosition[1] > 460 or snakePosition[1] < 0: | |
for snakeBody in snakeSegments[1:]: | |
if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]: | |
gameOver() | |
fpsClock.tick(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment