Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had a similar problem in a very simple piece of code:</p> <pre><code> import sys, pygame pygame.init() size = width, height = 640, 480 speed = [2, 2] black = 0, 0, 0 screen = pygame.display.set_mode(size) ball = pygame.image.load("Golfball.png") ballrect = ball.get_rect() while 1: event = pygame.event.poll() if event.type == pygame.QUIT: pygame.quit() ballrect = ballrect.move(speed) if ballrect.left &lt; 0 or ballrect.right &gt; width: speed[0] = -speed[0] if ballrect.top &lt; 0 or ballrect.bottom &gt; height: speed[1] = -speed[1] screen.fill(black) screen.blit(ball, ballrect) pygame.display.flip() pygame.time.delay(5) </code></pre> <p>Error message was:</p> <pre><code> Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "bounce.py", line 22, in &lt;module&gt; screen.fill(black) pygame.error: display Surface quit </code></pre> <p>So I put</p> <pre><code> import pdb </code></pre> <p>at the top after</p> <pre><code> pygame.init() </code></pre> <p>and used</p> <pre><code> pdb.set_trace() </code></pre> <p><em>after</em> the line with</p> <pre><code> pygame.quit() </code></pre> <p>Now I ran the program, clicked to close the window and was actually a bit surprised to see that I fell into the debugger (after all, I expected the quit to completely take me out immediately). So I concluded that the <em>quit</em> doesn't actually stop everything at that point. Looked like the program was continuing beyond the quit, was reaching</p> <pre><code> screen.fill(black) </code></pre> <p>and this was causing the problem. So I added</p> <pre><code> break </code></pre> <p>after the</p> <pre><code> pygame.quit() </code></pre> <p>and all works happily now.</p> <p>[ Added later: It occurs to me now that</p> <pre><code> pygame.quit() </code></pre> <p>is quitting the <em>module</em>, and not the <em>program that is running</em>, so you need the <em>break</em> to get out of this simple program. ]</p> <p>Just for the record, this means the good version is</p> <pre><code> import sys, pygame pygame.init() size = width, height = 640, 480 speed = [2, 2] black = 0, 0, 0 screen = pygame.display.set_mode(size) ball = pygame.image.load("Golfball.png") ballrect = ball.get_rect() while 1: event = pygame.event.poll() if event.type == pygame.QUIT: pygame.quit() break ballrect = ballrect.move(speed) if ballrect.left &lt; 0 or ballrect.right &gt; width: speed[0] = -speed[0] if ballrect.top &lt; 0 or ballrect.bottom &gt; height: speed[1] = -speed[1] screen.fill(black) screen.blit(ball, ballrect) pygame.display.flip() pygame.time.delay(5) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload