2016-04-25 8 views
-1

pygameにプロジェクトを書きましたが、今はログイン部分を書いています。 私はspesific場所で画面に1つの文字を印刷する関数を書いたが、それは動作しません、なぜ私は知らない。私はpygameで画面に書き込む機能を持っていますが、動作しません。

ここで私のコード:print_char_by_place(char、row、col)という関数は、コードの最後に使用します。

import pygame 
import os 




def if_button_pressed(left_col, right_col, high_row, low_row, mouse): 
    if mouse[0]>=left_col and mouse[0]<=right_col and mouse[1]>=high_row and   mouse[1]<=low_row: 
     return True 
    return False 

# get char that pressed in the keyboard and print it by place 
def print_char_by_place(char, row, col): 
    font = pygame.font.SysFont("monospace", 15) 
    label = font.render(char, 1, black) 
    screen.blit(label, (row, col)) 



pygame.init() 
screen = pygame.display.set_mode((700, 700)) 
done = True 
login = True 
user_name_p = False 
password_p = False 
username = "" 
password = "" 
row_u = 343 
col_u = 260 
while done: 
    img = pygame.image.load("LOGIN.png") 
    screen.blit(img,(0,0)) 
    for event in pygame.event.get(): 
     if event.type == pygame.MOUSEBUTTONUP: 
      mouse = pygame.mouse.get_pos() 
      if if_button_pressed(255,418,331,355,mouse):  # if the  username pressed 
       username_p = True 
       password_p = False 

      if if_button_pressed(255,418,382,403,mouse):  # if the password pressed 
       username_p = False 
       password_p = True 

      else: 
       user_name_p = False 
       password_p = False 

     if event.type == pygame.KEYDOWN: 
      if user_name_p: 
       char = str(event.key) 
       username+=char 
       print_char_by_place(char,row_u,col_u) 
       col_u+=2 



     if event.type == pygame.QUIT: 
      done = False 
    pygame.display.update() 

答えて

1

ユーザーが入力した各文字は、1つのフレームにのみレンダリングされるという問題があります。次のフレームが回転すると、背景イメージがその上に描画されます(最初に気付くことさえあった場合)。

テキストを表示したままにするには、各フレームを引き続き保持する必要があります。おそらく、username変数を拡張するロジックを持っているキー押下検出コードでは、これをしたくないかもしれません。 usernameを画面に表示する他のコードを追加する必要があります(キーが押されたときのフレームだけでなく)。

0
name="" 
if event.key>=97 and event.key<=122 and len(name)< 22 or event.key>=48 and event.key<=57 and len(name)< 22: 
    name += str(pygame.key.name(event.key)) 

このコードを使用すると、各キーを押して文字列を追加できます。この文字列をイメージに変換し、各ループを画面に出力することができます。あなたが最後の文字を消去する必要がある場合名前はあなたが真か偽かどうかを確認するためにthaの実際のusenameまたは何でこの文字列を比較することができます完了したら、あなたは単に

if event.key == K_BACKSPACE: 
    name = name[:-1] 

ください。

関連する問題