Solved

Pygame.org example crashes? Why?

Posted on 2014-01-22
12
238 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about problems I had with the new location API and permissions in Marshmallow
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

760 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

19 Experts available now in Live!

Get 1:1 Help Now