Re: [pygame] Gracefully exiting with errors

2015-09-05 Thread Russell Jones
I use

try:
...
except:
  raise #show what went wrong
finally:
  pygame.display.quit()

This works for me with pygame_sdl2 in a python 3.4.0 virtualenv (using idle
with a script in the VE as per
http://stackoverflow.com/questions/4924068/how-to-launch-python-idle-from-a-virtual-environment-virtualenv
)
It closes cleanly in that case. python3-pygame isn't available in the
distro I'm using... OK, tried it in idle-python2.7 and it works there too.

Full example
import sys

try:
import pygame_sdl2 as pygame
except ImportError:
import pygame

pygame.init()

window_size = width, height = (800, 600)
speed = [5, 5]
background = (255, 144, 0) #colour. red, green and blue.

screen = pygame.display.set_mode(window_size)

ball = pygame.image.load("ball.gif")
ballrect = ball.get_rect()

keep_going = True

try: #get ready to deal with any problems
  while keep_going:
for event in pygame.event.get():
  if event.type == pygame.QUIT:
keep_going = False #exit the loop

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(background)
screen.blit(ball, ballrect)
pygame.display.flip()
except:
  raise #show what went wrong
finally:
  pygame.display.quit() #close the window


On 27 August 2015 at 05:29,  wrote:

> Some example code:
>
> Try:
> While running:
>   .pygame.update .etc
> Pygame.quit
> Except exception:
>   Pygame.quit()
>   Raise
>
> That way even if it errors we still call pygame.quit(), which is what
> isn't happening since the idle doesn't call garbage collection on some code
> on error.
>
>
>
>
> On Aug 26, 2015, at 14:23, Paul Vincent Craven 
> wrote:
>
> When using Wing, I teach students to hit the red 'stop' button. The issue
> is when the process errors, it does not quit, it pauses. Thus the windows
> stays open and is unresponsive. By hitting the red square 'stop' button,
> you kill the process.
>
> Paul Vincent Craven
>
> On Wed, Aug 26, 2015 at 1:29 PM, bw  wrote:
>
>> I agree with Ian. You should just be able to allow the script to end
>> without special closure. But you may also use sys.exit() and quit().
>> pygame.quit() is not usually needed, unless you intend to quit pygame and
>> let the program continue onto something else.
>>
>> What you're seeing, Bob, may be a problem with the IDE.
>>
>> There is consensus that IDLE is bad. It creates weird problems (with
>> pygame only? not sure how far it goes). Annoyances like those with IDLE
>> will likely turn your students off of Python.
>>
>> I don't know about WingIDE. You could try a different one. I really like
>> PyCharm, but it is vast in features and a memory hog--possibly not ideal
>> for beginners and school computers.
>>
>> For learning you might consider going with a light syntax-highlighting
>> editor and teaching students the use of the discrete pieces that an IDE
>> integrates for you. In other words teach fundamentals and let an IDE be an
>> elective choice for later. When it comes to ferreting out problems in the
>> dev environment, one needs to know what is going on behind the IDE. For me
>> it was very useful learning nuts and bolts, and then discovering what more
>> an IDE can do for me.
>>
>> Hope these tips are helpful.
>>
>> bw
>>
>>
>> On 8/26/2015 10:55 AM, Ian Mallett wrote:
>>
>> On Wed, Aug 26, 2015 at 11:38 AM, Bob Irving  wrote:
>>
>>> Is there a way to exit your game gracefully when there are errors? We
>>> have found with both IDLE and WingIDE that the game hangs, requiring
>>> several clicks of the X, etc.
>>>
>>> We are ending our game loop with
>>>
>>> pygame.quit()
>>> sys.exit()
>>>
>> ​I prefer to let the script terminate itself (i.e. fall out the bottom),
>> but sys.exit should work fine too. I remember having this issue with older
>> versions of Python, but 2.7 or 3.* should work fine.
>>
>> Here's some skeleton code
>> 
>> that I use.
>>
>> Ian
>>
>>
>>
>


Re: [pygame] Gracefully exiting with errors

2015-08-26 Thread rockachu2
Some example code:

Try:
While running:
  .pygame.update .etc
Pygame.quit
Except exception:
  Pygame.quit()
  Raise

That way even if it errors we still call pygame.quit(), which is what isn't 
happening since the idle doesn't call garbage collection on some code on error. 




