Home > other >  How to implement two input box in pygame, without having to repeat the code
How to implement two input box in pygame, without having to repeat the code

Time:01-28


Def InputBox () :
The font=pygame. The font. The font (None, 32)

InputBox=pygame. The Rect (50, 50, 140, 32)
ColourInactive=pygame. Color (' lightskyblue3 ')
ColourActive=pygame. Color (' dodgerblue2 ')
See colour=colourInactive
Text='

Active=False
IsBlue=True

While True:
For the event in pg. Event. The get () :
If the event. The type==pg. QUIT:
Pg. Quit ()
Sys. The exit ()
If the event. The type==pygame. MOUSEBUTTONDOWN:
If inputBox. Collidepoint (event. Pos) :
Active=not active
The else:
Active=False
See colour=colourActive if active else colourInactive
If the event. The type==pygame. KEYDOWN:
If the active:
If the event. The key==pygame. K_RETURN:
Print (text)
Text='
Elif event. The key==pygame. K_BACKSPACE:
Text=text] [: - 1
The else:
Unicode
text +=the event.
Screen. The fill (screenGray)

TxtSurface=font. Render (text, True, see
Width=Max (200, txtSurface get_width () + 10)
InputBox. W=width
Screen. The blit (txtSurface, (inputBox. X + 5, inputBox. Y + 5))
Pygame. The draw. The rect (screen, see colour, inputBox, 2)

If isBlue:
Color=(0, 128, 255)
The else:
Color=(255, 100, 0)

Pg. Display. Flip ()
The clock. Tick (60)

InputBox ()

The above is a work function, make the screen has a text box, now, how do you create two text box in the same screen, rather than just copy and paste the same code twice?
My idea is, click the text box will perform events or activating the contents of the part, so you don't have to repeat everything, but I don't know how to implement it,
In this first thank






CodePudding user response:


Class, you can create a method in attracting it and handle events, then you can use many times
All the work sample
The import pygame

The class InputBox () :

Def __init__ (self, x, y) :

The self. The font=pygame. The font. The font (None, 32)

The self. The inputBox=pygame. The Rect (x, y, 140, 32)

Self. ColourInactive=pygame. Color (' lightskyblue3)
Self. ColourActive=pygame. Color (' dodgerblue2)
Self. See colour=self. ColourInactive

The self. The text='

The self. The active=False
Self. IsBlue=True

Def handle_event (self, event) :

If the event. The type==pygame. MOUSEBUTTONDOWN:
The self. The active=self. InputBox. Collidepoint (event. Pos)
Self. See colour=self. ColourActive if self. Active else self. ColourInactive
If the event. The type==pygame. KEYDOWN:
If the self. The active:
If the event. The key==pygame. K_RETURN:
Print (self. Text)
The self. The text='
Elif event. The key==pygame. K_BACKSPACE:
Self. Text=self. Text] [: - 1
The else:
The self. The text +=event. Unicode

Def the draw (self, screen) :
TxtSurface=self. The font. Render (self. Text, True, the self. See
Width=Max (200, txtSurface get_width () + 10)
The self. The inputBox. W=width
Screen. The blit (txtSurface, (self) inputBox) x + 5, the self. The inputBox, y + 5))
Pygame. The draw. The rect (screen, the self. See colour, self inputBox, 2)

If self. IsBlue:
The self color=(0, 128, 255)
The else:
The self color=(255, 100, 0)

# -- -- -- the main -

Def mainloop () :

# the create objects
Input1=InputBox (50, 50)
Input2=InputBox (450, 50)

Clock=pygame. Time. Clock ()

While True:

For the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
Pygame. Quit ()
Sys. The exit ()

# handle every event
Input1. Handle_event (event)
Input2. Handle_event (event)

Screen. The fill ((128128, 128))

# the draw it
Input1. The draw (screen)
Input2. The draw (screen)

Pygame. Display. Flip ()
The clock. Tick (60)


Pygame. The init ()
Screen=pygame. Display. Set_mode ((800600))
Mainloop ()


BTW: more investment, you can let them in the list (as in the GUI framework)
Def mainloop () :

Widgets=[
InputBox (50, 50),
InputBox (450, 50),
InputBox (50, 150),
InputBox (450, 150),
InputBox (50, 250),
InputBox (450, 250),
InputBox (50, 350),
InputBox (450, 350),
]

Clock=pygame. Time. Clock ()

While True:

For the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
Pygame. Quit ()
Sys. The exit ()

For the child widgets in:
Child. Handle_event (event)

Screen. The fill ((128128, 128))

For the child widgets in:
Child. The draw (screen)

Pygame. Display. Flip ()

The clock. Tick (60)

CodePudding user response:


You can create a class, and in need of different input box instantiated multiple times,
Is this:
# # pseudocode

The class InputBox (pygame. The Rect) :
Def __init__ (self, position=(50, 50, 140, 32),
The font=pygame. The font. The font (None, 32),
Color_inactive=pygame. Color (' lightskyblue3),
Color_active=pygame. Color (' dodgerblue2),
Text=",
Active=False)
Super () __init__ (position) # & lt; - this may need to be adjusted to pygame the Rect specs
The self. The font=the font
Self. Color_inactive=color_inactive
Self. Color_active=color_active
nullnullnullnullnullnullnullnullnullnull
  • Related