1

this app runs and displays the words but no boxes around it as it should, it also types on all areas rather than the one that should be active. This app is going to be a survey to make an account which will be entered into a .txt file to be read by a function within the main menu for a POS machine as an assessment. I am extremely grateful for any help!

If someone could also explain debugging logic errors using pygame that would help for future issues as I am new to python and want to learn so that I can pass on help!

"""
This file is the executable file which calls all other files including the
full game as well as each individual level.
"""
import pygame, sys
mainClock = pygame.time.Clock()

#setting window
from pygame.locals import *

click = bool
pygame.init()
pygame.display.set_caption('Make an Account')
screen = pygame.display.set_mode((600,950), 0, 32)
windowSurface = pygame.display.set_mode((600, 800), pygame.DOUBLEBUF)
run=2
font = pygame.font.SysFont(None, 32)
user_text_name =''
user_text_ph_number =''
user_text_email_address =''
user_text_account_number =''

user_text_name_rect= pygame.Rect(150,200,300,35)
user_text_ph_number_rect= pygame.Rect(150,350,300,35)
user_text_email_address_rect= pygame.Rect(150,500,300,35)
user_text_account_number_rect= pygame.Rect(150,650,300,35)


color_name_active = pygame.Color('lightskyblue3')
color_name_passive = pygame.Color('gray15')
color_name = color_name_passive
Colour_name = pygame.Color(0,0,0)

color_ph_number_active = pygame.Color('lightskyblue3')
color_ph_number_passive = pygame.Color('gray15')
color_ph_number = color_ph_number_passive
Colour_ph_number = pygame.Color(0,0,0)

color_email_address_active = pygame.Color('lightskyblue3')
color_email_address_passive = pygame.Color('gray15')
color_email_address = color_email_address_passive
Colour_email_address = pygame.Color(0,0,0)

color_account_number_active = pygame.Color('lightskyblue3')
color_account_number_passive = pygame.Color('gray15')
color_account_number = color_account_number_passive
Colour_account_number = pygame.Color(0,0,0)

keys = pygame.key.get_pressed()
click = pygame.MOUSEBUTTONDOWN
user_text_name_active = False
user_text_ph_number_active = False
user_text_email_address_active = False
user_text_account_number_active = False



