Bat and ball game

General programming chat and advice for beginners

13 posts
by Killerspec » Thu Nov 01, 2012 3:31 am
I sat down and typed out the bat and ball game to night. Ironed out all the errors and once ready I came up with this:

Traceback (most recent call last):
File "/home/pi/magpiissue2batball.py", line 1, in <module>
import pygame
ImportError: No module named pygame

Ok so I tried import python_pygame as that is the file that I installed earlier coz when i tried to install pygame it gave me some error about being discontinued and I got this error:

Traceback (most recent call last):
File "/home/pi/magpiissue2batball.py", line 1, in <module>
import python_pygame
ImportError: No module named python_pygame

Ummmm what am I missing as i really want to get this working.

Matthew
Posts: 31
Joined: Thu Sep 06, 2012 10:41 pm
by malakai » Thu Nov 01, 2012 3:43 am
Don't know if this will help but try adding this as the first line of code:

#!/usr/bin/env python
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)
User avatar
Posts: 1383
Joined: Sat Sep 15, 2012 10:35 am
by billb » Thu Nov 01, 2012 5:43 am
Pygame doesn't come pre-installed on Raspbian, and the version you can install using apt-get is only for Python 2.x. If you're using "IDLE" and trying to run your code from there, you're using Python 2.x, and if you're using "IDLE 3" you're using Python 3.x.

To install the Pygame package for Python 2.x:

Code: Select all
sudo apt-get install python-pygame


To install Pygame for Python 3 you need to build it yourself. Here's how:

# change to your home directory (just in case)
Code: Select all
cd

# get pygame source code
Code: Select all
sudo apt-get install mercurial


Code: Select all
hg clone https://bitbucket.org/pygame/pygame


Code: Select all
cd pygame


# install dependencies
Code: Select all
sudo apt-get install python3-dev python3-numpy libsdl-dev libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev libportmidi-dev libavformat-dev libswscale-dev

(that's all one line)

# build and install pygame
Code: Select all
python3 setup.py build


Code: Select all
sudo python3 setup.py install


That all takes around half an hour.
User avatar
Posts: 175
Joined: Wed Sep 19, 2012 10:27 pm
by Killerspec » Thu Nov 01, 2012 1:35 pm
Thanks a million for the replies.

As soon as I get home I will follow both options and see what happens.

I have a feeling that in my passion for wanting to swim I dived into the deep end of the pond with water wings on and having a load of problems paddling. If you get what I mean.

Matthew
Posts: 31
Joined: Thu Sep 06, 2012 10:41 pm
by billb » Thu Nov 01, 2012 4:31 pm
I think that particular Python code listing in the MagPi was only tested on Windows (where Pygame for Python 3 would have been easily available for install). As I described above, you have to build and install it on your own with Raspbian Linux (and probably others). It's not the first time this question has come up on the forum, so don't be discouraged. You only learn so much from reading and have to dive in sometime! :)
User avatar
Posts: 175
Joined: Wed Sep 19, 2012 10:27 pm
by Killerspec » Fri Nov 02, 2012 1:38 am
Thanks a million for all the help. I sort of have it working. Now it is hanging on me aint a cooking clue why, I have overclocked the chip to turbo as i thought that maybe it was running to slow but still just hangs, so guess I have to send the file to my MS pc and go through it with a fine tooth comb to make sure that I have all the text in the right place.

Matthew
Posts: 31
Joined: Thu Sep 06, 2012 10:41 pm
by Killerspec » Fri Nov 02, 2012 1:41 am
If I try to close down program it shows the bat and ball but it dont move.

Hmmmm confusion dot com.

Matthew
Posts: 31
Joined: Thu Sep 06, 2012 10:41 pm
by malakai » Fri Nov 02, 2012 1:52 am
Curious to know if you remember or know exactly what you had to do to get it running.

Also for myself I didn't have much time so I just pasted the code from here into a file and it is slow but runs testing over ssh.

http://magpiunofficial.wikia.com/wiki/MagPi_volume_1_Listing
http://www.raspians.com - always looking for content feel free to ask to have it posted. Or sign up and message me to become a contributor to the site. Raspians is not affiliated with the Raspberry Pi Foundation. (RPi's + You = Raspians)
User avatar
Posts: 1383
Joined: Sat Sep 15, 2012 10:35 am
by antiloquax » Fri Nov 02, 2012 6:13 pm
billb wrote:I think that particular Python code listing in the MagPi was only tested on Windows.


Hi, I'm sorry to hear that people have been having problems with this code. I wrote the original version, which was edited a fair bit in the published version.
I can confirm, however, that it was tested on the Raspberry Pi. In fact I don't use Windows on my home machines (I'm a dedicated Puppy Linux user!)

