{"id":3323,"date":"2014-03-23T02:20:17","date_gmt":"2014-03-23T02:20:17","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/pygame-error-display-surface-quit-why-collection-of-common-programming-errors-2\/"},"modified":"2014-03-23T02:20:17","modified_gmt":"2014-03-23T02:20:17","slug":"pygame-error-display-surface-quit-why-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/23\/pygame-error-display-surface-quit-why-collection-of-common-programming-errors-2\/","title":{"rendered":"Pygame error: display surface quit: Why?-Collection of common programming errors"},"content":{"rendered":"<p>I had a similar problem in a very simple piece of code:<\/p>\n<pre><code>    import sys, pygame\n    pygame.init()\n\n    size = width, height = 640, 480\n    speed = [2, 2]\n    black = 0, 0, 0\n\n    screen = pygame.display.set_mode(size)\n    ball = pygame.image.load(\"Golfball.png\")\n    ballrect = ball.get_rect()\n    while 1:\n        event = pygame.event.poll()\n        if event.type == pygame.QUIT:\n                pygame.quit()\n\n        ballrect = ballrect.move(speed)\n        if ballrect.left &lt; 0 or ballrect.right &gt; width:\n            speed[0] = -speed[0]\n        if ballrect.top &lt; 0 or ballrect.bottom &gt; height:\n            speed[1] = -speed[1]\n        screen.fill(black)\n        screen.blit(ball, ballrect)\n        pygame.display.flip()\n        pygame.time.delay(5)\n<\/code><\/pre>\n<p>Error message was:<\/p>\n<pre><code>    Traceback (most recent call last):\n      File \"\", line 1, in \n      File \"bounce.py\", line 22, in \n        screen.fill(black)\n    pygame.error: display Surface quit\n<\/code><\/pre>\n<p>So I put<\/p>\n<pre><code>    import pdb\n<\/code><\/pre>\n<p>at the top after<\/p>\n<pre><code>    pygame.init()\n<\/code><\/pre>\n<p>and used<\/p>\n<pre><code>    pdb.set_trace()\n<\/code><\/pre>\n<p><em>after<\/em> the line with<\/p>\n<pre><code>    pygame.quit()\n<\/code><\/pre>\n<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&#8217;t actually stop everything at that point. Looked like the program was continuing beyond the quit, was reaching<\/p>\n<pre><code>    screen.fill(black)\n<\/code><\/pre>\n<p>and this was causing the problem. So I added<\/p>\n<pre><code>    break\n<\/code><\/pre>\n<p>after the<\/p>\n<pre><code>    pygame.quit()\n<\/code><\/pre>\n<p>and all works happily now.<\/p>\n<p>[ Added later: It occurs to me now that<\/p>\n<pre><code>    pygame.quit()\n<\/code><\/pre>\n<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>\n<p>Just for the record, this means the good version is<\/p>\n<pre><code>    import sys, pygame\n    pygame.init()\n\n    size = width, height = 640, 480\n    speed = [2, 2]\n    black = 0, 0, 0\n\n    screen = pygame.display.set_mode(size)\n    ball = pygame.image.load(\"Golfball.png\")\n    ballrect = ball.get_rect()\n    while 1:\n        event = pygame.event.poll()\n        if event.type == pygame.QUIT:\n                pygame.quit()\n                break\n\n        ballrect = ballrect.move(speed)\n        if ballrect.left &lt; 0 or ballrect.right &gt; width:\n            speed[0] = -speed[0]\n        if ballrect.top &lt; 0 or ballrect.bottom &gt; height:\n            speed[1] = -speed[1]\n        screen.fill(black)\n        screen.blit(ball, ballrect)\n        pygame.display.flip()\n        pygame.time.delay(5)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I had a similar problem in a very simple piece of 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(&#8220;Golfball.png&#8221;) ballrect = ball.get_rect() while 1: event = pygame.event.poll() if event.type == pygame.QUIT: pygame.quit() ballrect = ballrect.move(speed) if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3323","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3323"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3323\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}