> On Aug 26, 2015, at 14:23, Paul Vincent Craven  wrote:
> 
> When using Wing, I teach students to hit the red 'stop' button. The issue is 
> when the process errors, it does not quit, it pauses. Thus the windows stays 
> open and is unresponsive. By hitting the red square 'stop' button, you kill 
> the process.
> 
> Paul Vincent Craven
> 
>> On Wed, Aug 26, 2015 at 1:29 PM, bw  wrote:
>> I agree with Ian. You should just be able to allow the script to end without 
>> special closure. But you may also use sys.exit() and quit(). pygame.quit() 
>> is not usually needed, unless you intend to quit pygame and let the program 
>> continue onto something else.
>> 
>> What you're seeing, Bob, may be a problem with the IDE.
>> 
>> There is consensus that IDLE is bad. It creates weird problems (with pygame 
>> only? not sure how far it goes). Annoyances like those with IDLE will likely 
>> turn your students off of Python.
>> 
>> I don't know about WingIDE. You could try a different one. I really like 
>> PyCharm, but it is vast in features and a memory hog--possibly not ideal for 
>> beginners and school computers.
>> 
>> For learning you might consider going with a light syntax-highlighting 
>> editor and teaching students the use of the discrete pieces that an IDE 
>> integrates for you. In other words teach fundamentals and let an IDE be an 
>> elective choice for later. When it comes to ferreting out problems in the 
>> dev environment, one needs to know what is going on behind the IDE. For me 
>> it was very useful learning nuts and bolts, and then discovering what more 
>> an IDE can do for me.
>> 
>> Hope these tips are helpful.
>> 
>> bw
>> 
>> 
>>> On 8/26/2015 10:55 AM, Ian Mallett wrote:
 On Wed, Aug 26, 2015 at 11:38 AM, Bob Irving  wrote:
 Is there a way to exit your game gracefully when there are errors? We have 
 found with both IDLE and WingIDE that the game hangs, requiring several 
 clicks of the X, etc.
 
 We are ending our game loop with 
 
 pygame.quit()
 sys.exit()
>>> ​I prefer to let the script terminate itself (i.e. fall out the bottom), 
>>> but sys.exit should work fine too. I remember having this issue with older 
>>> versions of Python, but 2.7 or 3.* should work fine.
>>> 
>>> Here's some skeleton code that I use.
>>> 
>>> Ian
> 


Re: [pygame] Gracefully exiting with errors

2015-08-26 Thread rockachu2
I believe the issue your having is when the code throws an exception, 
pygame.quit() is never called ( in idle) so the window persists. 
A good way to deal with this is to wrap the execution in a try/except that 
calls pygame.quit then re raises the exception. 




> On Aug 26, 2015, at 14:23, Paul Vincent Craven  wrote:
> 
> When using Wing, I teach students to hit the red 'stop' button. The issue is 
> when the process errors, it does not quit, it pauses. Thus the windows stays 
> open and is unresponsive. By hitting the red square 'stop' button, you kill 
> the process.
> 
> Paul Vincent Craven
> 
>> On Wed, Aug 26, 2015 at 1:29 PM, bw  wrote:
>> I agree with Ian. You should just be able to allow the script to end without 
>> special closure. But you may also use sys.exit() and quit(). pygame.quit() 
>> is not usually needed, unless you intend to quit pygame and let the program 
>> continue onto something else.
>> 
>> What you're seeing, Bob, may be a problem with the IDE.
>> 
>> There is consensus that IDLE is bad. It creates weird problems (with pygame 
>> only? not sure how far it goes). Annoyances like those with IDLE will likely 
>> turn your students off of Python.
>> 
>> I don't know about WingIDE. You could try a different one. I really like 
>> PyCharm, but it is vast in features and a memory hog--possibly not ideal for 
>> beginners and school computers.
>> 
>> For learning you might consider going with a light syntax-highlighting 
>> editor and teaching students the use of the discrete pieces that an IDE 
>> integrates for you. In other words teach fundamentals and let an IDE be an 
>> elective choice for later. When it comes to ferreting out problems in the 
>> dev environment, one needs to know what is going on behind the IDE. For me 
>> it was very useful learning nuts and bolts, and then discovering what more 
>> an IDE can do for me.
>> 
>> Hope these tips are helpful.
>> 
>> bw
>> 
>> 
>>> On 8/26/2015 10:55 AM, Ian Mallett wrote:
 On Wed, Aug 26, 2015 at 11:38 AM, Bob Irving  wrote:
 Is there a way to exit your game gracefully when there are errors? We have 
 found with both IDLE and WingIDE that the game hangs, requiring several 
 clicks of the X, etc.
 
 We are ending our game loop with 
 
 pygame.quit()
 sys.exit()
>>> ​I prefer to let the script terminate itself (i.e. fall out the bottom), 
>>> but sys.exit should work fine too. I remember having this issue with older 
>>> versions of Python, but 2.7 or 3.* should work fine.
>>> 
>>> Here's some skeleton code that I use.
>>> 
>>> Ian
> 


Re: [pygame] Gracefully exiting with errors

2015-08-26 Thread Paul Vincent Craven
When using Wing, I teach students to hit the red 'stop' button. The issue
is when the process errors, it does not quit, it pauses. Thus the windows
stays open and is unresponsive. By hitting the red square 'stop' button,
you kill the process.

Paul Vincent Craven

