Hey, having a hard time coding a button for python (and pygame).
Here's my attempt thus far at the code:
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)
#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
#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
My goal was to save this file, and import it into my main code, and then be able to define a button using any given font and image I like, on any given x,y point (centered on that point, but I can switch it to topleft without much hassle). It doesn't seem to work when I try it, though.
Here's a sample code you guys can see how I'm trying to make it work.
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)
#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')