def main_menu():
    
    global click
    global color_name
    global color_ph_number
    global color_email_address
    global color_account_number
    global Colour_name
    global Colour_ph_number
    global Colour_email_address
    global Colour_account_number
    global user_text_name_rect
    global user_text_ph_number_rect
    global user_text_email_address_rect
    global user_text_account_number_rect
    global user_text_name
    global user_text_ph_number
    global user_text_email_address
    global user_text_account_number
    global user_text_name_active
    global user_text_ph_number_active
    global user_text_email_address_active
    global user_text_account_number_active
    
    
    while True:

        for event in pygame.event.get():
            if event.type == QUIT:
                print("Quit")
                pygame.quit()
                sys.exit()

                
            if event.type == pygame.MOUSEBUTTONDOWN:
                if user_text_name_rect.collidepoint(event.pos):
                    user_text_name_active = True
                    user_text_ph_number_active = False
                    user_text_email_address_active = False
                    user_text_account_number_active = False
                    
                else:
                    user_text_name_active = False
                    
            if event.type == pygame.MOUSEBUTTONDOWN:
                if user_text_ph_number_rect.collidepoint(event.pos):
                    user_text_name_active = False
                    user_text_ph_number_active = True
                    user_text_email_address_active = False
                    user_text_account_number_active = False
                else:
                    user_text_ph_number_active = False
                    
            if event.type == pygame.MOUSEBUTTONDOWN:
                if user_text_email_address_rect.collidepoint(event.pos):
                    user_text_name_active = False
                    user_text_ph_number_active = False
                    user_text_email_address_active = True
                    user_text_account_number_active = False
                else:
                    user_text_email_address_active = False
                    
            if event.type == pygame.MOUSEBUTTONDOWN:
                if user_text_account_number_rect.collidepoint(event.pos):
                    user_text_name_active = False
                    user_text_ph_number_active = False
                    user_text_email_address_active = False
                    user_text_account_number_active = True
                else:
                    user_text_account_number_active = False

            
                    
                    
            
            if event.type == pygame.KEYDOWN:
                if user_text_name_active == True:
                    print("USER_TEXT_NAME_ACTIVE")
                    #user_text_ph_number_active = False
                    #user_text_email_address_active = False
                    #user_text_account_number_active = False
                    if event.key == pygame.K_BACKSPACE:
                        user_text_name=user_text_name[:-1]
                    else:
                        if text_surface_name.get_width() <= 410:
                            user_text_name+= event.unicode

                            
                if event.type == pygame.KEYDOWN:
                    if user_text_ph_number_active == True:
                        print("USER_TEXT_ph_number_ACTIVE")
                        #user_text_name_active = False
                        #user_text_email_address_active = False
                        #user_text_account_number_active = False
                        if event.key == pygame.K_BACKSPACE:
                            user_text_ph_number=user_text_ph_number[:-1]
                    else:
                        if text_surface_ph_number.get_width() <= 410:
                            user_text_ph_number+= event.unicode
                            
                if event.type == pygame.KEYDOWN:
                    if user_text_email_address_active == True:
                        print("USER_TEXT_email_address_ACTIVE")
                        #user_text_ph_number_active = False
                        #user_text_name_active = False
                        #user_text_account_number_active = False
                        if event.key == pygame.K_BACKSPACE:
                            user_text_email_address=user_text_email_address[:-1]
                    else:
                        if text_surface_email_address.get_width() <= 410:
                            user_text_email_address+= event.unicode
                            
                if event.type == pygame.KEYDOWN:
                    if user_text_account_number_active == True:
                        print("USER_TEXT_Account_number_ACTIVE")
                        #user_text_ph_number_active = False
                        #user_text_email_address_active = False
                        #user_text_name_active = False
                        if event.key == pygame.K_BACKSPACE:
                            user_text_account_number=user_text_account_number[:-1]
                    else:
                        if text_surface_account_number.get_width() <= 410:
                            user_text_account_number+= event.unicode
                            
                
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    click = True

            if click:
                print(pygame.mouse.get_pos())
        
        #Background colour
        screen.fill((255, 255, 255))

        if user_text_name_active:
            color_name = color_name_active
        else:
            color_name = color_name_passive

        if user_text_ph_number_active:
            color_ph_number = color_ph_number_active
        else:
            color_ph_number = color_ph_number_passive

        if user_text_email_address_active:
            color_email_address = color_email_address_active
        else:
            color_email_address = color_email_address_passive

        if user_text_account_number_active:
            color_account_number = color_account_number_active
        else:
            color_account_number = color_account_number_passive





        
        #pygame.draw.rect(screen,color,input_rect,2)
                
        if user_text_name=='':
            text_surface_empty_name = font.render("ENTER NAME:", True, (0,0,0))
            screen.blit(text_surface_empty_name, (150,200))

        if user_text_ph_number=='':
            text_surface_empty_ph_number = font.render("ENTER PHONE NUMBER:", True, (0,0,0))
            screen.blit(text_surface_empty_ph_number, (150,350))

        if user_text_email_address=='':
            text_surface_empty_email_address = font.render("ENTER EMAIL ADDRESS:", True, (0,0,0))
            screen.blit(text_surface_empty_email_address, (150,500))

        if user_text_account_number=='':
            text_surface_empty_account_number = font.render("MAKE AN ACCOUNT NUMBER:", True, (0,0,0))
            screen.blit(text_surface_empty_account_number, (150,650))
            
        
        text_surface_name = font.render(user_text_name, True, (0, 0, 0))
        screen.blit(text_surface_name, (user_text_name_rect.x +5, user_text_name_rect.y +5))

        text_surface_ph_number = font.render(user_text_ph_number, True, (0, 0, 0))
        screen.blit(text_surface_ph_number, (user_text_ph_number_rect.x +5, user_text_ph_number_rect.y +5))

        text_surface_email_address = font.render(user_text_email_address, True, (0, 0, 0))
        screen.blit(text_surface_email_address, (user_text_email_address_rect.x +5, user_text_email_address_rect.y +5))

        text_surface_account_number = font.render(user_text_account_number, True, (0, 0, 0))
        screen.blit(text_surface_account_number, (user_text_account_number_rect.x +5, user_text_account_number_rect.y +5))
    
        
        
        #mouse position
        mx, my = pygame.mouse.get_pos()


        #Location and size of button (not picture)
        button_1 = pygame.Rect(53, 500, 80, 80)
        
        
        #What happens if the mouse click is over buttons listed above
        
                
       
    
       
        
        pygame.draw.rect(screen, (255,255,255), button_1)
        

        #load picture
        ButtonBolt = pygame.image.load("LionsHEAD.png")
        #Size of picture
        ButtonBolt = pygame.transform.scale(ButtonBolt, (80, 80))
        #Background of button
        pygame.draw.rect(screen, (255, 255, 255), button_1)
        #location of picture
        screen.blit(ButtonBolt, (53, 53))

        

        text_surface_LeVOGUE = font.render("LeVOGUE", True, (0,0,0))
        screen.blit(text_surface_LeVOGUE, (100,53))
        


        


        
        click = False

        pygame.display.update()
        mainClock.tick(60)
        



