我在pygame中編寫項目,現在正在編寫Login部分。 我寫了一個函數,打印一個字符到屏幕的特定位置,但它不起作用,我不知道爲什麼。我有用於在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()