Calling a PyGame Function, from clicking a Object-Orientated PyGame Button

前端 未结 1 852
感动是毒
感动是毒 2020-11-29 13:46
import pygame as pg
import sys

pg.init()

buttonFont = pg.font.SysFont(\"garamond\", 25)

screenGray = pg.Color(\'gray80\')
buttonGray2 = pg.Color(\'gray50\')
textC         


        
1条回答
  •  渐次进展
    2020-11-29 14:16

    You have to call pg.display.flip() in the Menu function.

    I also have a little recommendation about the code structure. I'd use another function or class (main in this case) to manage the different scenes. So I first assign the current scene function to a variable and call it in the main while loop. When the scene is done, I return the next scene and assign it to the scene variable to swap the scenes. That will avoid potential recursion errors which you get if you just call the next function directly from within another scene (although it's unlikely that you'll exceed the recursion limit of 1000 in a simple game or app).

    import pygame as pg
    
    
    pg.init()
    screen = pg.display.set_mode((600, 400))
    clock = pg.time.Clock()
    BLUE = pg.Color('dodgerblue3')
    ORANGE = pg.Color('sienna3')
    
    
    def front_page():
        while True:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    return None
                # Press a key to return the next scene.
                elif event.type == pg.KEYDOWN:
                    return menu
    
            screen.fill(BLUE)
            pg.display.flip()
            clock.tick(60)
    
    
    def menu():
        while True:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    return None
                # Press a key to return the next scene.
                elif event.type == pg.KEYDOWN:
                    return front_page
    
            screen.fill(ORANGE)
            pg.display.flip()
            clock.tick(60)
    
    
    def main():
        scene = front_page  # Set the current scene.
        while scene is not None:
            # Execute the current scene function. When it's done
            # it returns either the next scene or None which we
            # assign to the scene variable.
            scene = scene()
    
    
    main()
    pg.quit()
    

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