I am a student programmer stuck on a problem that I could really use some advice on. Basically, I tried making one big text box and that didn't work as pygame.font.render() doesn't allow multiple lines so I sperated it into multiple smaller boxes (12) and currently each button is tied to a text box. I have code for identifying the next empty box but I am stuck on actually populating it. Variables used have the following purposes: Item1 = Defines the text within the text box Empty_row = defines the next available empty text box I know its a lot so any advice is really appreciated. Also I know there are better modules to use but my assessment needed pygame
This is code for one box from start to finish and bellow will be the whole application if needed
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
global Item2
global Item1_rect
global Item2_rect
global click
global sc1_num
global sc1_price
global sc2_num
global 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()
That is basically the whole code just for 2 buttons. I fixed the issue of not staying on for long but the underlying question still stands. How do I get the text to populate the first text box rather than the one it is assigned to?
# window ----------------------------------------
"""
This file is the executable file which calls all other files including the
full application.
"""
#The following imports are modules which have prewritten
#functions which I am able to use. They came predownloaded
#with python as well as downloading some from pygame.org
import pygame, sys
from collections import Counter as count
from datetime import datetime
import time
import string
#import read
mainClock = pygame.time.Clock()
#setting window
from pygame.locals import *
#The following are Constants which are defined here for easier maintanence
click = bool
pygame.init()
pygame.display.set_caption('LeVOGUE')
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1200, 800), 0, 32)
windowSurface = pygame.display.set_mode((1200, 650), pygame.DOUBLEBUF)
run = 2
font = pygame.font.SysFont(None, 35)
font2 = pygame.font.SysFont(None, 70)
font3 = pygame.font.SysFont(None, 25)
base_font = pygame.font.Font(None, 50)
base_font2 = pygame.font.Font(None, 160)
Item1 = ''
Item2 = ''
Item3 = ''
Item4 = ''
Item5 = ''
Item6 = ''
Item7 = ''
Item8 = ''
Item9 = ''
Item10 = ''
Item11 = ''
Item12 = ''
Item1_rect = pygame.Rect(780, 33, 400, 547)
Item2_rect = pygame.Rect(780, 58, 400, 547)
Item3_rect = pygame.Rect(780, 83, 400, 547)
Item4_rect = pygame.Rect(780, 108, 400, 547)
Item5_rect = pygame.Rect(780, 133, 400, 547)
Item6_rect = pygame.Rect(780, 158, 400, 547)
Item7_rect = pygame.Rect(780, 183, 400, 547)
Item8_rect = pygame.Rect(780, 208, 400, 547)
Item9_rect = pygame.Rect(780, 233, 400, 547)
Item10_rect = pygame.Rect(780, 258, 400, 547)
Item11_rect = pygame.Rect(780, 283, 400, 547)
Item12_rect = pygame.Rect(780, 308, 400, 547)
row = 1
Colour = pygame.Color(0,0,0)
found =False
#key_text_num = 2
s = " "
fh =open("user_info.txt", "r")
keys = pygame.key.get_pressed()
click = pygame.MOUSEBUTTONDOWN
Volume = True
active = False
#tick_active = False
#This is responsible fo importing the mixer module
from pygame import mixer
#Load music file
mixer.music.load("MainMenuMusic.mp3")
#play music file
mixer.music.play()
sc1_num = 0
sc1_price = 0
sc2_num = 0
sc2_price = 0
sc3_num = 0
sc3_price = 0
sc4_num = 0
sc4_price = 0
sc5_num = 0
sc5_price = 0
sc6_num = 0
sc6_price = 0
sc7_num = 0
sc7_price = 0
sc8_num = 0
sc8_price = 0
sc9_num = 0
sc9_price = 0
sc10_num = 0
sc10_price = 0
sc11_num = 0
sc11_price = 0
sc12_num = 0
sc12_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
global Item2
global Item3
global Item4
global Item5
global Item6
global Item7
global Item8
global Item9
global Item10
global Item11
global Item12
global Item1_rect
global Item2_rect
global Item3_rect
global Item4_rect
global Item5_rect
global Item6_rect
global Item7_rect
global Item8_rect
global Item9_rect
global Item10_rect
global Item11_rect
global Item12_rect
global click
global active
global bags
global current_time
global found
global count
global s
global fh
global i_a
global key_text_num
global sc1_num
global sc1_price
global sc2_num
global sc2_price
global sc3_num
global sc3_price
global sc4_num
global sc4_price
global sc5_num
global sc5_price
global sc6_num
global sc6_price
global sc7_num
global sc7_price
global sc8_num
global sc8_price
global sc9_num
global sc9_price
global sc10_num
global sc10_price
global sc11_num
global sc11_price
global sc12_num
global sc12_price
global row
#This checkbox is imported from a module found within the LeVOGUE folder
#The checkbox file is imported above and called with the function checkbox
while True:
#Defining Quit
for event in pygame.event.get():
if event.type == QUIT:
print("Quit")
pygame.quit()
sys.exit()
#~~~~~~~~~~~~~~~~~~~~~~~~End Code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#if key escape key is pressed, Quit
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
#if click, click is true
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
click = True
if click:
print(pygame.mouse.get_pos())
#Background colour
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))
text3_surface = font3.render(Item3, True, (0,0,0))
screen.blit(text3_surface, (Item3_rect.x+20 , Item3_rect.y +100))
text4_surface = font3.render(Item4, True, (0,0,0))
screen.blit(text4_surface, (Item4_rect.x+20 , Item4_rect.y +100))
text5_surface = font3.render(Item5, True, (0,0,0))
screen.blit(text5_surface, (Item5_rect.x+20 , Item5_rect.y +100))
text6_surface = font3.render(Item6, True, (0,0,0))
screen.blit(text6_surface, (Item6_rect.x+20 , Item6_rect.y +100))
text7_surface = font3.render(Item7, True, (0,0,0))
screen.blit(text7_surface, (Item7_rect.x+20 , Item7_rect.y +100))
text8_surface = font3.render(Item8, True, (0,0,0))
screen.blit(text8_surface, (Item8_rect.x+20 , Item8_rect.y +100))
text9_surface = font3.render(Item9, True, (0,0,0))
screen.blit(text9_surface, (Item9_rect.x+20 , Item9_rect.y +100))
text10_surface = font3.render(Item10, True, (0,0,0))
screen.blit(text10_surface, (Item10_rect.x+20 , Item10_rect.y +100))
text11_surface = font3.render(Item11, True, (0,0,0))
screen.blit(text11_surface, (Item11_rect.x+20 , Item11_rect.y +100))
text12_surface = font3.render(Item12, True, (0,0,0))
screen.blit(text12_surface, (Item12_rect.x+20 , Item12_rect.y +100))
#mouse position
mx, my = pygame.mouse.get_pos()
#This section of code was applied from a tutorial which I followed
#It was used to allow me to create rectangular buttons although it was
#heavily modified to add functionality to different buttons
#~~~~~~~~~~~~~~Start code applied from tutorial ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#have each text box be represented in an array and check the array for the next empty bucket
b_w = 180
b_h = 70
b2_x = 274
b2_y = 562
b3_x = 55
b3_y = 200
b4_x = 275
b4_y = 200
b5_x = 490
b5_y = 200
b6_x = 55
b6_y = 290
b7_x = 275
b7_y = 290
b8_x = 490
b8_y = 290
b9_x = 55
b9_y = 380
b10_x = 275
b10_y = 380
b11_x = 490
b11_y = 380
b12_x = 55
b12_y = 470
b13_x = 275
b13_y = 470
b14_x = 490
b14_y = 470
button_2 = pygame.Rect(b2_x, b2_y, b2_w, b2_h)
button_3 = pygame.Rect( b3_x, b3_y ,b_w, b_h)
button_4 = pygame.Rect( b4_x, b4_y ,b_w, b_h)
button_5 = pygame.Rect( b5_x, b5_y ,b_w, b_h)
button_6 = pygame.Rect( b6_x, b6_y ,b_w, b_h)
button_7 = pygame.Rect( b7_x, b7_y ,b_w, b_h)
button_8 = pygame.Rect( b8_x, b8_y ,b_w, b_h)
button_9 = pygame.Rect( b9_x, b9_y ,b_w, b_h)
button_10 = pygame.Rect( b10_x, b10_y ,b_w, b_h)
button_11 = pygame.Rect( b11_x, b11_y ,b_w, b_h)
button_12 = pygame.Rect( b12_x, b12_y ,b_w, b_h)
button_13 = pygame.Rect( b13_x, b13_y ,b_w, b_h)
button_14 = pygame.Rect( b14_x, b14_y ,b_w, b_h)
#if Item1 == '':
# empty1 = true
# Item2 = ''
# Item3 = ''
# Item4 = ''
# Item5 = ''
# Item6 = ''
# Item7 = ''
# Item8 = ''
# Item9 = ''
# Item10 = ''
# Item11 = ''
# Item12 = ''
found = False
""""for x in range (1,12):
if found == False:
varName = "Item"+str(x)
v = globals()[varName]
print(v)
if v == '':
found = True
Empty_row = v
print("called")
print(Empty_row)
else:
x=x+1
#print(f"{varName} is empty")"""
if Item1 == '':
Empty_row = 1
elif Item2 == '':
Empty_row = 2
elif Item3 == '':
Empty_row = 3
elif Item4 == '':
Empty_row = 4
elif Item5 == '':
Empty_row = 5
elif Item6 == '':
Empty_row = 6
elif Item7 == '':
Empty_row = 7
elif Item8 == '':
Empty_row = 8
elif Item9 == '':
Empty_row = 9
elif Item10 == '':
Empty_row = 10
elif Item11 == '':
Empty_row = 11
elif Item12 == '':
Empty_row = 12
print(Empty_row)
#What happens if the mouse click is over buttons listed above
if button_2.collidepoint(mx, my):
if click:
print("button2")
import CheckoutMenu
if button_3.collidepoint(mx, my):
if click:
# if (f"Scrunchie1 {sc1_num} ${sc1_price}") in Item1:
# Item1 = Item1[:-50]
#Try an array of strings
#when added/modified update array and reprint everything
#if that doesnt work have everything on the screen and have 0 qty and delete everything
#and reprint with new variables to the screen
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")
if button_5.collidepoint(mx, my):
if click:
if (f"Scrunchie3 {sc3_num} ${sc3_price}") in Item3:
Item3 = Item3[:-50]
sc3_num = sc3_num+1
sc3_price = sc3_price+15
Item3 = Item3 + (f"Scrunchie3 {sc3_num} ${sc3_price}")
print(" Button5")
if button_6.collidepoint(mx, my):
if click:
if (f"Scrunchie4 {sc4_num} ${sc4_price}") in Item4:
Item4 = Item4[:-100]
sc4_num = sc4_num+1
sc4_price = sc4_price+15
Item4 = Item4 + (f"Scrunchie4 {sc4_num} ${sc4_price}")
print(" Button6")
if button_7.collidepoint(mx, my):
if click:
if (f"Scrunchie5 {sc5_num} ${sc5_price}") in Item5:
Item5 = Item4[:-100]
sc5_num = sc5_num+1
sc5_price = sc5_price+15
Item5 = Item5 + (f"Scrunchie5 {sc5_num} ${sc5_price}")
print(" Button7")
if button_8.collidepoint(mx, my):
if click:
if (f"Shirt6 {sc6_num} ${sc6_price}") in Item6:
Item6 = Item6[:-100]
sc6_num = sc6_num+1
sc6_price = sc6_price+15
Item6 = Item6 + (f"Shirt6 {sc6_num} ${sc6_price}")
print(" Button8")
if button_9.collidepoint(mx, my):
if click:
if (f"Scrunchie7 {sc7_num} ${sc7_price}") in Item7:
Item7 = Item7[:-100]
sc7_num = sc7_num+1
sc7_price = sc7_price+15
Item7 = Item7 + (f"Scrunchie7 {sc7_num} ${sc7_price}")
print(" Button9")
if button_10.collidepoint(mx, my):
if click:
if (f"Scrunchie8 {sc8_num} ${sc8_price}") in Item8:
Item8 = Item8[:-100]
sc8_num = sc8_num+1
sc8_price = sc8_price+15
Item8 = Item8 + (f"Scrunchie8 {sc8_num} ${sc8_price}")
print(" Button10")
if button_11.collidepoint(mx, my):
if click:
if (f"Scrunchie9 {sc9_num} ${sc9_price}") in Item9:
Item9 = Item9[:-100]
sc9_num = sc9_num+1
sc9_price = sc9_price+15
Item9 = Item9 + (f"Scrunchie9 {sc9_num} ${sc9_price}")
print(" Button11")
if button_12.collidepoint(mx, my):
if click:
if (f"Scrunchie10 {sc10_num} ${sc10_price}") in Item10:
Item10 = Item10[:-100]
sc10_num = sc10_num+1
sc10_price = sc10_price+15
Item10 = Item10 + (f"Scrunchie10 {sc10_num} ${sc10_price}")
print(" Button12")
if button_13.collidepoint(mx, my):
if click:
if (f"Scrunchie11 {sc11_num} ${sc11_price}") in Item11:
Item11 = Item11[:-100]
sc11_num = sc11_num+1
sc11_price = sc11_price+15
Item11 = Item11 + (f"Scrunchie11 {sc11_num} ${sc11_price}")
print(" Button13")
if button_14.collidepoint(mx, my):
if click:
if (f"Scrunchie12 {sc12_num} ${sc12_price}") in Item12:
Item12 = Item12[:-100]
sc12_num = sc12_num+1
sc12_price = sc12_price+15
Item12 = Item12 + (f"Scrunchie12 {sc12_num} ${sc12_price}")
print(" Button14")
Total_Items = sc1_price + sc2_price + sc3_price + sc4_price + sc5_price + sc6_price + sc7_price + sc8_price + sc9_price + sc10_price + sc11_price + sc12_price
Total_Qty = sc1_num + sc2_num + sc3_num + sc4_num + sc5_num + sc6_num + sc7_num + sc8_num + sc9_num + sc10_num + sc11_num + sc12_num
text_surface_Cart = font.render("Cart", True, (0,0,0))
text_surface_Item = font.render( "Item Qty Price", True, (0,0,0))
screen.blit(text_surface_Cart, (790,50))
screen.blit(text_surface_Item, (790, 100))
#for Button 2
text_surface_Enter = font2.render("Enter", True, (0,0,0))
screen.blit(text_surface_Enter, pygame.Rect((280, 565), (120, 70)))
text_surface_Items = font.render(f"${Total_Items}", True, (0,0,0))
screen.blit(text_surface_Items, pygame.Rect((1110, 530), (120, 70)))
text_surface_Enter = font.render(f"{Total_Qty}", True, (0,0,0))
screen.blit(text_surface_Enter, pygame.Rect((1022, 530), (120, 70)))
#Cart
pygame.draw.rect(screen,Colour,pygame.Rect(780,30,400,550),2)
#button 2; Enter
pygame.draw.rect(screen, Colour , button_2, 2)
#Draw boxes for the number buttons
pygame.draw.rect(screen, Colour, button_3, 2)
pygame.draw.rect(screen, Colour, button_4, 2)
pygame.draw.rect(screen, Colour, button_5, 2)
pygame.draw.rect(screen, Colour, button_6, 2)
pygame.draw.rect(screen, Colour, button_7, 2)
pygame.draw.rect(screen, Colour, button_8, 2)
pygame.draw.rect(screen, Colour, button_9, 2)
pygame.draw.rect(screen, Colour, button_10, 2)
pygame.draw.rect(screen, Colour, button_11, 2)
pygame.draw.rect(screen, Colour, button_12, 2)
pygame.draw.rect(screen, Colour, button_13, 2)
pygame.draw.rect(screen, Colour, button_14, 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))
screen.blit((font3.render("Scrunchie3", True, (0,0,0))),(b5_x+20, b5_y+15))
screen.blit((font3.render("Scrunchie4", True, (0,0,0))),(b6_x+20, b6_y+15))
screen.blit((font3.render("Scrunchie5", True, (0,0,0))),(b7_x+20, b7_y+15))
screen.blit((font3.render("Shirt6", True, (0,0,0))),(b8_x+20, b8_y+15))
screen.blit((font3.render("Shirt7", True, (0,0,0))),(b9_x+20, b9_y+15))
screen.blit((font3.render("Shirt8", True, (0,0,0))),(b10_x+20, b10_y+15))
screen.blit((font3.render("Shirt9", True, (0,0,0))),(b11_x+20, b11_y+15))
screen.blit((font3.render("Pants10", True, (0,0,0))),(b12_x+20, b12_y+15))
screen.blit((font3.render("Pants11", True, (0,0,0))),(b13_x+20, b13_y+15))
screen.blit((font3.render("Pants11", True, (0,0,0))),(b14_x+20, b14_y+15))
#~~~~~~~~~~~~~~~~~~ End Code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ButtonBolt = pygame.image.load("LionsHEAD.png")
ButtonBolt = pygame.transform.scale(ButtonBolt, (120, 120))
screen.blit(ButtonBolt, (20,15))
text_surface_LeVOGUE = font2.render("LeVOGUE", True, (0,0,0))
screen.blit(text_surface_LeVOGUE, (140,55))
Key_Rect = pygame.Rect(260, 125, 230, 40)
pygame.draw.rect(screen, (0,0,0) , Key_Rect, 2)
Left_select_Rect = pygame.Rect(208, 125, 40, 40)
pygame.draw.rect(screen, (0,0,0), Left_select_Rect , 2)
text_surface_Left_select = font2.render("<", True, (0,0,0))
screen.blit(text_surface_Left_select, (215,118))
Right_select_Rect = pygame.Rect(502, 125, 40, 40)
pygame.draw.rect(screen, (0,0,0), Right_select_Rect , 2)
text_surface_Left_select = font2.render(">", True, (0,0,0))
screen.blit(text_surface_Left_select, (510,118))
#CART
pygame.draw.rect(screen,Colour,pygame.Rect(780,30,400,550),2)
click = False
#update screen
pygame.display.update()
pygame.display.flip()
mainClock.tick(60)
#The following code was derived from a tutorial which allows the application
#to quit without causing any error codes.
#~~~~~~~~~~~~Start code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def Quit():
running = True
while running:
pygame.quit()
quit()
#~~~~~~~~~~~ End Code applied from tutorial~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#run the main menu
Main_sales()
pygame.quit()
sys.exit()