Advertisement
Karp_xD

Button

May 16th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. import pygame
  2. pygame.init()
  3. class Button:
  4.     def __init__(self, x, y, image):
  5.         self.width = image.get_width()
  6.         self.height = image.get_height()
  7.         self.image = image
  8.         self.rect = self.image.get_rect()
  9.         self.rect.topleft = (x, y)
  10.         self.clicked = False
  11.     def draw(self, win):
  12.         win.blit(self.image, (self.rect.x, self.rect.y))
  13.         action = False
  14.         pos = pygame.mouse.get_pos()
  15.         if self.rect.collidepoint(pos) and self.clicked == False:
  16.             if pygame.mouse.get_pressed()[0] == 1:
  17.                 self.clicked = True
  18.                 action = True
  19.         if pygame.mouse.get_pressed()[0] == 0:
  20.             self.clicked = False
  21.             action = False
  22.         return action
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement