horwoodlake
Posts: 2
Joined: Fri Aug 31, 2012 10:53 pm

Need help with a Python Program

Wed Sep 02, 2015 1:55 am

I am a newbie. I have type in all the Python programs in Issue 1 of the Magpi. I type them in with either the Leaf Text editor found in Accessories or with the Nano editor when I am using the Terminal. To run them I do the following: Python3 ./name.py All eight programs worked and I played around with them making slight changes to see how they worked. I type in the "The Bat and Ball" and when I ran it I got the following error:
i@raspberrypi ~ $ python3 Bat.pi
File "Bat.pi", line 29
font = pygame.font.Font(None, 36)
^
SyntaxError: invalid syntax
pi@raspberrypi ~ $

I checked again and again and the program is type in correctly. I paste the sentence here:

# make a text object
font = pygame.font.Font(None, 36)
text = font.render("Game Over", True, (255,0,0))

Before posting this message I searched the Forum but could not find the answer to my problem. Please help. I am new and really enjoying the this.
Donald Tambeau
Horwoodlake

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Need help with a Python Program

Wed Sep 02, 2015 3:17 am

There is some discussion of that program here:
viewtopic.php?f=31&t=21769

But, I don't think it's your specific problem.

Presumably you have installed pygame? If not, then that's probably the first thing you should do.

Secondly, if you typed the program yourself you may have made an error on the preceding lines, i.e. the error is not on line 29, but due to some typo one or two lines earlier.

Or it could be something else. Keep at it.

horwoodlake
Posts: 2
Joined: Fri Aug 31, 2012 10:53 pm

Re: Need help with a Python Program

Thu Sep 03, 2015 4:59 am

Thanks for your help. I have double and doubled check the spelling. I have tried to install pygame. with
apt-get install pygame and get the reply E: Unable to locate package pygame.
I am doing this from the Terminal program.
Don

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Need help with a Python Program

Thu Sep 03, 2015 5:06 am

Did you follow the instructions in the third message?
viewtopic.php?p=206639#p206639

The package is called python-pygame, and it only supports python 2.x, although that was three years ago, so maybe it's no longer an issue.

gordon77
Posts: 4312
Joined: Sun Aug 05, 2012 3:12 pm

Re: Need help with a Python Program

Thu Sep 03, 2015 7:28 am

If you are using Raspbian then pygame will already be installed.

This is the code I use with text in pygame...

Code: Select all

fontObj = pygame.font.Font('freesansbold.ttf',fsize)
   msgSurfaceObj = fontObj.render(msg, False,color)
 
Can you show more of your code, as it may not like you using 'font = ', try changing to 'fontObj' in both lines you have shown.

Code: Select all

fontObj = pygame.font.Font(None, 36)
text = fontObj.render("Game Over", True, (255,0,0))

Return to “The MagPi”