Pygame basic tutorial 13: Use Sprite to implement buttons, switch buttons and check boxes

Original link:https://xiets.blog.csdn.net/article/details/131395471

Copyright Statement: Reprinting of original articles is prohibited

Column directory:Pygame column (general directory)

There are no basic interactive components such as buttons, switch buttons, and check boxes in Pygame, but they are often used in actual game development. For example, the "Start Button" is used to click the button to start the game, and the "Switch Button" or "Check Box" is used to turn on/off the background music. In this section we will use sprites to implement these components.

1. Button: Button

Write a sprite-derived class that implements the Button effect, uses two Surfaces as images displayed in the button's pressed state and normal state, and supports click button callback monitoring. As needed, the button can also add other states, such as unavailable state and mouse over state.

The following two pictures need to be used in the code example, as images of the button's pressed and normal states respectively. Save them to the directory where the source code file is located, and name them respectively:button_normal.png andbutton_press.png

button_normal.png

Guess you like

Origin blog.csdn.net/xietansheng/article/details/131395471