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 * pygame.init() 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) pygame.display.flip() time.sleep(5) pygame.quit() sys.exit() while True: 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)) 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 += 20 if direction == 'left': snakePosition -= 20 if direction == 'up': snakePosition -= 20 if direction == 'down': snakePosition += 20 snakeSegments.insert(0,list(snakePosition)) if snakePosition == raspberryPosition and snakePosition == raspberryPosition: raspberrySpawned = 0 else: snakeSegments.pop() if raspberrySpawned == 0: x = random.randrange(1,32) y = random.randrange(1,24) raspberryPosition = [int(x*20),int(y*20)] raspberrySpawned = 1 playSurface.fill(blackColour) for position in snakeSegments: pygame.draw.rect(playSurface,whiteColour,Rect(position, position, 20, 20)) pygame.draw.rect(playSurface,redColour,Rect(raspberryPosition, raspberry, 20, 20)) pygame.display.flip() if snakePosition > 620 or snakePosition < 0: gameOver() if snakePosition > 460 or snakePosition < 0: gameOver() for snakeBody in snakeSegments[1:]: if snakePosition == snakeBody and snakePosition == snakeBody: gameOver() fpsClock.tick(20)