#credits menu
def Quit():
    running = True
    while running:
        pygame.quit()
        quit()

        pygame.display.update()
        mainClock.tick(60)





main_menu()

pygame.quit()
sys.exit()

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

If you want to draw text with a non-transparent background, you need to set the background parameter in the render method. For example:

enter image description here

text_surface_name = font.render(user_text_name, True, (0, 0, 0), (127, 127, 127))
screen.blit(text_surface_name, (user_text_name_rect.x +5, user_text_name_rect.y +5))

If you want to draw a box around the text you need to draw a rectangle with pygame.draw.rect(). Get the bounding rectangle from the text Surface with get_rect(). Use inflate() to grow the rectangle in size. For example:

text_surface_ph_number = font.render(user_text_ph_number, True, (0, 0, 0))
text_pos = (user_text_ph_number_rect.x +5, user_text_ph_number_rect.y +5)
box_rect = text_surface_ph_number.get_rect(topleft = text_pos).inflate(20, 20)
pygame.draw.rect(screen, (127, 127, 127), box_rect)
screen.blit(text_surface_ph_number, text_pos)

If you want to draw an outline around the text, you have to set the width parameter when you draw the rectangle with pygame.draw.rect(). For example:

text_surface_email_address = font.render(user_text_email_address, True, (0, 0, 0))
text_pos = (user_text_email_address_rect.x +5, user_text_email_address_rect.y +5)
box_rect = text_surface_email_address.get_rect(topleft = text_pos).inflate(20, 20)
pygame.draw.rect(screen, (0, 0, 0), box_rect, 3)
screen.blit(text_surface_email_address, text_pos)

See also Text and Font


The text input logic doesn't work, because there are some indentation issues. The else case belongs to if event.key == pygame.K_BACKSPACE: instead of if user_text_name_active == True::

def main_menu():
    # [...]

    while True:

        for event in pygame.event.get():
            # [...]

            if event.type == pygame.KEYDOWN:
                if user_text_name_active == True:
                    print("USER_TEXT_NAME_ACTIVE")
                    if event.key == pygame.K_BACKSPACE:
                        user_text_name=user_text_name[:-1]
                    else:
                        if text_surface_name.get_width() <= 410:
                            user_text_name+= event.unicode

                            
                if event.type == pygame.KEYDOWN:
                    if user_text_ph_number_active == True:
                        print("USER_TEXT_ph_number_ACTIVE")
                        if event.key == pygame.K_BACKSPACE:
                            user_text_ph_number=user_text_ph_number[:-1]
                        else:
                            if text_surface_ph_number.get_width() <= 410:
                                user_text_ph_number+= event.unicode
                            
                if event.type == pygame.KEYDOWN:
                    if user_text_email_address_active == True:
                        print("USER_TEXT_email_address_ACTIVE")
                        if event.key == pygame.K_BACKSPACE:
                            user_text_email_address=user_text_email_address[:-1]
                        else:
                            if text_surface_email_address.get_width() <= 410:
                                user_text_email_address+= event.unicode
                            
                if event.type == pygame.KEYDOWN:
                    if user_text_account_number_active == True:
                        print("USER_TEXT_Account_number_ACTIVE")
                        if event.key == pygame.K_BACKSPACE:
                            user_text_account_number=user_text_account_number[:-1]
                        else:
                            if text_surface_account_number.get_width() <= 410:
                                user_text_account_number+= event.unicode

Rabbid76
  • 202,892
  • 27
  • 131
  • 174