python - Blit user text input to screen -


i want blit text input user screen. each time user presses return, typed text should blitted screen. text input use [text_input module] (https://github.com/nearoo/pygame-text-input).

here code came far:

import pygame_textinput import pygame pygame.init()  # set parameters duration = 5.0 time = pygame.time.get_ticks()/1000  screen = pygame.display.set_mode((400, 400)) clock = pygame.time.clock()  yoffset = 5 # function positions user input rects on screen def renderinput(text, xoffset, yoffset):     font = pygame.font.sysfont("arial", 20)     rendertext = font.render(text, false, (0, 0, 0))     recttext = rendertext.get_rect()     recttext = recttext.move((0 + xoffset),  (screen.get_height()/2 + yoffset))     return  rendertext, recttext  # fills screen once @ beginning screen.fill((225, 225, 225))  while (pygame.time.get_ticks()/1000) < time + duration:     # creat new text input object on every trial     textinput = pygame_textinput.textinput()      while true:         # fills surface after each keypress         screen.fill((225, 225, 225))          # check events         events = pygame.event.get()         event in events:             if event.type == pygame.quit:                 exit()          # feed events every frame         # evaluates true once return pressed         if textinput.update(events):             userinput = textinput.get_text()             yoffset += 20             break          # blit surface onto screen         screen.blit(textinput.get_surface(), (10, 10))         # update screen         pygame.display.update()         clock.tick(30)      # blits user input screen each time "return" pressed     # first input text , rectangle of text     text, textrect = renderinput(userinput, 5, yoffset)     # blit screen     screen.blit(text, textrect)     pygame.display.update() 

my problem is, blitting works if not fill screen after each keypress within while-loop handles input. if that, text input, however, not cleared after each time user presses return.

so there way have both, redraw after each keypress , have text displayed below after each time return pressed user.

thanks.

if understand correctly, text in input field should cleared , should blit in main area of screen. i'd assign text user_input variable if user presses enter , create new pygame_textinput.textinput() instance clear input field.

i've tried simplify code, because 2 while loops bit confusing , i'm not sure purpose is. there should 1 while loop in game.

import pygame import pygame_textinput   pygame.init()  screen = pygame.display.set_mode((400, 400)) clock = pygame.time.clock() font = pygame.font.sysfont("arial", 20)  textinput = pygame_textinput.textinput() user_input = ''  done = false  while not done:     events = pygame.event.get()     event in events:         if event.type == pygame.quit:             done = true      if textinput.update(events):        user_input = textinput.get_text()        textinput = pygame_textinput.textinput()      # draw everything.     screen.fill((225, 225, 225))      screen.blit(textinput.get_surface(), (10, 10))      user_input_surface = font.render(user_input, true, (30, 80, 100))     screen.blit(user_input_surface, (10, 50))      pygame.display.update()     clock.tick(30)  pygame.quit() 

edit: in version append rendered text surfaces list , blit them offset.

import pygame import pygame_textinput   pygame.init()  screen = pygame.display.set_mode((400, 400)) clock = pygame.time.clock() font = pygame.font.sysfont("arial", 20)  textinput = pygame_textinput.textinput() user_inputs = []  done = false  while not done:     events = pygame.event.get()     event in events:         if event.type == pygame.quit:             done = true      if textinput.update(events):        user_inputs.append(            font.render(textinput.get_text(), true, (30, 80, 100)))        textinput = pygame_textinput.textinput()      screen.fill((225, 225, 225))      screen.blit(textinput.get_surface(), (10, 10))      y, text_surf in enumerate(user_inputs):         screen.blit(text_surf, (10, 50+30*y))      pygame.display.update()     clock.tick(30)  pygame.quit() 

edit2: table, can use modulo row offset , floor division column offset. problem example text surfaces can overlap if wide.

for n, text_surf in enumerate(user_inputs):     # 5 rows. offset = 30 pixels.     y_pos = 50 + (n%5) * 30     # after 5 rows add new column. offset = 100 pixels.     x_pos = 10 + n // 5 * 100     screen.blit(text_surf, (x_pos, y_pos)) 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -