0

I'm doing a project that using Python and Pygame. I want to ask some question about the key in Pygame.

Thank you Shubhitgarg. I use his/her suggestions(use pygame_textinput), it succeed. Now I want to change the text input position with pressed an enter, but it can't detect the enter. How can I fix it? My real code:

# installing package
import pygame
from pygame import *
import pygame_textinput
pygame.init()

# colour settings
red = (255, 0, 0)
green = (0, 255, 0)
grass_green = (112, 173, 71)
blue = (0, 0, 255)
yellow = (255, 255, 0)
white = (255, 255, 255)
black = (0, 0, 0)

# screen settings
window = pygame.display.set_mode((680, 600))
window.fill(grass_green)
pygame.display.flip()

# font settings
default_font = pygame.font.get_default_font()
big_font = pygame.font.Font(default_font, 96)
font_a = pygame.font.Font(default_font, 50)
font_b = pygame.font.Font(default_font, 30)
font_c = pygame.font.Font(default_font, 18)

# text input settings
textinput = pygame_textinput.TextInput("freeansbold.ttf", 96, True, black, white, 400, 35)

# timer
start = pygame.time.get_ticks()

# text
please_guess_a_number = font_a.render("Please guess a number. ", 1, white)
text_range = font_c.render("from                                                        to", 1, white)
wrong_bigger = font_b.render("Sorry, You haven’t guess it rightly. Please try again.(The answer must be bigger.)", 1, white)
wrong_smaller = font_b.render("Sorry, You haven’t guess it rightly. Please try again.(The answer must be smaller.)", 1, white)
correct = font_b.render("Congratulations! You guess it correctly!!", 1, white)

# game
ask_you_to_set_range_1 = True
ask_you_to_set_range_2 = False
while True:
    if ask_you_to_set_range_1:
        window.blit(please_guess_a_number, (60, 80))
        pygame.draw.rect(window, yellow, [60, 200, 230, 300])
        pygame.draw.rect(window, yellow, [390, 200, 230, 300])
        window.blit(text_range, (10, 550))
        pygame.display.flip()
        while True:
            events = pygame.event.get()
            textinput.update(events)
            window.blit(textinput.get_surface(), (110, 300))
            pygame.display.flip()
    if ask_you_to_set_range_2:
        while True:
            events = pygame.event.get()
            textinput.update(events)
            window.blit(textinput.get_surface(), (440, 300))
            pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if ask_you_to_set_range_1 and event.key == pygame.K_KP_ENTER:
                ask_you_to_set_range_1 = False
                ask_you_to_set_range_2 = True
                if textinput.update(event):
                    num1 = textinput.get_text()
                    text1 = big_font.render(num1, 1, black)
                    window.blit(text1, (110, 300))

Can anyone teach me to solve it?

3
  • 1
    Actually, this answer fits better. Using input (or raw_input) isn't a nice solution, because the user has to switch to the console window to enter something. Commented Feb 21, 2018 at 14:45
  • BTW, don't just repost the same questions again. You could either try to reopen the previous question or delete it before you post it again, and in both cases describe more precisely what you want to achieve, what problems you have and how it differs from the duplicate question. Commented Feb 21, 2018 at 15:07
  • @skrx Oh, sorry. Thank you for your suggestions and instructions! I have already delete the post that duplicate Commented Feb 21, 2018 at 15:13

1 Answer 1

0

You see this https://github.com/ShubhitGarg/pygame-text-input . You can import this and use the text_input class and blit it on the same or next page Use this

textinput = pygame_textinput.TextInput()

while True:
    screen.fill((225, 225, 225))

    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            exit()

    # Feed it with events every frame
    textinput.update(events)
    # Blit its surface onto the screen
    screen.blit(textinput.get_surface(), (10, 10))

    pygame.display.update()
Sign up to request clarification or add additional context in comments.

15 Comments

Sorry, I download it, but I can't import it!!
where have you saved it?, You can also copy the class and past it in your code, if you want to
Do you mean that I need to save it in my project file(I'm a Pycharm user. In C:// there have a file to put the project in it)?
You copy paste the code or you can import it by saving it in /.../python /lib
and yes create a folder in python/lib , name it pygame_textinput, copy the pygame_textinput.py file from github into it and create an empty python file named __init__.py in same folder. It will act as module
|