1
我在pygame中製作遊戲,製作這款遊戲時我在屏幕上使用了很多文字。但是當我在代碼中達到這一點時,首先寫入的文本不會閃爍,但第二個文本會執行,我想要更改該文本。另外,當我持有一封密鑰時,信件一遍又一遍地被寫入,而我只希望它能寫出一封信。代碼:pygame閃爍文字
def game_loop():
while True:
clock = pygame.time.Clock()
def enter_name():
global kiriko
global pressed_key
global ikey
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_1 :
pressed_key = '1'
if event.key == pygame.K_2 :
pressed_key = '2'
if event.key == pygame.K_3 :
pressed_key = '3'
if event.key == pygame.K_4 :
pressed_key = '4'
if event.key == pygame.K_5 :
pressed_key = '5'
if event.key == pygame.K_6 :
pressed_key = '6'
if event.key == pygame.K_7 :
pressed_key = '7'
if event.key == pygame.K_8 :
pressed_key = '8'
if event.key == pygame.K_9 :
pressed_key = '9'
if event.key == pygame.K_0 :
pressed_key = '0'
if event.key == pygame.K_q :
pressed_key = 'q'
if event.key == pygame.K_w :
pressed_key = 'w'
#same thing but for every letter in the alphabet...
if event.key == pygame.K_PLUS :
pressed_key = '+'
if event.key == pygame.K_BACKSPACE :
kiriko = kiriko[:-1]
if event.type == pygame.KEYUP:
pressed_key = ''
try:
message_small(kiriko, display_width/2, display_height/1.3)
message_display("Enter your name")
pygame.display.update()
window.fill(white)
enter_name()
kiriko += pressed_key
message_small(kiriko, display_width/2, display_height/1.3)
message_display("Enter your name")
pygame.display.update()
clock.tick(120)
except:
message_small(kiriko, display_width/2, display_height/1.3)
message_display("Enter your name")
pygame.display.update()
window.fill(white)
enter_name()
message_display("Enter your name")
pygame.display.update()
clock.tick(120)
編輯:現在唯一的問題是閃爍的文字,因爲我得到了該
*另外,當我拿着鑰匙的那封信被迅速過書面及以上時,我只希望這一次寫的信,*那是因爲你在聽'KEYDOWN'事件。 ..如果你只想讓它觸發一次,那麼請聽一下'KEYUP' – Michael
@謝謝,它工作。對於愚蠢的問題。儘管我沒有看到閃爍屏幕的答案 –