button
1import pygame 2 3""" 4Python file for button objects 5 6Button.py is creates a button object out of an image, position, and the images scale. 7All of the features aren't fully implemented as plan, main.py currently handles the button logic 8while the Button class creates the rectangle and its dimensions, and draws the image on screen. 9 10Typical usage example: 11 12 pause_button = Button(100, 200, pause_button.pdf, .5) 13 pause_button.draw 14""" 15 16class Button(): 17 """button objects 18 19 Attributes: 20 width: an int representing the original images width 21 height: an int representing the original images height 22 image: an image file that is passed through the constructor 23 rect: a rect object for the image 24 rect.topleft: an int representing the top left coordinate for the image 25 clicked: a boolean representing whether the button has been clicked 26 """ 27 28 29 def __init__(self, x, y, image, scale): 30 """initializes an instance based on the image, it's position and scale 31 32 Args: 33 x: defines x coordinate for instance 34 y: defines y coordinate for instance 35 image: the image file for instance 36 scale: scale for the new image relative to the original 37 """ 38 width = image.get_width() 39 height = image.get_height() 40 self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale))) 41 self.rect = self.image.get_rect() 42 self.rect.topleft = (x, y) 43 self.clicked = False 44 45 def draw(self, surface): 46 """draws the button and returns true if it is clicked 47 48 Args: 49 surface: the screen/surface that the button will be drawn on (window). 50 51 Returns: 52 Action - a boolean that is true when the button is clicked 53 """ 54 action = False 55 56 # get mouse position 57 pos = pygame.mouse.get_pos() 58 59 # Check if mouse is clicked 60 if self.rect.collidepoint(pos): 61 if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: 62 self.clicked = True 63 action = True 64 65 if pygame.mouse.get_pressed()[0] == 0: 66 self.cliciked = False 67 68 surface.blit(self.image, (self.rect.x, self.rect.y)) 69 70 return action
17class Button(): 18 """button objects 19 20 Attributes: 21 width: an int representing the original images width 22 height: an int representing the original images height 23 image: an image file that is passed through the constructor 24 rect: a rect object for the image 25 rect.topleft: an int representing the top left coordinate for the image 26 clicked: a boolean representing whether the button has been clicked 27 """ 28 29 30 def __init__(self, x, y, image, scale): 31 """initializes an instance based on the image, it's position and scale 32 33 Args: 34 x: defines x coordinate for instance 35 y: defines y coordinate for instance 36 image: the image file for instance 37 scale: scale for the new image relative to the original 38 """ 39 width = image.get_width() 40 height = image.get_height() 41 self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale))) 42 self.rect = self.image.get_rect() 43 self.rect.topleft = (x, y) 44 self.clicked = False 45 46 def draw(self, surface): 47 """draws the button and returns true if it is clicked 48 49 Args: 50 surface: the screen/surface that the button will be drawn on (window). 51 52 Returns: 53 Action - a boolean that is true when the button is clicked 54 """ 55 action = False 56 57 # get mouse position 58 pos = pygame.mouse.get_pos() 59 60 # Check if mouse is clicked 61 if self.rect.collidepoint(pos): 62 if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: 63 self.clicked = True 64 action = True 65 66 if pygame.mouse.get_pressed()[0] == 0: 67 self.cliciked = False 68 69 surface.blit(self.image, (self.rect.x, self.rect.y)) 70 71 return action
button objects
Attributes: width: an int representing the original images width height: an int representing the original images height image: an image file that is passed through the constructor rect: a rect object for the image rect.topleft: an int representing the top left coordinate for the image clicked: a boolean representing whether the button has been clicked
30 def __init__(self, x, y, image, scale): 31 """initializes an instance based on the image, it's position and scale 32 33 Args: 34 x: defines x coordinate for instance 35 y: defines y coordinate for instance 36 image: the image file for instance 37 scale: scale for the new image relative to the original 38 """ 39 width = image.get_width() 40 height = image.get_height() 41 self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale))) 42 self.rect = self.image.get_rect() 43 self.rect.topleft = (x, y) 44 self.clicked = False
initializes an instance based on the image, it's position and scale
Args: x: defines x coordinate for instance y: defines y coordinate for instance image: the image file for instance scale: scale for the new image relative to the original
46 def draw(self, surface): 47 """draws the button and returns true if it is clicked 48 49 Args: 50 surface: the screen/surface that the button will be drawn on (window). 51 52 Returns: 53 Action - a boolean that is true when the button is clicked 54 """ 55 action = False 56 57 # get mouse position 58 pos = pygame.mouse.get_pos() 59 60 # Check if mouse is clicked 61 if self.rect.collidepoint(pos): 62 if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False: 63 self.clicked = True 64 action = True 65 66 if pygame.mouse.get_pressed()[0] == 0: 67 self.cliciked = False 68 69 surface.blit(self.image, (self.rect.x, self.rect.y)) 70 71 return action
draws the button and returns true if it is clicked
Args: surface: the screen/surface that the button will be drawn on (window).
Returns: Action - a boolean that is true when the button is clicked