Thread: Pygame Help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Pygame Help


    Hi guys,
    I'm very new to python and pygame and need a bit of help.

    I know how to make my program exit with KEYUP or KEYDOWN. I just want to know this: how would I write it so that I could exit the program with any other key, like the "A" key for example.

    Thank You
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    615
    Rep Power
    65
    The "a" key would be simpler.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Yea, that is what I meant. I just want to know how I would be able to close the program by pressing one specific key.
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    615
    Rep Power
    65
    I modified a typical example I had ...
    Code:
    ''' pg_exit1.py
    exploring best ways to exit a Pygame loop
    for key codes see:
    http://www.pygame.org/docs/ref/key.html
    '''
    
    import pygame as pg
    
    pg.init()
    
    # create a 300 x 300 pixel display window
    # the default background is black
    win = pg.display.set_mode((300, 300))
    # optional title bar caption
    pg.display.set_caption('A Pygame Circle')
    
    # draw a white circle
    white = (255, 255, 255)
    center = (150, 150)
    radius = 100
    # width of 0 (default) fills the circle
    # otherwise it is thickness of outline
    width = 0
    # draw.circle(Surface, color, pos, radius, width)
    # width of 0 (default) fills the circle
    pg.draw.circle(win, white, center, radius, width)
    
    # update the display window to show the drawing
    pg.display.flip()
    
    # event loop and exit conditions using
    # specified key or display window x click
    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                # most reliable exit on x click
                pg.quit()
                raise SystemExit
            elif event.type == pg.KEYDOWN:
                # optional exit with "a" key
                if event.key == pg.K_a:
                    pg.quit()
                    raise SystemExit
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Thank You

IMN logo majestic logo threadwatch logo seochat tools logo