Python Forum

Full Version: Problems with loading buttons (pygame)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am making a game which is so far good, but after the player fills in all the info, it doesn't display the buttons. The shell is in typable mode (Can't type commands). I don't know how to fix it can someone please tell me what I'm doing wrong. Thx

The Button Func on line 58 and executed on lines 96 and 97 inside Game_Func
import pygame
import random
import time

pygame.init()
#GameData
signInList = {'Sheepposu' : 'rachl032078', 'gg' : 'rachl1979'}

#Variables

#Colors
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
darker_red = (200,0,0)
green = (0,255,0)
blue = (0,0,255)
CustomColor1 = (48,150,140)
CustomColor2 = (36,112.5,105)
Unique_Color = (190,140,210)
ColorList = [black, red, green, blue]
Confirm = True

#Game Config
display_width = 1000
display_height = 800
pd = pygame.display
gD = pygame.display.set_mode((display_width,display_height))
fps = 100
pd.set_caption('Game')
clock = pygame.time.Clock()
gD.fill(blue)
clock.tick(fps)

pygame.display.update()

#functions
def SignIn():
    global signInList
    username = input('(Type "new" for new player) Username: ')

    if username == 'new':
        newUsername = input('Type your Username: ')
        newPassword = input('Type your Password: ')
        signInList = {'Sheepposu' : 'rachl032078',
                      newUsername : newPassword}
        Game_Func(username)

    else:
        if username in signInList:
            password = input('Please type your password: ')
            if password == signInList[username]:
                Game_Func(username)
        else:
            print('Invalid Username')
            SignIn()

def Button(Butx, Buty, Butx2, Buty2, Butcolor, ShadowColor, text, textsize, textcolor, textFont, command=None, command2=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    smallText = pygame.font.SysFont(textFont, textsize)
    textSurf, textRect = text_objects(text, smallText)
    textRect.center = ((Butx + (Butx2/2)), Buty + (Buty2/2))
    pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2))
    gD.blit(textSurf, textRect)
    if Butx + Butx2 > mouse[0] > Butx and Buty + Buty2 > mouse[1] > Buty: 
        pygame.draw.rect(gD, ShadowColor, (Butx, Buty, Butx2, Buty2))
        gD.blit(textSurf, textRect)
        if click[0] == 1:
            if command != None:
                command()
            if command2 != None:
                command2()
    else:
        pygame.draw.rect(gD, Butcolor, (Butx, Buty, Butx2, Buty2))
        gD.blit(textSurf, textRect)
    pygame.display.update()

def message_display(text, size, centerX, centerY):
    font = pygame.font.SysFont('arial', size)
    textSurf, TextRect = text_objects(text, font)
    TextRect.center = ((centerX),(centerY))
    gD.blit(textSurf, TextRect)

    pygame.display.update()

def text_objects(text, font):
    textSurface = font.render(text, True, Unique_Color)
    return textSurface, textSurface.get_rect()

def Game_Func(username):
    global Confirm
    while Confirm:
        gD.fill(blue)
        message_display('You\'re %s, correct?' %username, round(display_width/17), round(display_width/2), display_height * .25)
        Button(round(display_width * .3), round(display_height * .7), round(display_width * .4), round(display_height * .2), CustomColor1, CustomColor2, 'Confirm!', round(display_width/20), blue, 'arial', Game_Func2)
        Button(round(display_width * .75), round(display_height * .8), round(display_width * .2), round(display_height * .1), red, darker_red, "No!", round(display_width/50), blue, 'arial', SignIn)
        
        clock.tick(15)

def Game_Func2():
    global Confirm
    Confirm = False
    End_Game()

def End_Game():
    print(signInList)

message_display('Please fill in info on the console', round(display_width/20), round(display_width/2), round(display_height/2))
SignIn()
i do see the buttons, but they are completely unresponsive and even not rendering every frame. You have a lot of poor programming practices in this one piece of code. If i fixed your code to work, i would completely have to rewrite it. Fixing it to just work now is setting you up for failure in t he future.

1)There should only ever be one line in your code of
pygame.display.update()
and it should reside in your main game loop. The fact that you have more than one is a sign that you are creating your structure incorrectly and you are making spaghetti code. As a result the code is not working correctly and is really hard to fix. The more that is added, the worse it gets.

2) There is a reason we use classes. Buttons are complex enough that it is much simpler to handle them in classes. As a bonus you wont have confusing 11 parameters to one function anymore.

3) You are creating and running the button in the middle of the main game loop. The buttons should be created beforehand, and only update and draw methods should be in the main game loop.

Here are some links that i would suggest to read thoroughly.
state machine
user interface
classes
Thx for the advice. I'm only 13, but I try to do the best I can. We have those 2-week long tests going on right now