“After 10,000 years of development, hopefully it will be worth the wight.”
I have finished the GUI Module. This will double the speed at which the game is made and halve the size of the files!
Because this is open-source:
#PyGUI V.1.2
import pygame, random
from pygame.locals import *
pygame.init();
def help():
print “”“
If Screen((x, y), True) is active, it will run faster and smoother, but will take processing power.
Elif Screen((x, y), False) is not active, it will run blockier and some graphical functions may not work, but will take way less processing power.
Outdated Functions and Classes:
Funtions:
help()
mouse_pos()
set_fontsize(size)
Classes:
Screen(size, active)
Functions:
set_background(image)
set_button(image, (x, y), (x2, y2), title)
clicked(buttontitle)
hover(buttontitle)
set_button_image(image, buttontitle)
del_object(objecttitle)
update()
Variables:
objects
Button(image, (x, y), (x2, y2), title)
Variables:
image
title
x
y
x2
y2
Message(text, (x, y), title)
Variables:
title
text
x
y
Predifined Classes:
Font()
Functions:
set_fontsize(size)
set_fontcolour((R, G, B))
Variables:
size
colour
””“
class Screen:
def init(self, size, active = False):
self.objects = []
self.size = size
self.background = False
self.default_background = pygame.image.load(‘default.png’) #Unstable
self.active = active
def start(self):
self.scr = pygame.display.set_mode((self.size[0], self.size[1]))
def set_background(self, image):
self.background = pygame.image.load(image)
def set_button(self, image, pos, dim, title=”untitled_button”):
self.objects.append(Button(image, pos, dim, title))
def set_button2(self, text, pos, title=”untitled_button2”): #Remove dim
self.objects.append(Button2(text, pos, title))
def set_message(self, text, pos, title=’untitled_button’):
self.objects.append(Message(text, pos, title))
def set_image(self, image, pos):
self.objects.append(Image(image, pos))
#Referencing objects
def clicked(self, tit):
for obj in self.objects:
if obj.type != ‘image’:
if obj.title == tit:
if obj.clicked:
return True
def clicked_released(self, tit):
for obj in self.objects:
if obj.type != ‘image’:
if obj.title == tit:
if obj.hover:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN:
return True
def hover(self, tit):
for obj in self.objects:
if obj.type != ‘image’:
if obj.title == tit:
if obj.hover:
return True
def set_button_image(self, img, tit):
for obj in self.objects:
if obj.type != ‘image’:
if obj.title == tit:
obj.image = pygame.image.load(img)
def set_button_hover(self, img, tit):
for obj in self.objects:
if obj.type != ‘image’:
if obj.title == tit:
obj.image_hover = pygame.image.load(img)
def del_object(self, tit):
for obj in self.objects:
if obj.type != ‘image’:
if obj.title == tit:
del self.objects[self.objects.index(obj)]
def move_object(self, tit, pos):
for obj in self.objects:
if obj.type != ‘image’:
if obj.title == tit:
obj.x = pos[0]
obj.y = pos[1]
def align_object(self, tit, grid):
for obj in self.objects:
if obj.type != ‘image’:
if obj.title == tit:
while obj.x % grid != 0:
obj.x += 1
while obj.y % grid != 0:
obj.y += 1
#Main update loop
def update(self):
if self.active == False:
for event in pygame.event.get():
if event != None:
if self.background != False:
self.scr.blit(self.background, (0, 0))
else:
self.scr.blit(self.default_background, (0, 0))
for obj in self.objects:
obj.update(self.scr)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pass
pygame.display.update()
else:
if self.background != False:
self.scr.blit(self.background, (0, 0))
else:
self.scr.blit(self.default_background, (0, 0))
for obj in self.objects:
obj.update(self.scr)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pass
pygame.display.update()
class Button2:
def init(self, text, pos, title = “untitled_button2”):
self.type = “button”
self.text = text
self.title = title
self.x = pos[0]
self.y = pos[1]
self.dim = (len(self.text) * Font.size / 2, Font.size) #Dimensions (Need to format to font)
self.x2 = self.x + self.dim[0]
self.y2 = self.y + self.dim[1]
self.hover = False
self.clicked = False
def update(self, scr):
self.x2 = self.x + self.dim[0]
self.y2 = self.y + self.dim[1]
self.check_hover()
self.check_clicked()
if self.hover:
font = pygame.font.Font(None, Font.size)
temp_text = font.render(self.text, 1, (Font.colour))
pygame.draw.rect(scr, Font.colour, (self.x, self.y, self.dim[0], self.dim[1]), 4)
scr.blit(temp_text, (self.x + 8, self.y + 4))
else:
font = pygame.font.Font(None, Font.size)
temp_text = font.render(self.text, 1, (Font.colour))
pygame.draw.rect(scr, Font.colour, (self.x, self.y, self.dim[0], self.dim[1]), 2)
scr.blit(temp_text, (self.x + 8, self.y + 4))
def check_clicked(self):
if self.check_hover():
if pygame.mouse.get_pressed()[0] == 1:
self.clicked = True
else:
self.clicked = False
def check_hover(self):
m_p = mouse_pos()
mouse_x = m_p[0]
mouse_y = m_p[1]
if mouse_x > self.x and mouse_x < self.x2 and mouse_y > self.y and mouse_y < self.y2:
self.hover = True
return True
else:
self.hover = False
return False
class Button:
def init(self, image, pos, dim, title=’untitled_button’):
self.type = “button”
self.image = pygame.image.load(image)
self.title = title
self.x = pos[0]
self.y = pos[1]
self.dimx = dim[0]
self.dimy = dim[1]
self.x2 = self.x + self.dimx
self.y2 = self.y + self.dimy
self.image_default = self.image
self.image_hover = False
self.hover = False
self.clicked = False
def update(self, scr):
self.x2 = self.x + self.dimx
self.y2 = self.y + self.dimy
self.check_hover()
self.check_clicked()
#Updating for hover image
if self.image_hover != False:
if self.hover:
self.image = self.image_hover
else:
self.image = self.image_default
scr.blit(self.image, (self.x, self.y))
def check_clicked(self):
if self.check_hover():
if pygame.mouse.get_pressed()[0] == 1:
#for event in pygame.event.get(): <——- A possibility
#if event.type == MOUSEBUTTONDOWN:
self.clicked = True
else:
self.clicked = False
def check_hover(self):
m_p = mouse_pos()
mouse_x = m_p[0]
mouse_y = m_p[1]
if mouse_x > self.x and mouse_x < self.x2 and mouse_y > self.y and mouse_y < self.y2:
self.hover = True
return True
else:
self.hover = False
return False
class Message: #Not functioning
def init(self, text, pos, title = ‘untitled_message’):
self.type = “message”
self.title = title
self.text = text
self.x = pos[0]
self.y = pos[1]
def update(self, scr):
font = pygame.font.Font(None, Font.size)
temp_text = font.render(self.text, 1, (Font.colour))
scr.blit(temp_text, (self.x, self.y))
class Image:
def init(self, image, pos):
self.image = pygame.image.load(image)
self.type = ‘image’
self.x = pos[0]
self.y = pos[1]
def update(self, scr):
scr.blit(self.image, (self.x, self.y))
def mouse_pos():
return pygame.mouse.get_pos()
class FONT():
def init(self):
self.size = 10
self.colour = (100, 50, 150)
def set_size(self, size):
self.size = size
def set_colour(self, colour):
self.colour = colour
Font = FONT();
1 comment