Solved

Pygame.org example crashes? Why?

Posted on 2014-01-22
12
239 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to split this type of line? 5 84
listing all functions in JavaScript 19 180
Hide vba in gp 7 83
Reading text file in Python gives strange results 7 69
The purpose of this article is to demonstrate how we can use conditional statements using Python.
A short article about a problem I had getting the GPS LocationListener working.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now