Trying to figure out how to track Pygame events and organize the game's functions

后端 未结 1 855
一向
一向 2021-01-16 03:31

I\'m new to Pygame, so I\'m still struggling with the whole \"events\" concept.

Basically, my current challenge is to:

  1. Get pygame.event.get() workin

相关标签:
1条回答
  • 2021-01-16 03:44

    Stick with a single main loop, and don't use threads if you don't really need them.

    You already figured out that your game consists of different screens (let's call them Scene so we don't confuse it with the actual screen where we draw stuff), so the next logical step is to seperate them out from the main loop.


    Say you have the following scenes:

    • intro
    • main menu
    • game play

    then you should create a class for each of those, which will then be responsible for drawing/event handling/whatever-needed-in-this-part.

    A simple example:

    import pygame
    pygame.init()
    
    class Scene(object):
        screen = None
    
    class Intro(Scene):
        def __init__(self):
            self.c = (32, 32, 100)
    
        def draw(self):
            Scene.screen.fill(self.c)
    
        def update(self):
            # since scenes are classes, they have a state that we can modify
            r,g,b = self.c
            r += 1
            g += 1
            b += 2
            if r > 255: r = 0
            if g > 255: g = 0
            if b > 255: b = 0
            self.c = r, g, b
    
        def handle(self, event):
            # move to Menu-scene when space is pressed
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    # returning a scene from handle or update changes the current scene
                    return Menu()
    
    class Menu(Scene):
        def draw(self):
            # draw menu
            Scene.screen.fill((200, 200, 100))
    
        def update(self):
            pass
            # do something
    
        def handle(self, event):
            # handle menu input
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:
                    return Game()
                if event.key == pygame.K_b:
                    return Intro()
    
    class Game(Scene):
        pass # implement draw update handle
    
    Scene.screen = pygame.display.set_mode((800, 600))
    
    scene = Intro()
    while True:
        if pygame.event.get(pygame.QUIT): break
        for e in pygame.event.get(): 
            scene = scene.handle(e) or scene
        scene = scene.update() or scene
        scene.draw()
        pygame.display.flip()
    

    This way, you have a single mainloop, but different parts of your game are managed by seperate classes. In the simple example above, each scene knows what other scene to activate on a certain event, but this is not necessary. You could create some kind of SceneHandler that manages the transitions between the scenes. Also, you may want to have some kind of game state object that you want to pass around scenes (e.g. show your points from the game scene in your game-over scene).

    0 讨论(0)
提交回复
热议问题