I was working on my raspberry pi creating a game called raspberrysnake from code out of a book.

I entered the code exactly as shown... I had a few errors which turned out to be missing characters or mistyped characters. Finally, after catching these, I ran it through the command line.. no error!.. yet, only a black screen showed, after which the game would close itself. I'm assuming it was running and my snake just hit the wall, which 'game over' the game and quits.

I looked at the example in the book appendix and even the website for the book, thinking maybe it was more updated for errors in the book, but nothing. I'm hoping someone here can show me where I'm going wrong. I'm assuming since the game is actually starting, it has something to do with how it draws to the screen.

I want an answer, but I'm also game to learn by troubleshooting. Could anyone point me to the line causing an issue? I feel like I understood this script in sections, what each bit does in general, but I still feel like I'm looking a foreign language. lol Thanks in advance for helping me learn and fix this one.

#!/usr/bin/env python

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

fpsClock = pygame.time.Clock()

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

redColour = pygame.Color(255, 0, 0)
blackColour = pygame.Color(0, 0, 0)
whiteColour = pygame.Color(255, 255, 255)
greyColour = pygame.Color(150, 150, 150)
snakePosition = [100,100]
snakeSegments = [[100,100],[80,100],[60,100]]
raspberryPosition = [300,300]
raspberrySpawned = 1
direction = 'right'
changeDirection = direction

def gameOver():
    gameOverFont = pygame.font.Font('freesansbold.ttf', 72)
    gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
    gameOverRect = gameOverSurf.get_rect()
    gameOverRect.midtop = (320, 10)
    playSurface.blit(gameOverSurf, gameOverRect)

while True:
    for event in pygame.event.get():
        if event.type == 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:
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
if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]:
    raspberrySpawned = 0
if raspberrySpawned == 0:
    x = random.randrange(1,32)
    y = random.randrange(1,24)
    raspberryPosition = [int(x*20),int(y*20)]
raspberrySpawned = 1
for position in snakeSegments:
    pygame.draw.rect(playSurface,whiteColour,Rect(position[0], position[1], 20, 20))
    pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition[0], raspberry[1], 20, 20))
if snakePosition[0] > 620 or snakePosition[0] < 0:
if snakePosition[1] > 460 or snakePosition[1] < 0:
for snakeBody in snakeSegments[1:]:
    if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]:


You must indent line 50 to 89 (they belong to the while loop) and you must probably change raspberry[1] at line 79 with raspberryPosition[1].

Gribouillis, That worked like a charm. Glad you let me figure out how to indent everything correctly. lol Took a bit of trial and error. The raspbery[1] thing was definitely a typo >< I think the program got big enough and I confused enough that I completely missed that. Thank you for your help.

The raspberry pi sounds interesting. Could you tell us a little bit more about your personal experience with the device?

I had some loose change in my pocket so I ordered myself a Raspberry Pi B+ Ultimate Starter Kit from Amazon.com.

I was reading up on the Raspberry Pi, an interesting little computer with Linux OS and the Python language that costs about $30 - $40. You supply the keyboard and a monitor. It can be used for a myriad of science projects.

Okay, I got my Raspberry Pi B+ and have the Linux installed and running. Even wrote a few Python programs on the included Idle IDE.

I tried to write a Python program to turn on some of the io pins, but can't find good examples. Most of the web site references are pretty bad or nonexistent.

My Windows7 computer got hit by "Browser Hijacker Malware". Google Chrome shows an extension called "PriceLess" that comes back after it has been removed. One of the more obvious signs is that the yahoo sign on does not work. The program Malwarebytes does not find any malware infection, neither does Windows Security Essentials.

Should I blame North Korea for this?

So, tiny Raspberry Pi with its Linux OS to the rescue, at least I can read my e-mail again.

Good news, I was able to remove the malware that seems to have come in on a Adobe or Java update.
First go to
this downloads
close all Windows programs and run it
(this also removes the potentially infected MBAM program)
when this done reboot

Second go to
download and install MBAR
run it, it will scan and show any malware
when done with the scan click cleanup button
(had found a trojan.agent in a hidden folder C:\ProgramData\ )