So I'm creating a menu for a game and I have made a button function. The buttons do work but only sometimes:
- The first button (2-Player) works first click almost every time
- The second button (1-Player) works less like maybe every 10 clicks
- The third button (Scores) is even harder to get to work than the others
This doesn't make sense to me as all buttons use the same function:
def button(msg,x,y,h):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(screen, RED, (x,y, BUTTON_WIDTH, h))
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects(msg, smallText, WHITE)
textRect.center = ((x+(BUTTON_WIDTH/2)),(y+(h/2)))
screen.blit(textSurf, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if x+BUTTON_WIDTH > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, BRIGHT_RED, (x,y, BUTTON_WIDTH, h))
screen.blit(textSurf, textRect)
if click[0] == 1:
return True
def intro_screen():
intro = True
while intro:
screen.fill(GREEN)
if button("2-Player",245,145,BUTTON_HEIGHT):
multiplayer_loop()
if button("1-Player",245,270,BUTTON_HEIGHT):
login_screen(True)
if button("Scores",245,395,40):
login_screen(False)
screen.blit(TITLE, (120, 5))
pygame.display.update()
pygame.init()
intro_screen()
Any help would be greatly appreciated, thanks.