Solved

Pygame.org example crashes? Why?

Posted on 2014-01-22
12
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
import 13 row from .csv file into another csv file 11 67
BATCH to EXE Converter 2 72
Java List 4 42
Starting to use Git with Visual  Studio Online 1 26
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

730 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