Solved

Pygame.org example crashes? Why?

Posted on 2014-01-22
12
240 Views
Last Modified: 2014-03-16
Hi
This Python PyGame intro code from pygame.org crashes

I can't see why

This the source website.

here

and below is the code that causes an "unexpected error"
Why?
Thanks
       
    import sys, pygame
    pygame.init()
 
    size = width, height = 320, 240
    speed = [2, 2]
    black = 0, 0, 0
 
    screen = pygame.display.set_mode(size)
 
    ball = pygame.image.load("http://www.pygame.org/docs/tut/intro/ball.gif")
    ballrect = ball.get_rect()

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()

        ballrect = ballrect.move(speed)
        if ballrect.left < 0 or ballrect.right > width:
            speed[0] = -speed[0]
        if ballrect.top < 0 or ballrect.bottom > height:
            speed[1] = -speed[1]

        screen.fill(black)
        screen.blit(ball, ballrect)
        pygame.display.flip()

Open in new window

0
Comment
Question by:beavoid
  • 5
  • 5
12 Comments
 
LVL 19

Expert Comment

by:mrwad99
ID: 39808862
At a guess,

ball = pygame.image.load("http://www.pygame.org/docs/tut/intro/ball.gif")

is causing the problems; most likely because that image cannot be downloaded (although it does exist),  meaning

ballrect = ball.get_rect()

will explode as "ball" has no image in it.

Can you step through the code in the debugger?
0
 

Author Comment

by:beavoid
ID: 39809653
The debugging system isn't blatantly obvious, like eclipse, for example.

There aren't any 'breakpoint' entries in the menus. How is it done?
Thanks
0
 

Author Comment

by:beavoid
ID: 39809694
Thx
If I left out an attempt to find the file in a path and just had ball.jpg as an argument, where would it look for the file, online and on a file system? wherever the .py file is?

Also, I thought that python was runnable from a webpage, like a Java applet, but it has to be downloaded and then run?

How, where are these games run on this pygame page?

here

How are they stored, in a zip?
?

Thanks
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 19

Expert Comment

by:mrwad99
ID: 39810038
I would assume that it will look in the directory relative to the source file for the image.

What IDE are you using to develop in?
0
 

Author Comment

by:beavoid
ID: 39810100
I'm using IDLE
It launches a Python 3.3.3 shell

how does it know what source to look in? all my recent Java games linked to graphics files in my webspace. My first Java RTS had the image files in a subfolder of the game's disk location.
thx
0
 

Author Comment

by:beavoid
ID: 39931855
I've requested that this question be deleted for the following reason:

Th mods suggested I delete this.
0
 
LVL 19

Accepted Solution

by:
mrwad99 earned 500 total points
ID: 39931828
Sorry about the delay in getting back to you!

I tried this code in Python 2.7.3 with IDLE.  I too found that it fails to load the image when fetched from online (I don't yet know why this is).  I changed it to load the image locally (in answer to your earlier question: it fetches the image from relative to the source file), and it worked fine.  I have attached the images showing this working; please let me know if you still have difficulties.

Again, sorry about the delay.

Code
Running
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 39931856
<Cancelling delete request so beavoid has chance to read my answer before it gets deleted!>
0
 

Author Closing Comment

by:beavoid
ID: 39931944
m99, I appreciate your tenacity, thanks
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 39932949
Glad to help.  

Oh, and I almost forgot: regarding breakpoints, you set them via a right click in the editor; the line with the breakpoint on is highlighted in yellow.  Then, with the debugger open, you choose Run Module (or hit F5), then "Go" in the debugger dialog to continue from line 1 up to your chosen breakpoint position :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 90
Please explain: Aspect Oriented Programming 2 92
What are programming codes and how do they work? 7 93
Downgrading from Python 3.4.5 to 3.4.3 5 39
A short article about a problem I had getting the GPS LocationListener working.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question