On Wed, Aug 26, 2015 at 1:29 PM, bw  wrote:

> I agree with Ian. You should just be able to allow the script to end
> without special closure. But you may also use sys.exit() and quit().
> pygame.quit() is not usually needed, unless you intend to quit pygame and
> let the program continue onto something else.
>
> What you're seeing, Bob, may be a problem with the IDE.
>
> There is consensus that IDLE is bad. It creates weird problems (with
> pygame only? not sure how far it goes). Annoyances like those with IDLE
> will likely turn your students off of Python.
>
> I don't know about WingIDE. You could try a different one. I really like
> PyCharm, but it is vast in features and a memory hog--possibly not ideal
> for beginners and school computers.
>
> For learning you might consider going with a light syntax-highlighting
> editor and teaching students the use of the discrete pieces that an IDE
> integrates for you. In other words teach fundamentals and let an IDE be an
> elective choice for later. When it comes to ferreting out problems in the
> dev environment, one needs to know what is going on behind the IDE. For me
> it was very useful learning nuts and bolts, and then discovering what more
> an IDE can do for me.
>
> Hope these tips are helpful.
>
> bw
>
>
> On 8/26/2015 10:55 AM, Ian Mallett wrote:
>
> On Wed, Aug 26, 2015 at 11:38 AM, Bob Irving  wrote:
>
>> Is there a way to exit your game gracefully when there are errors? We
>> have found with both IDLE and WingIDE that the game hangs, requiring
>> several clicks of the X, etc.
>>
>> We are ending our game loop with
>>
>> pygame.quit()
>> sys.exit()
>>
> ​I prefer to let the script terminate itself (i.e. fall out the bottom),
> but sys.exit should work fine too. I remember having this issue with older
> versions of Python, but 2.7 or 3.* should work fine.
>
> Here's some skeleton code
> 
> that I use.
>
> Ian
>
>
>


Re: [pygame] Gracefully exiting with errors

2015-08-26 Thread bw
I agree with Ian. You should just be able to allow the script to end 
without special closure. But you may also use sys.exit() and quit(). 
pygame.quit() is not usually needed, unless you intend to quit pygame 
and let the program continue onto something else.


What you're seeing, Bob, may be a problem with the IDE.

There is consensus that IDLE is bad. It creates weird problems (with 
pygame only? not sure how far it goes). Annoyances like those with IDLE 
will likely turn your students off of Python.


I don't know about WingIDE. You could try a different one. I really like 
PyCharm, but it is vast in features and a memory hog--possibly not ideal 
for beginners and school computers.


For learning you might consider going with a light syntax-highlighting 
editor and teaching students the use of the discrete pieces that an IDE 
integrates for you. In other words teach fundamentals and let an IDE be 
an elective choice for later. When it comes to ferreting out problems in 
the dev environment, one needs to know what is going on behind the IDE. 
For me it was very useful learning nuts and bolts, and then discovering 
what more an IDE can do for me.


Hope these tips are helpful.

bw

On 8/26/2015 10:55 AM, Ian Mallett wrote:
On Wed, Aug 26, 2015 at 11:38 AM, Bob Irving > wrote:


Is there a way to exit your game gracefully when there are errors?
We have found with both IDLE and WingIDE that the game hangs,
requiring several clicks of the X, etc.

We are ending our game loop with

pygame.quit()
sys.exit()

​I prefer to let the script terminate itself (i.e. fall out the 
bottom), but sys.exit should work fine too. I remember having this 
issue with older versions of Python, but 2.7 or 3.* should work fine.


Here's some skeleton code 
 
that I use.


Ian




Re: [pygame] Gracefully exiting with errors

2015-08-26 Thread Ian Mallett
On Wed, Aug 26, 2015 at 11:38 AM, Bob Irving  wrote:

> Is there a way to exit your game gracefully when there are errors? We have
> found with both IDLE and WingIDE that the game hangs, requiring several
> clicks of the X, etc.
>
> We are ending our game loop with
>
> pygame.quit()
> sys.exit()
>
​I prefer to let the script terminate itself (i.e. fall out the bottom),
but sys.exit should work fine too. I remember having this issue with older
versions of Python, but 2.7 or 3.* should work fine.

Here's some skeleton code

that I use.

Ian


[pygame] Gracefully exiting with errors

2015-08-26 Thread Bob Irving
Hello all,

I teach Pygame in 9th grade computer science, and this is our first year
using Python/Pygame (we previously used BlitzBasic).

Is there a way to exit your game gracefully when there are errors? We have
found with both IDLE and WingIDE that the game hangs, requiring several
clicks of the X, etc.

We are ending our game loop with

pygame.quit()
sys.exit()

Am I missing something?

Thanks in advance,
Bob Irving
Porter-Gaud School
Charleston, SC

-- 
Twitter: @birv2
www.bob-irving.com
http://www.scoop.it/t/on-the-digital-frontier