Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8053393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:49:44+00:00 2026-06-05T07:49:44+00:00

I’ve done a google search and searched through my two python beginner books to

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:49:45+00:00Added an answer on June 5, 2026 at 7:49 am

    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.

    import pygame, sys
    from pygame.locals import *
    
    TIMER = 30
    SCREEN_X = 200
    SCREEN_Y = 200
    
    screen = pygame.display.set_mode((SCREEN_X, SCREEN_Y))
    clock = pygame.time.Clock() #tick-tock
    
    ending = button1 = button2 = False
    
    corner1 = (28,18)  #Top Left corner of button 1
    corner2 = (56,18)  #Top Left corner of button 2
    
    image_length = 100 #length of the buttons
    image_height = 100 #height of the buttons
    
    counter = 0
    
    #Main Loop:
    while ending==False:
        counter+=1
        clock.tick(TIMER)
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    ending=True # Time to leave
                    print("Game Stopped Early by user")
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    mouse_x, mouse_y = event.pos
                    if (mouse_x >= corner1[0]) and (mouse_x <= corner1[0]+image_length) and (mouse_y >= corner1[1]) and (mouse_y <= corner1[1]+image_height):
                        print ("Button one is selected")
                        button1=True
                        button2=False
                    elif (mouse_x >= corner2[0]) and (mouse_x <= corner2[0]+image_length) and (mouse_y >= corner2[1]) and (mouse_y <= corner2[1]+image_height):
                        print ("Button two is selected")
                        button1=False
                        button2=True
                    else:
                        print ("That's not a button")
                        button1=False
                        button2=False
        if counter == TIMER:  #prints the statements once a second
            counter=0
            if button1==True:
                print ("Button one is currently selected")
            elif button2==True:
                print ("Button two is currently selected")
            else:
                print ("No buttons currently selected")
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.