0

I'm trying to get the text boxes to populate in ascending order e.g. button 2 fills text box 1 if clicked first. Currently the bellow code has each button tied to a textbox but I am trying to fix this to be used on a larger scale app (with 12 buttons available).

this is for my major project in school so any help is appreciated (the full version runs and looks better)

This is the minimum code needed to run


import pygame, sys

click = bool
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1200, 800), 0, 32)

font3 = pygame.font.SysFont(None, 25)
Item1 = ''
Item2 = ''
Item1_rect = pygame.Rect(780, 33, 400, 547)
Item2_rect = pygame.Rect(780, 58, 400, 547)
Colour = pygame.Color(0,0,0)
found =False
keys = pygame.key.get_pressed()
click = pygame.MOUSEBUTTONDOWN
sc1_num = 0
sc1_price = 0
sc2_num = 0
sc2_price = 0



def Main_sales():
#this area of the code was reused from a previous file
#which is an example of the RAD approach.

#~~~~~~~~~~~~~~~~~Start Code applied from previous programs~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     
    
    global Item1, Item2, Item1_rect, Item2_rect, click, sc1_num, sc1_price, sc2_num, sc2_price 
   

    while True:           
        for event in pygame.event.get():

            if event.type == QUIT:
                print("Quit")
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                       click = True

        screen.fill((255, 255, 255))

        text1_surface = font3.render(Item1, True, (0,0,0))
        screen.blit(text1_surface, (Item1_rect.x+20 , Item1_rect.y +100))

        text2_surface = font3.render(Item2, True, (0,0,0))
        screen.blit(text2_surface, (Item2_rect.x+20 , Item2_rect.y +100))

        
        mx, my = pygame.mouse.get_pos()

       

        b_w = 180
        b_h = 70
        
        b3_x = 55
        b3_y = 200
        b4_x = 275
        b4_y = 200
        
        button_3 = pygame.Rect( b3_x, b3_y ,b_w, b_h)
        button_4 = pygame.Rect( b4_x, b4_y ,b_w, b_h)
        

        if Item1 == '':
            Empty_row = 1
        elif Item2 == '':
            Empty_row = 2
    
        print(Empty_row)
      
        
        if button_3.collidepoint(mx, my):
            if click:

                sc1_num = sc1_num+1
                sc1_price = sc1_price+15
                Item1 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
                print(" Button3")
               
        if button_4.collidepoint(mx, my):
            if click:
 
                sc2_num = sc2_num+1
                sc2_price = sc2_price+15
                Item2 = (f"Scrunchie2            {sc2_num}    ${sc2_price}")
                print(" Button4")
        
        pygame.draw.rect(screen, Colour, button_3, 2)
        pygame.draw.rect(screen, Colour, button_4, 2)
        
        screen.blit((font3.render("Scrunchie1", True, (0,0,0))),(b3_x+20, b3_y+15))
        screen.blit((font3.render("Scrunchie2", True, (0,0,0))),(b4_x+20, b4_y+15))
 
        click = False
        #update screen

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

def QUIT():
    running = True
    while running:
        pygame.quit()
        quit()
      
       

#run the main menu
Main_sales()
pygame.quit()


```
Rabbid76
  • 202,892
  • 27
  • 131
  • 174

2 Answers2

0

You would be better off making a list of textboxes and keeping track that way. Or even better would be a simple class for the textbox, and using a list with them that way. This works for now

if button_3.collidepoint(mx, my): if click:

            sc1_num = sc1_num+1
            sc1_price = sc1_price+15
            if current_textbox == 0:
                Item1 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
            else:
                Item2 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
                current_textbox = 1
                
            print(" Button3")
           
    if button_4.collidepoint(mx, my):
        if click:

            sc2_num = sc2_num+1
            sc2_price = sc2_price+15
            if current_textbox == 1:
                Item2 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
            else:
                Item1 = (f"Scrunchie1            {sc1_num}    ${sc1_price}")
                current_textbox = 1
            print(" Button4")
    

Also, its

pygame.QUIT not QUIT.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
marienbad
  • 1,461
  • 1
  • 9
  • 19
0

Create a list of Items and use a list to manage the buttons. Create the buttons and calculate the button positions in a loop. Do this before the application loop:

itmes = [Item1, Item2]

b_w = 180
b_h = 70
        
b_x = 55
b_y = 200

button_rects = []
button_surfs = []
for i in range(len(Items)):
    button_rects.append(pygame.Rect(b_x + i*220, b_y, b_w, b_h)) 
    button_surfs.append(font3.render(items[i], True, (0,0,0)))
    text1_surface = font3.render(Item1, True, (0,0,0))    

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

Draw the buttons in a loop and see if a button is clicked in a loop:

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

    screen.fill((255, 255, 255))

    for rect, text_surf in zip(button_rects, button_surfs):
        screen.blit(text_surf[i], text_surf.get_rect(center = rect.center))
    
    # [...]

    if click:
        for i, rect in enumerate(button_rects):
            if rect.collidepoint(mx, my):

                if i == 0:
                    # Item1 clicked
                    # [...]

                if i == 1:
                    # Item2 clicked
                    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174