How to make buttons in python/pygame?

前端 未结 5 1987
粉色の甜心
粉色の甜心 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:11

    I don't have a code example for you, but how I would do it is to:

    1. Make a Button class, with the text to go on the button as a constructor argument
      1. Create a PyGame surface, either of an image or filled Rect
      2. Render text on it with the Font.Render stuff in Pygame
    2. Blit to game screen, save that rect.
    3. Check, on mouse click, to see the mouse.get_pos() matches a coord in the rect that it returned by the blit of the button to the main surface.

    That is similar to what your example is doing, although different still.

提交回复
热议问题