Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- UI module for pygame
- This (very) basic module just reduces code
- to make buttons / UI elements in pygame.
- ~Piorjade
- """
- import pygame
- class Element:
- __posX = None
- __posY = None
- __width = None
- __height = None
- __type = "Element"
- def __init__(self, typ, x, y, w, h):
- # The size is given as a float between 0 and 1, that way the element scales
- # Typ basically is a string which defines the type of the element (currently no use)
- self.__posX = x
- self.__posY = y
- self.__width = w
- self.__height = h
- self.__type = typ
- def getX(self): return self.__posX
- def getY(self): return self.__posY
- def setX(self, x): self.__posX = x
- def setY(self, y): self.__posY = y
- def getW(self): return self.__width
- def getH(self): return self.__height
- def setW(self, w): self.__width = w
- def setH(self, h): self.__height = h
- def getType(self): return self.__type
- class Button(Element):
- __color = None
- __text = None
- __font = None
- def __init__(self, x, y, w, h, color, text, font):
- # Self explanatory
- Element.__init__(self, "Button", x, y, w, h)
- self.__color = color
- self.__text = text
- self.__font = font
- def getColor(self): return self.__color
- def setColor(self, color): self.__color = color
- def getText(self): return self.__text
- def setText(self, text): self.__text = text
- def draw(self, screen):
- # transform to real points
- size = screen.get_size()
- x = size[0] * self.getX()
- y = size[1] * self.getY()
- w = size[0] * self.getW()
- h = size[1] * self.getH()
- # draw rect
- pygame.draw.rect(screen, self.__color, (x, y, w, h), 1)
- screen.fill(self.__color, (x, y, w, h))
- label = self.__font.render(self.__text, 1, (0, 0, 0))
- screen.blit(label, (x+(w/2)-(len(self.__text)/2), y+(h/2)))
- def isClick(self, screen, mx, my):
- # transform to real points
- size = screen.get_size()
- x = size[0] * self.getX()
- y = size[1] * self.getY()
- w = size[0] * self.getW()
- h = size[1] * self.getH()
- #Check click
- if mx >= x and mx <= x+w and my >= y and my <= y+h:
- return True
- else:
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement