I wanted to simulate the hover effect on the button when you pass the pointer of the mouse on in pygame. So here it is my first attempt. It works only on button 1. I will make it more easy to achieve for every button in the next post.
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
def button(screen, position, text, size, colors="white on blue"):
fg, bg = colors.split(" on ")
font = pygame.font.SysFont("Arial", size)
text_render = font.render(text, 1, fg)
x, y, w , h = text_render.get_rect()
x, y = position
pygame.draw.line(screen, (150, 150, 150), (x, y), (x + w , y), 5)
pygame.draw.line(screen, (150, 150, 150), (x, y - 2), (x, y + h), 5)
pygame.draw.line(screen, (50, 50, 50), (x, y + h), (x + w , y + h), 5)
pygame.draw.line(screen, (50, 50, 50), (x + w , y+h), [x + w , y], 5)
pygame.draw.rect(screen, bg, (x, y, w , h))
return screen.blit(text_render, (x, y))
def start():
print("Ok, let's go")
def hover(button):
if button.collidepoint(pygame.mouse.get_pos()):
button = button(screen, (300, 300), "Quit me", 50, "red on green")
else:
button = button(screen, (300, 300), "Quit me", 50, "red on yellow")
def menu():
""" This is the menu that waits you to click the s key to start """
b0 = button(screen, (10, 10), "Here comes the buttons", 55, "red on yellow")
b1 = button(screen, (300, 300), "Quit me", 50, "red on yellow")
b2 = button(screen, (500, 300), "Start", 50, "white on green")
while True:
for event in pygame.event.get():
if (event.type == pygame.QUIT):
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
key_to_start = event.key == pygame.K_s or event.key == pygame.K_RIGHT or event.key == pygame.K_UP
if key_to_start:
start()
if event.type == pygame.MOUSEMOTION:
if b1.collidepoint(pygame.mouse.get_pos()):
b1 = button(screen, (300, 300), "Quit me", 50, "red on green")
else:
b1 = button(screen, (300, 300), "Quit me", 50, "red on yellow")
if event.type == pygame.MOUSEBUTTONDOWN:
if b1.collidepoint(pygame.mouse.get_pos()):
pygame.quit()
elif b2.collidepoint(pygame.mouse.get_pos()):
start()
pygame.display.update()
pygame.quit()
menu()

https:\\github.com\formazione\pygame_button
Subscribe to the newsletter for updates
Tkinter templatesTwitter: @pythonprogrammi - python_pygame
Claude's Games
1. Memory gameVideos
Speech recognition gamePygame's Platform Game