Python Forum
Problems with loading buttons (pygame)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with loading buttons (pygame)
#1
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()
Reply
#2
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
Recommended Tutorials:
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame installation problems Gheryk 5 8,497 Nov-29-2023, 08:49 PM
Last Post: E_Mohamed
  [PyGame] pygame-manu : using controller buttons to move around menu mlw19mlw91 2 1,543 Mar-12-2023, 01:55 PM
Last Post: deanhystad
  [PyGame] Problems with jump code in pygame Joningstone 4 5,271 Aug-23-2021, 08:23 PM
Last Post: deanhystad
  Pygame - Images As Buttons vman44 3 12,817 Mar-20-2020, 08:13 PM
Last Post: vman44
  [PyGame] pygame image loading error BlueClaw 6 6,324 Dec-10-2019, 08:50 PM
Last Post: BlueClaw
  [Pygame] Problems with my textbox SheeppOSU 1 3,037 May-27-2019, 12:03 AM
Last Post: metulburr
  [pygame] Inventory problems. Weapons equipped to wrong slot SheeppOSU 6 3,942 May-07-2019, 02:46 AM
Last Post: SheeppOSU
  [pygame] Equiping inventory slots with invisible buttons SheeppOSU 6 4,660 Apr-26-2019, 08:45 PM
Last Post: SheeppOSU
  drawing, moving, and collision problems (pygame) SheeppOSU 26 14,435 Apr-22-2019, 03:09 AM
Last Post: SheeppOSU
  Buttons in PyGame mzmingle 4 12,953 Oct-09-2018, 05:19 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020