Here's my version of the code:
Code: Select all
# bat_ball.py: simple pygame demo

# import the pygame library
import pygame, random
# this saves us from typing in full paths to the pygame modules.
from pygame.locals import *
# initialise the game engine
pygame.init()

# initialise variables
FPS = 100               # Frames Per Second
size = 300, 300         # size of pygame window
title = "Bat and Ball"  # window title
bat_gap = 50            # gap between bat and bottom of window
speed = [1, 1]          # speed of ball
ball_size = 40, 40      # size of ball surface
ball_rad = 20           # radius of ball
bat_size = 64, 12       # size of bat

# define colours
raspberry = [135,  38,  87]
black     = [  0,   0,   0]
white     = [255, 255, 255]
blue      = [  0,   0, 255]
green     = [  0, 255,   0]
red       = [255,   0,   0]
yellow    = [255, 255,   0]

# set up the graphic window
width, height = size
screen = pygame.display.set_mode(size)
screenrect = screen.get_rect()

# give the window a title
pygame.display.set_caption(title)

# This makes the normal mouse pointer invisible in graphics window
pygame.mouse.set_visible(False)

# make surfaces for bat and ball
# bat
bat_surf = pygame.Surface(bat_size)
bat_surf.fill(green)       
batrect = bat_surf.get_rect()
# ball
ball_surf = pygame.Surface((ball_size))
ball_surf.set_colorkey(black)
ballrect = ball_surf.get_rect()
ball = pygame.draw.circle(ball_surf, blue,[ballrect.centerx, ballrect.centery], ball_rad)

# puts the bat center of screen, near the bottom
batrect.center = (screenrect.centerx, (screenrect.bottom - bat_gap))

# make a text object
font = pygame.font.Font(None, 36)
text = font.render("Game Over.", True, (red))
textRect = text.get_rect()
textRect.centerx = screenrect.centerx
textRect.centery = screenrect.centery

# loop until the user clicks the close button
done=False

# create a timer to control how often the screen updates
clock = pygame.time.Clock()

# main game loop
while done==False:
    # fill the screen with a colour
    screen.fill(yellow)

    # event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True

    # moves bat in accordance with the mouse position
    position = pygame.mouse.get_pos()
    batrect.centerx = position[0]

    # move the ball
    ballrect.left += speed[0]
    ballrect.top += speed[1]

    # collision detection
    if ballrect.left <= (batrect.right) and ballrect.right >= (batrect.left):
        if ballrect.bottom == batrect.top:
            speed[1] = -speed[1]

    #check if the ball is going off screen
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0:
        speed[1] = -speed[1]

    # print "Game Over" if the ball leaves screen
    if ballrect.top > screenrect.bottom:
        screen.blit(text, textRect)

    # blit the images to the screen
    screen.blit(bat_surf, batrect)
    screen.blit(ball_surf, ballrect)

    # set the loop to fps cycles per second
    clock.tick(FPS)

    #  update the display
    pygame.display.flip()

# close pygame
pygame.quit()



Also I have done a couple of YouTube Videos which walk you through the code.
Click here.

I'm adding a screen-shot of this running in the Raspbian release on my Pi. It is seen here running from Python 2 - although I work in Python 3, for this code it doesn't make any difference.
Attachments
bat.png
bat.png (21.06 KiB) Viewed 1443 times
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
by billb » Fri Nov 02, 2012 6:50 pm
antiloquax wrote:I can confirm, however, that it was tested on the Raspberry Pi


Good -- I was going by the "TESTED!" box printed on page 29 which just says the python / pygame version and Windows 7. I think users tend to go with Python 3 and find that they can't import pygame because apt-get only installs python-pygame for Python 2.x.
User avatar
Posts: 175
Joined: Wed Sep 19, 2012 10:27 pm
by antiloquax » Fri Nov 02, 2012 8:03 pm
Yes, it's a pain that Pygame doesn't work fully with Python 3.2 (not sure what the situation is with Python 3.3).
Actually I like to use 3.14, which works properly with Pygame. I compiled it myself and then installed Pygame. Also I use Arch day-to-day, although I also use the Raspbian and Puppy images.
mark
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am
by Neil.Thompson » Sun Nov 04, 2012 8:37 pm
I'm a newbie but for those that find this post:

I followed the instructions above to download and build pygame for use with Python3. (Python 3.2.3) Running IDLE3, the'Bat and Ball' code from PiMag just froze, but copying the full code posted above worked fine. Yay!

Thanks everyone. I can move on to PiMag Issue 2 now...
Posts: 1
Joined: Tue Oct 30, 2012 7:26 pm
by antiloquax » Mon Nov 05, 2012 8:41 am
Hi Neil,
I am glad you got it working!
:mrgreen:
Posts: 406
Joined: Sun Nov 20, 2011 11:37 am