User avatar
peachoftree
Posts: 4
Joined: Mon Aug 05, 2013 2:48 am

Raspberry Snake Variation

Mon Aug 05, 2013 2:59 am

I made a few modifications to the raspberry snake game found in the raspberry pi user guide

please feel free to post your variations and ideas and i will try to add them and post the code here

i included:
blueberries (they make you lose length)
wrap (when you hit the top you go to the bottom)
a head (the first segment is red)
score (posted in the middle at game over)

the original code can be found here:

http://media.wiley.com/product_ancillar ... rysnake.py

Code: Select all

#!/usr/bin/env python


# Raspberry Snake
# Written by Gareth Halfacree for the Raspberry Pi User Guide
# Head, score, replay, blueberries, and Wrap written by John Petryk

#initislize the modules

import pygame, sys, time, random
from pygame.locals import *

pygame.init()

fpsClock = pygame.time.Clock()

#draw the window and set the caption

playSurface = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Raspberry Snake')



def gameOver():
    
    # draw game over on the top of the screen
    
    gameOverFont = pygame.font.Font('freesansbold.ttf', 72)
    gameOverSurf = gameOverFont.render('Game Over', True, grey)
    gameOverRect = gameOverSurf.get_rect()
    gameOverRect.centerx = playSurface.get_rect().centerx
    playSurface.blit(gameOverSurf, gameOverRect)

    # draw the score in the middle of the screen
    
    berriesFont = pygame.font.Font('freesansbold.ttf', 20)
    berriesSurf = berriesFont.render( str(len(snakeSegments)), True, grey)
    playSurface.blit(berriesSurf,(320, 240))
    pygame.display.flip()
    time.sleep(2)

#define some variables

red = pygame.Color(255, 0, 0)
blue = pygame.Color(0,0, 255)
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
grey = pygame.Color(150, 150, 150)
snakePosition = [100,120]
snakeSegments = [[100,120],[80,120],[60,120], [40, 120], [20,120], [10,120]]
raspberryPosition = [300, 300]
blueBerryPosition = [200, 200]
blueBerryPosition2 = [400, 400]
blueBerryPosition3 = [0, 3]
blueBerryPosition4 = [0, 4]
counter = 0
raspberrySpawned = 1
direction = 'right'
blueDirect = 1
changeDirection = direction
overTop = False
overBottom = False
overRight = False
overLeft = False
blueOverTop = False
blueOverBottom = False
blueOverRight = False
blueOverLeft = False

# main game loop

while True:
    
    # detect the key pressed and act on it
    
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
        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))
    if not (overTop or overBottom or overLeft or overRight):
        
        # change direction (if required)
        
        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

        # move the Snake
        
        if direction == 'right':
            snakePosition[0] += 20
        if direction == 'left':
            snakePosition[0] -= 20
        if direction == 'up':
            snakePosition[1] -= 20
        if direction == 'down':
            snakePosition[1] += 20

    # if needed, wrap the snake around the screen

    if overTop == True:
        snakePosition[1] = 460
        overTop = False
    if overBottom == True:
        snakePosition[1] = 0
        overBottom = False
    if overRight == True:
        snakePosition[0] = 0
        overRight = False
    if overLeft == True:
        snakePosition[0] = 620
        overLeft = False

    #add a segment if a raspberry is eaten
    
    snakeSegments.insert(0,list(snakePosition))
    if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
        raspberrySpawned = False
    else:
        raspberrySpawned = True
        snakeSegments.pop()

    # take away a segment if a blueberry is eaten
    
    if (snakePosition[0] in [blueBerryPosition[0], blueBerryPosition2[0], blueBerryPosition3[0], blueBerryPosition4[0]]) and (snakePosition[1] in [blueBerryPosition[1], blueBerryPosition2[1], blueBerryPosition3[1], blueBerryPosition4[1]]):
        snakeSegments.pop()
        blueBerrySpawned = False
        blueBerrySpawned1 = False

    
    # spawn the Blueberries
    

    blueBerryPosition[0] = (raspberryPosition[0] + 20)
    blueBerryPosition[1] = raspberryPosition[1] + 20
    
    blueBerryPosition2[0] = (raspberryPosition[0] - 20)
    blueBerryPosition2[1] = raspberryPosition[1] -20

    blueBerryPosition3[1] = (raspberryPosition[1] - 20)
    blueBerryPosition3[0] = raspberryPosition[0] + 20
    
    blueBerryPosition4[1] = (raspberryPosition[1] + 20)
    blueBerryPosition4[0] = raspberryPosition[0] -20



        
    #fill in the window with color
    
    playSurface.fill(black)

    #spawn a new raspberry if needed
    
    if raspberrySpawned == False:
        x = random.randrange(1, 32)
        y = random.randrange(1, 24)
        raspberryPosition = [int(x*20), int(y*20)]
        pygame.display.flip()
        counter += 1
    else:
        snakeSegments.pop
    
    #draw entities
    
    pygame.draw.rect(playSurface, blue, Rect(blueBerryPosition[0], blueBerryPosition[1], 20, 20))
    pygame.draw.rect(playSurface, blue, Rect(blueBerryPosition2[0], blueBerryPosition2[1], 20, 20))
    pygame.draw.rect(playSurface, blue, Rect(blueBerryPosition3[0], blueBerryPosition3[1], 20, 20))
    pygame.draw.rect(playSurface, blue, Rect(blueBerryPosition4[0], blueBerryPosition4[1], 20, 20))
    
    for snakeBody in snakeSegments:
        pygame.draw.rect(playSurface, white, Rect(snakeBody[0], snakeBody[1], 20, 20))
    if len(snakeSegments) > 0:
        pygame.draw.rect(playSurface, red, Rect(snakePosition[0], snakePosition[1], 20, 20))
    pygame.draw.rect(playSurface, red, Rect(raspberryPosition[0], raspberryPosition[1], 20, 20))
 
    
    pygame.display.flip()

    # detect if wrap is needed
    
    if snakePosition[0] > 639:
        overRight = True
    elif snakePosition[0] < 0:
        overLeft = True 
    elif snakePosition[1] > 479:
        overBottom = True
    elif snakePosition[1] < 0:
        overTop = True
    else:
        overTop = False
        overBottom = False
        overRight = False
        overLeft = False

    # detect game over (when snake touches itself)
    
    for snakeBody in snakeSegments[1:]:
        if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:
            gameOver()
            snakePosition = [100, 120]
            snakeSegments = 0
            pygame.display.update()
            snakeSegments = [[100,120],[80,120],[60,120], [40, 120], [20,120]]
            direction = "right"
            pygame.display.update()
    if len(snakeSegments) == 0:
            gameOver()
            snakePosition = [100, 120]
            snakeSegments = 0
            pygame.display.update()
            snakeSegments = [[100,120],[80,120],[60,120], [40, 120], [20,120]]
            direction = "right"
            pygame.display.update()
    
    # set the pace
    fpsClock.tick(30)
    
    
    


Return to “Python”