Python Forum
Pygame - Images As Buttons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame - Images As Buttons
#1
Hi,

I've been trying to develop a game w/ Pygame.

One of the items in my game is a "Goodbye" button.
This is a png file.

The Goodbye image will be placed at a certain location.
Let's call it (x1, y1).

How do I make it into a button which will call "quit()"
if the user clicks on the goodbye image/button?

Thanks.
Reply
#2
There is a tutorial on the forum here.

Do you work with classes? They are the answer to 7/10th of pygame questions.
Reply
#3
You should learn to break it down to parts.
1. You need a surface.
2. You need a position.
3. You need a callback
4. You need event action function.

Now you need a structure to hold data. There many ways to build it and store. Here two ways. Dict and a class. I would recommend the class. But do what you like.

Dict example
def create_button(button, image, position, callback):
    button["image"] = image
    button["rect"] = image.get_rect(topleft=position)
    button["callback"] = callback

def button_on_click(button, event):
    if event.button == 1:
        if button["rect"].collidepoint(event.pos):
            button["callback"](button)

def push_button_goodbye(button):
    print("You push Goodbye button")

# Define your button
button = {}
# Create button
create_button(button, your_image, (10, 10), push_button_goodbye)
# In event loop see if button is trigger. Under pygame.MOUSEBUTTONDOWN.
button_on_click(button, event)
# In main loop draw area.
surface.blit(button["image"], button["rect"])
Class example
class Button:
    def __init__(self, image, position, callback):
        self.image = image
        self.rect = image.get_rect(topleft=position)
        self.callback = callback

    def on_click(self, event):
        if event.button == 1:
            if self.rect.collidepoint(event.pos):
                self.callback(self)

# Define and create button
button = Button(your_image, (10, 10), push_button_goodbye)
# In event loop. Under pygame.MOUSEBUTTONDOWN.
button.on_click(event)
# In main loop draw area.
surface.blit(button.image, button.rect)
99 percent of computer problems exists between chair and keyboard.
Reply
#4
Thanks.

I'll check it out.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] pygame-manu : using controller buttons to move around menu mlw19mlw91 2 1,543 Mar-12-2023, 01:55 PM
Last Post: deanhystad
  [PyGame] drawing images onto pygame window djwilson0495 1 3,423 Feb-22-2021, 05:39 PM
Last Post: nilamo
  [pygame] Equiping inventory slots with invisible buttons SheeppOSU 6 4,660 Apr-26-2019, 08:45 PM
Last Post: SheeppOSU
  Problems with loading buttons (pygame) SheeppOSU 2 3,051 Apr-12-2019, 08:04 PM
Last Post: SheeppOSU
  Pygame loads only part of images Prof_Jar_Jar 3 3,011 Dec-22-2018, 03:42 PM
Last Post: Windspar
  Buttons in PyGame mzmingle 4 12,952 Oct-09-2018, 05:19 PM
Last Post: Mekire
  Menus, buttons and other widgets for Pygame Olleus 4 11,411 Apr-17-2017, 11:08 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020