How to make buttons in python/pygame?

前端 未结 5 1989
粉色の甜心
粉色の甜心 2020-12-16 22:35

I\'m making a game in pygame and on the first screen I want there to be buttons that you can press to (i) start the game, (ii) load a new screen with instructions, and (iii)

5条回答
  •  不知归路
    2020-12-16 23:18

    Another good way to create buttons on pygame (in Python) is by installing the package called pygame_widgets (pip3 install pygame_widgets).

    # Importing modules
    import pygame as pg
    import pygame_widgets as pw
    
    # Creating screen
    pg.init()
    screen = pg.display.set_mode((800, 600))
    running = True
    button = pw.Button(
        screen, 100, 100, 300, 150, text='Hello',
        fontSize=50, margin=20,
        inactiveColour=(255, 0, 0),
        pressedColour=(0, 255, 0), radius=20,
        onClick=lambda: print('Click')
    )
    

    While running:

    events = pg.event.get()
    for event in events:
        if event.type == pg.QUIT:
            running = False
    button.listen(events)
    button.draw()
    pg.display.update()
    

提交回复
热议问题