Bottom Page

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()
Quote
#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
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [pygame] Inventory problems. Weapons equipped to wrong slot SheeppOSU 6 281 May-07-2019, 02:46 AM
Last Post: SheeppOSU
  [pygame] Equiping inventory slots with invisible buttons SheeppOSU 6 157 Apr-26-2019, 08:45 PM
Last Post: SheeppOSU
  drawing, moving, and collision problems (pygame) SheeppOSU 26 852 Apr-22-2019, 03:09 AM
Last Post: SheeppOSU
  Help w/ variables and destroying buttons SheeppOSU 5 193 Feb-19-2019, 05:34 PM
Last Post: Larz60+
  pygame installation problems Gheryk 2 1,157 Dec-12-2018, 07:05 AM
Last Post: Aikanwha
  [PyGame] Create multiple buttons in a loop? mzmingle 2 450 Nov-15-2018, 04:42 PM
Last Post: Windspar
  Buttons in PyGame mzmingle 4 827 Oct-09-2018, 05:19 PM
Last Post: Mekire
  [PyGame] Problem importing pygame / installing pygame Klar 4 2,183 Dec-16-2017, 05:48 PM
Last Post: Klar
  Menus, buttons and other widgets for Pygame Olleus 4 4,102 Apr-17-2017, 11:08 PM
Last Post: metulburr
  [Pygame] Avoid SentDex pygame tutorials georgecoopers 1 1,622 Apr-09-2017, 12:40 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)