I've done a google search and searched through my two python beginner books to find how to do this. I assume it has to be a simple task. Basically, I'm working with pygame with python.
I want that if I click on the button1_image, it changes to button1select_image, right? And if you click on button2_image, it sets button1select_image back to button1_image, and button2_image changes to button2select_image.
So what I am wondering is if it is a simple if else statement or is it much more complicated. Obviously, the buttons will do something else later but I cannot find a tutorial on how to do something like this based on the click of the users mouse.
# Button Mouse Click Image Change
# Demonstrates changing from one button image to another based on click of mouse.
from livewires import games, color
games.init(screen_width = 281, screen_height = 500, fps = 50)
button1_image = games.load_image("button1.png", transparent = False)
button1 = games.Sprite(image = button1_image, x = 28,y = 18)
games.screen.add(button1)
button1select_image = games.load_image("button1select.png", transparent = False)
button1select = games.Sprite(image = button1select_image, x = 28,y = 18)
games.screen.add(button1select)
button2_image = games.load_image("button2.png", transparent = False)
button2 = games.Sprite(image = button2_image, x = 56,y = 18)
games.screen.add(button2)
button2select_image = games.load_image("button2select.png", transparent = False)
button2select = games.Sprite(image = button2select_image, x = 56,y = 18)
games.screen.add(button2select)
games.screen.mainloop()
Take a look at this example menu : https://stackoverflow.com/a/10747990/341744 specifically at his code: https://gist.github.com/2802185
It creates a class
Option
, which changes color when you mouseover. You could extend it, so on click, the button calls a function. ( ie:new_game()
,show_options()
etc )Here's some pseudocode:
Here, I whipped this up to show how the mouse works. The line
if event.button == 1:
checks if the left mouse button has been pressed, change the 1 to a 2 if you want the right mouse button.At the print statements at the bottom. Simply use the selected image for buttons 1 or 2 if the button1 or button2 variable, respectively, is
True
. The else would be if none are selected so you have both of the images as the unselected button. If you don't know how to use images and the like, have a look around here: http://www.pygame.org/docs/ It really helped me. Try it out yourself, and if you're still stuck Stack Exchange will still be here for your questions :)Hope it helps