This is my first ever try at making reusable code. The function makes use of two images to give the illusion of turning on and turning off a button in pygame.
I would like to know what would you do differently and if I did a good job? Thanks everyone.
Also, this is assuming you already loaded your images and gave it a name using pygame.image.load() function in pygame.
"""
The function `mouse_on_button() takes all the arguments needed
to produce a light-able button. For example, you have an image
of a thumbs up in grey and another image of a thumbs up in bright red and stack the on top of each other.
They are called by clicking the position of the image. By clicking they dark image will get transformed into the lit up image and vice-versa.
thumbs_up_img = pygame.image.load('thumbsup.png')
thumbs_up_lit_up_img = pygame.image.load('thumbsupred.png')
thumbs_up = Buttons(60, 240, 50, 50) # X, Y, Width, Height
mouse_on_button(pygame, screen, thumbs_up_img, thumbs_up_lit_up_img,thumbs_up.btn_x, thumbs_up.btn_y, thumbs_up.width, thumbs_up.height, SCREEN_HEIGHT, SCREEN_WIDTH)
"""
class Buttons:
"""
This class takes the x-position and the y-position, width and height of an image.
"""
def __init__(self, btn_x, btn_y, width, height):
self.btn_x = btn_x
self.btn_y = btn_y
self.width = width
self.height = height
def mouse_on_button(pygame, screen, off_btn, on_btn, btn_x, btn_y, width, height, SCREEN_HEIGHT, SCREEN_WIDTH):
"""
This function takes a dark image and a bright image to give the illusion of turning on a button.
"""
# Get mouse position.
mouse = pygame.mouse.get_pos()
# Setting up the boundries to light up or turn off around the button.
if (mouse[0] >= btn_x and mouse[0] <= btn_x + width) and (mouse[1] >= btn_y and mouse[1] <= btn_y + height):
# This will return the ON value of an image in the screen.
return screen.blit(on_btn,(btn_x, btn_y, SCREEN_HEIGHT, SCREEN_WIDTH))
else:
# This will return the OFF value of an image in the screen.
return screen.blit(off_btn,(btn_x, btn_y, SCREEN_HEIGHT, SCREEN_WIDTH))
mouse_on_buttonfunction \$\endgroup\$DISPLAY_HEIGHTandDISPLAY_WIDTHglobal constant values? \$\endgroup\$