I should have taken the time to respond to the question asked rather than hocking my wares.
import pygame
from pygame.locals import *
pygame.init()
#Text work
#This is meant to draw a custom size font from a specific folder
#and print it onto a rect taken from the size of the text, and centered on the chosen x,y coords.
def drawText(text, font, color, surface, x, y):
fontSize = int(length//len(text))
font = str(font)
textFont = pygame.font.Font(('C://ExampleDir/'+font+'.ttf')
textobj = textFont.render(text, 1, color)
textrect = textobj.get_rect()
textrect.center = (x, y)
surface.blit(textobj, textrect)
drawText isn't used anywhere? Any reason it's not a part of the class?
#Buttons
class Button:
#Here I'm trying to create a rect from the button's image data, so I can (in theory) check for mouse interaction with the button
def create_button(text, font, color, surface, length, height, x, y, image):
buttonSurface = menuButton(text, font, color, surface, length, height, x, y, image)
return buttonSurface
#This definition is meant to print a chosen custom font, in custom size and color, onto a
#rect with a chosen image as the backdrop; basically creates in one function the button itself
def menuButton(text, font, color, surface, length, height, x, y, image):
fontSize = int(length//len(text))
font = str(font)
buttonFont = pygame.font.Font(('C://ExampleDir/'+font+'.ttf'), fontSize)
textobj = buttonFont.render(text, 1, color)
textrect = textobj.get_rect()
textrect.center = (x, y)
button = textrect
button = button.inflate((int(length*.08)), (int(height*.15)))
menubutton = pygame.transform.scale(image, (button.width, button.height))
surface.blit(menubutton, button)
surface.blit(textobj, textrect)
return surface
These two methods should be refactored. Button initialization should be moved into __init__ and button drawing should be moved into a different function, likely named draw. The variables inherent to a Button should be turned into instance members.
As it is used currently, you're returning a surface object but never assigning it to anything, so it is immediately discarded.
#Should take the mouse's x,y coordinates and compare them to the rect's borders, and flag as true if they intersect
def mouse_over(buttonSurface, mouse):
if mouse[0] > buttonSurface.rect.topleft[0]:
if mouse[1] > buttonSurface.rect.topleft[1]:
if mouse[0] < buttonSurface.rect.bottomright[0]:
if mouse[1] < buttonSurface.rect.bottomright[1]:
return True
else: return False
else: return False
else: return False
else: return False
This could be replaced with
this. If everything is properly factored later, this could be a parameter-less function. buttonSurface should be a data member of Button and the mouse coords are easily available from everywhere.
import random, pygame, sys, Button
from pygame.locals import *
#setting up global variables
WINDOWWIDTH = 700
WINDOWHEIGHT = 700
FPS = 40
WHITE=(255,255,255)
DGREY=(55,55,55)
BLACK=(0,0,0)
RED= (255,0,0)
pygame.init()
#setting up the window, mouse, and background
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Button Test')
pygame.mouse.set_pos(300,300)
windowSurface.fill(BLACK)
#importing image
exampleImage = pygame.image.load('C://ExampleDir/ExampleImage.png')
#setting up the button
#Parameters: text, font, color, surface, x, y,length, height, image
Button1.create_button('Text', FontName, DGREY, windowSurface, 300, 200, 200, 100, exampleImage)
If this is an explanation, shouldn't this be commented out as well? Button1 doesn't exist yet.
#run the loop
Button1 = Button.Button()
while True:
#setting up the button
#Parameters: text, font, color, surface, x, y,length, height, image
Button1.create_button('Text', FontName, DGREY, windowSurface, 300, 200, 200, 100, exampleImage)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == MOUSEMOTION:
if self.Button1.mouse_over(pygame.mouse.get_pos()):
print('Mousover')
If you refactor create_button and menuButton as I suggested, you could create one button before the loop and not have to repeatedly create them within.
I recommend reading up on an introduction to object-oriented programming in Python.