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

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0

    Python 2.7 + Pygame 1.9.1 problem.


    Hello.

    Having a problem here exiting my program, as the entire program works but if I try to implement a QUIT 'function', if you will, the program simply will not run and Iam not sure why (My 'quit' function is at the very bottom):

    Code:
    import pygame, sys, time, random
    
    pygame.init()
    pygame.display.set_caption("Star Citizen - A Roguelike game")
    
    print("THE GAME SCRIPT LOADED CORRECTLY")
    
    SWidth = 1280
    SHeight = 1024
    Screen = pygame.display.set_mode((SWidth, SHeight), 0, 32)
    
    #COLOURS BELOW HERE#
    White = (248, 248, 255)
    #COLOURS ABOVE HERE#
    
    def UserInterface():
        UIB = ("C:\\Star Citizen - The Roguelike\\Graphics\\InGameGraphics\\GameUI.png")
        UIB = pygame.image.load(UIB).convert_alpha()
        Screen.blit(UIB, (0,0))
        pygame.display.update()
        print("USER INTERFACE WAS LOADED CORRECTLY")
        
    def gameIntro():
        quoteGen = random.randint(1, 3)
        if quoteGen == 1:
            myfont = pygame.font.SysFont("Comic Sans MS", 26)
            label = myfont.render("'God has no intention of setting a limit to the efforts of man to conquer space' - Pope Pius XII", 1, White)
            Screen.blit(label, (70, 512))
            pygame.display.flip()
            print("INTRO QUOTE GENERATED CORRECTLY")
            time.sleep(6)
            Screen.fill(0)
            UserInterface()
            return
        elif quoteGen == 2:
            myfont = pygame.font.SysFont("Comic Sans MS", 26)
            label = myfont.render("'That's one small step for a man; one giant leap for mankind' - Neil A. Armstrong", 1, White)
            Screen.blit(label, (100, 512))
            pygame.display.flip()
            print("INTRO QUOTE GENERATED CORRECTLY")
            time.sleep(6)
            Screen.fill(0)
            UserInterface()
            return
        elif quoteGen == 3:
            myfont = pygame.font.SysFont("Comic Sans MS", 26)
            label = myfont.render("'Beep... beep... beep... beep' - Civ. IV, upon discovering satellites", 1, White)
            Screen.blit(label, (100, 512))
            pygame.display.flip()
            print("INTRO QUOTE GENERATED CORRECTLY")
            time.sleep(6)
            Screen.fill(0)
            UserInterface()
            return
    gameIntro()
    
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
    Any help on the matter would be much appreciated!

    Thanks.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481

    pygame.QUIT


    if event.type == pygame.QUIT:
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    8
    Rep Power
    0
    If there were a smiley for a face palm here, it would be well placed here.

    One day I hope I'll get there.

  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Aside from forgetting the pygame prefix on QUIT, there are several other things that you are doing that are big no-nos.

    For one you should never use time.sleep in a pygame. Pygame provides its own functions for controlling and managing time. Two; you can't cut your game off from your event loop like that. It may or may not crash your program in this case but the fact is, if pygame doesn't make calls to the event queue often enough (or call pygame.event.pump() in its place) your program can lock up.

    Also there is no reason to be calling pygame.display.update or pygame.display.flip in multiple places. This call should happen once every time through your game loop (once per frame).

    Your entire game should be within a while loop. This while loop should call your event loop, and then depending on what state you are in, call the pertinent update function.

    Here is an example of what main loops can look like (although in reality I would take advantage of polymorphism when updating the states):
    python Code:
    def main(self):
        while not self.done:
            self.event_loop()
            if self.state == "QUIT":
                self.done = True
            elif self.state == "TITLE":
                self.Title.update()
            elif self.state == "MENU":
                self.Menu.update()
            elif self.state == "GAME":
                self.Game.update()
            self.myclock.tick(self.fps)
            pygame.display.update()
        pygame.quit();sys.exit()

    I say this fairly often but it remains true. A game of even trivial complexity is going to really need to use classes (the above would be a method of the class controlling fundamental operation of our game). Yes, you can do it without classes but you will be making your job harder, not easier.

    -Mek
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    As far as naming the module from which the constant if found, pygame.QUIT, you'd be hard pressed to guess high against the number of times I've written the likes of

    import tkinter

    widget.pack(fill=BOTH)
    # instead of
    widget.pack(fill=tkinter.BOTH)
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    194
    Rep Power
    3
    Doesn't help that 90% of the pygame sample code online all includes:
    Code:
    from pygame.locals import *
    So half the time people reading code examples will see just that.

    (Don't do it though. It is horrible namespace polution.)
    -Mek

IMN logo majestic logo threadwatch logo seochat tools logo