Skip to content Skip to sidebar Skip to footer

How To Allow The User To Type Only Under Certain Conditions In Pygame?

When you click the textbox I want the user to be able to type letters by setting self.active to True. When you click off the text box I want the player to lose that ability by sett

Solution 1:

Create the instance of Rectangle before the application loop, but you have to pass the list of events to the method naming and to handle all the relevant events in the method. Add a draw method to the class and draw the object in every frame:

classRectangle:
    # [...]defnaming(self, events):
        # [...]for event in evetnts:
            if e.type == KEYDOWN:
                # [...]if e.type == # [...]defdraw(self, screen):
        # [...]
rect_1 = Rectangle(200, 200)

run = Truewhile run :
    
    events = event.get()
    for e in events:
        if e.type == QUIT:
            run = False
    
    rect_1.naming(events)

    # [...]

    rect_1.draw(screen)      

Complete example:

import pygame
from pygame import *

init()
screen = display.set_mode((800, 600))

name_font = font.Font(None, 32)
name_text = ''classRectangle:

    def__init__(self, x, y):
        self.active = False
        self.x = x
        self.y = y
        self.text_surface = name_font.render(name_text, True, (255, 255, 255))
        self.rect_width = max(140, 10 + self.text_surface.get_width())
        self.input_rect = Rect(x, y, self.rect_width, 32)
        draw.rect(screen, (0, 0, 0), self.input_rect, 0)
        draw.rect(screen, (255, 255, 255), self.input_rect, 2)
        self.input_rect.w = self.text_surface.get_width() + 10
        screen.blit(self.text_surface, (self.input_rect.x + 5, self.input_rect.y + 5))

    defnaming(self, events):
        global rect_1
        global name_text

        for e in events:
            if e.type == MOUSEBUTTONDOWN:
                mx, my = e.pos
                if self.input_rect.x + 150 >= mx >= self.input_rect.x - 10:
                    if self.input_rect.y + 40 >= my >= self.input_rect.y - 10:
                        self.active = Trueifnot self.input_rect.x + 150 >= mx >= self.input_rect.x - 10:
                    ifnot self.input_rect.y + 40 >= my >= self.input_rect.y - 10:
                        self.active = Falseif e.type == KEYDOWN:
                if self.active:
                    if e.key == K_BACKSPACE:
                        name_text = name_text[:-1]
                    else:
                        name_text += e.unicode
                    self.text_surface = name_font.render(name_text, True, (255, 255, 255))
    
    defdraw(self, screen):
        draw.rect(screen, (0, 0, 0), self.input_rect, 0)
        draw.rect(screen, (255, 255, 255), self.input_rect, 2)
        self.input_rect.w = self.text_surface.get_width() + 10
        screen.blit(self.text_surface, (self.input_rect.x + 5, self.input_rect.y + 5))


rect_1 = Rectangle(200, 200)

run = Truewhile run :
    
    events = event.get()
    for e in events:
        if e.type == QUIT:
            run = False
    
    rect_1.naming(events)

    screen.fill(0)
    rect_1.draw(screen)  
    display.update()
    time.delay(1)  

Solution 2:

It looks to me like you need to take line 47 "rect_1 = Rectangle(200, 200)" out of the while loop because you keep recreating the box and it instantiates a new box with an active value equal to false.

Solution 3:

You are getting the mouse position but not doing anything with it like comparing it to the location of the box. if you start with self.active = True you can type in the box regardless of where the cursor is.

Post a Comment for "How To Allow The User To Type Only Under Certain Conditions In Pygame?"