Advertisement
Piorjade

PyGame basic UI module (part of TestProgram)

Sep 15th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. """
  2.  
  3.        UI module for pygame
  4.  
  5.    This (very) basic module just reduces code
  6.    to make buttons / UI elements in pygame.
  7.  
  8.    ~Piorjade
  9.  
  10. """
  11. import pygame
  12.  
  13. class Element:
  14.     __posX = None
  15.     __posY = None
  16.     __width = None
  17.     __height = None
  18.     __type = "Element"
  19.     def __init__(self, typ, x, y, w, h):
  20.         # The size is given as a float between 0 and 1, that way the element scales
  21.         # Typ basically is a string which defines the type of the element (currently no use)
  22.         self.__posX = x
  23.         self.__posY = y
  24.         self.__width = w
  25.         self.__height = h
  26.         self.__type = typ
  27.     def getX(self): return self.__posX
  28.     def getY(self): return self.__posY
  29.     def setX(self, x): self.__posX = x
  30.     def setY(self, y): self.__posY = y
  31.     def getW(self): return self.__width
  32.     def getH(self): return self.__height
  33.     def setW(self, w): self.__width = w
  34.     def setH(self, h): self.__height = h
  35.     def getType(self): return self.__type
  36.  
  37. class Button(Element):
  38.     __color = None
  39.     __text = None
  40.     __font = None
  41.     def __init__(self, x, y, w, h, color, text, font):
  42.         # Self explanatory
  43.         Element.__init__(self, "Button", x, y, w, h)
  44.         self.__color = color
  45.         self.__text = text
  46.         self.__font = font
  47.     def getColor(self): return self.__color
  48.     def setColor(self, color): self.__color = color
  49.     def getText(self): return self.__text
  50.     def setText(self, text): self.__text = text
  51.     def draw(self, screen):
  52.         # transform to real points
  53.         size = screen.get_size()
  54.        
  55.         x = size[0] * self.getX()
  56.         y = size[1] * self.getY()
  57.         w = size[0] * self.getW()
  58.         h = size[1] * self.getH()
  59.  
  60.         # draw rect
  61.         pygame.draw.rect(screen, self.__color, (x, y, w, h), 1)
  62.         screen.fill(self.__color, (x, y, w, h))
  63.  
  64.         label = self.__font.render(self.__text, 1, (0, 0, 0))
  65.         screen.blit(label, (x+(w/2)-(len(self.__text)/2), y+(h/2)))
  66.     def isClick(self, screen, mx, my):
  67.         # transform to real points
  68.         size = screen.get_size()
  69.         x = size[0] * self.getX()
  70.         y = size[1] * self.getY()
  71.         w = size[0] * self.getW()
  72.         h = size[1] * self.getH()
  73.  
  74.         #Check click
  75.         if mx >= x and mx <= x+w and my >= y and my <= y+h:
  76.             return True
  77.         else:
  78.             return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement