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?

  • 1
    Actually, [this answer](https://stackoverflow.com/a/48771100/6220679) 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. – skrx Feb 21 '18 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. – skrx Feb 21 '18 at 15:07
  • @skrx Oh, sorry. Thank you for your suggestions and instructions! I have already delete the post that duplicate –  Feb 21 '18 at 15:13

1 Answers1

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()
Shubhitgarg
  • 588
  • 6
  • 20
  • Sorry, I download it, but I can't import it!! –  Feb 21 '18 at 15:19
  • where have you saved it?, You can also copy the class and past it in your code, if you want to – Shubhitgarg Feb 21 '18 at 15:20
  • 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)? –  Feb 21 '18 at 15:21
  • You copy paste the code or you can import it by saving it in /.../python /lib – Shubhitgarg Feb 21 '18 at 15:23
  • 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 – Shubhitgarg Feb 21 '18 at 15:26
  • Thanks!! I can import it now! –  Feb 21 '18 at 15:36
  • great!!! Good luck! – Shubhitgarg Feb 21 '18 at 15:36
  • but it popped up the error: Traceback (most recent call last): File "C:/Users/user/PycharmProjects/What's_Your_Number/testing.py", line 5, in text_input = text_input.TextInput() AttributeError: module 'text_input' has no attribute 'TextInput' –  Feb 21 '18 at 15:53
  • double check the folder and files – Shubhitgarg Feb 21 '18 at 15:57
  • I have already put the file(unzip too) in C:\Users\user\PycharmProjects\What's_Your_Number\venv\Lib\site-packages –  Feb 21 '18 at 16:02
  • I found that the `TextInput` function is a `class` in the __init__.py. Is it because of this? –  Feb 21 '18 at 16:07
  • the `init` file should be empty and another file from github `pygame_textinput.py` and in lib not in site-packages – Shubhitgarg Feb 22 '18 at 03:54
  • Traceback (most recent call last): File "C:/Users/user/PycharmProjects/What's_Your_Number/testing.py", line 17, in pygame_textinput.update(events) AttributeError: module 'pygame_textinput' has no attribute 'update' –  Feb 22 '18 at 07:41
  • Thank you for your instructions, but it's not work! Did you misunderstand something about what I want to ask? I want to have a `raw_input`(just mean ask something and can type the answer on the console) but now I want it to display the question and can type the answer on the display! I want to know what will be display on the display. –  Feb 22 '18 at 07:46
  • Oh thanks!! I check for the [link](https://github.com/ShubhitGarg/pygame-text-input) and I found the answer!! But I don't know why it doesn't work in my real code. Please look at my new code at the top. –  Feb 22 '18 at 09:59