gaffer
Posts: 32
Joined: Fri Sep 09, 2016 2:14 am

Problem running the "Scary Spot the Difference" tutorial

Fri Oct 28, 2016 3:52 am

The blog at RaspberryPi.org is featuring a Python that project that I'm having troubles with. It is provided incrementally with explanations at each stage of the way; here's a link to the page that has the whole works: https://www.raspberrypi.org/learning/sc ... worksheet/

Before coding anything you're supposed to download two images and a sound file, all of which I've done. You then use IDLE to create a .py file in the same folder with the downloads. They feed it to you a bit at a time along with explanatory text. In the following code the pygame.quit() instruction marks the end of what they give you in stage one; they tell you that this will produce a black screen momentarily which then disappears when it reaches the pygame.quit() instruction. That worked for me as expected.

Then they give you four more lines to display the image plus a sleep. Here's what it looks like at that point, and where (at least in my case) the problem arises:

Code: Select all

import pygame
from pygame.locals import*
from time import sleep
from random import randrange

pygame.init()

width = pygame.display.Info().current_w
height = pygame.display.Info().current_h
screen = pygame.display.set_mode((width, height), FULLSCREEN)

#with the following uncommented, the program terminates as expected
#pygame.quit()

difference = pygame.image.load('spot_the_difference.jpg')
difference = pygame.transform.scale(difference, (width, height))

screen.blit(difference, (0, 0))
pygame.display.update()
sleep(3)

pygame.quit()
Theoretically that should display the image for 3 seconds and then terminate. What happens for me is I get the black graphics mode screen with a mouse cursor, which responds when I move the mouse, but nothing else. And it hangs there indefinitely. I tried lots of key combinations, starting with Ctrl-C, Ctrl-Q, Ctrl-Z, etc. with no apparent effect.

Eventually I got on another computer and learned while googling (I'm new to the Raspian OS) that I could open another terminal by using Ctrl-Alt-F2. The same place I found that suggested a "sudo killall", but that didn't work for me - switching back to the initial terminal via Ctrl-Alt-F7 I still had the black screen and cursor. In the end I bailed by issuing a "sudo shudown".

Basically I'm assuming that others are running this successfully and why it isn't working for me. I'm new to Python as well as to the Raspberry Pi, but have a lot of experience with C/C++ code. I'd very much like to understand what's going on beyond the obvious that I'm in graphics mode and hanging. I'd expect the program to terminate with an error code, or give me a pop-up notification, or something. If you have ideas for me, I'd appreciate hearing them.

I did a sudo update yesterday before starting this project. I'm using Python 3.4.2 on a RaspberryPi 3 which is running Linux version 4.4.21-v7+.

User avatar
B.Goode
Posts: 9243
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Problem running the "Scary Spot the Difference" tutorial

Fri Oct 28, 2016 10:16 am

Does the name of the puzzle image file that you downloaded and saved match the filename in the pygame.image.load line?

(Compare your code with the full version of the code towards the end of the worksheet.)

gaffer
Posts: 32
Joined: Fri Sep 09, 2016 2:14 am

Re: Problem running the "Scary Spot the Difference" tutorial

Sat Oct 29, 2016 5:45 am

Yes, you guessed the problem. I had double checked the names, but looking again I notice that the source refers to one of the files as a .jpg and the other as a .png file. I had downloaded both in .png format.

The fact that the program hangs when it fails to load the file seems particularly unfriendly to me. Up to now I'd been playing around with the turtle library and/or tkinter, displaying graphics within windows which could easily be closed if a program wasn't performing as expected. I see that this is more complex using pygame, with the entire display in graphics mode. I think I'll play around with adding try / catch procedures so the program can exit gracefully when something goes amiss.

Thanks again for your response.

Return to “Python”