我正在尝试创建一个打字游戏,并希望此人输入在一定时间内显示的单词,通过函数中的参数给出。我不知道该怎么做。我知道如何创建一个计时器,但它总是阻碍人们输入单词的能力。有人可以帮忙吗?如果需要,将提供更多代码。这是代码:
def text_objects(text, color, size):
if size == "small":
textSurf = smallFont.render(text, True, color)
elif size == "medium":
textSurf = medFont.render(text, True, color)
elif size == "large":
textSurf = largeFont.render(text, True, color)
elif size == "verySmall":
textSurf = vSmallFont.render(text, True, color)
return textSurf, textSurf.get_rect()
def messageToScreen(msg, color, y_displace = 0, size = "small"):
textSurface, textRect = text_objects(msg, color, size)
textRect.center = (display_width/2), (display_height/2) + y_displace
gameDisplay.blit(textSurface, textRect)
def text_to_button(msg, color, buttonX, buttonY, buttonWidth, buttonHeight, size = "small"):
textSurface, textRect = text_objects(msg, color, size)
textRect.center = ((buttonX + (buttonWidth/2), buttonY + (buttonHeight/2)))
gameDisplay.blit(textSurface, textRect)
def key(text,x,y,width,height,textColor,borderColor = black,textSize="verySmall"):
pygame.draw.rect(gameDisplay, blue, (x,y,width,height))
text_to_button(text,textColor,x,y,width,height,textSize)
pygame.draw.line(gameDisplay,borderColor,(x,y),(x+width,y))
pygame.draw.line(gameDisplay,borderColor,(x,y),(x,y+height))
pygame.draw.line(gameDisplay,borderColor,(x+width,y),(x+width,y+height))
pygame.draw.line(gameDisplay,borderColor,(x,y+height),(x+width,y+height))
def randWord():
rand = random.randint(0,996)
fp = open("1.txt")
for i, line in enumerate(fp):
if i == rand:
line = line.strip()
pygame.draw.rect(gameDisplay,white,(0,0,750,200))
messageToScreen(line,black,-200,size = "large")
global chars
chars = []
for char in line:
chars.extend(char)
def gameLoop(level):
pygame.display.update()
if level == '1':
gameDisplay.fill(white)
keyboard()
for turn in range(3):
randWord()
count = 0
while count < len(chars):
if chars[count] == 'a':
key("A",132.5,430,45,40,white)
letter = pygame.K_a
elif chars[count] == 'b':
key("B",342.5,470,45,40,white)
letter = pygame.K_b
elif chars[count] == 'c':
key("C",252.5,470,45,40,white)
letter = pygame.K_c
elif chars[count] == 'd':
key("D",222.5,430,45,40,white)
letter = pygame.K_d
elif chars[count] == 'e':
key("E",207.5,390,45,40,white)
letter = pygame.K_e
elif chars[count] == 'f':
key("F",267.5,430,45,40,white)
letter = pygame.K_f
elif chars[count] == 'g':
key("G",312.5,430,45,40,white)
letter = pygame.K_g
elif chars[count] == 'h':
key("H",357.5,430,45,40,white)
letter = pygame.K_h
elif chars[count] == 'i':
key("I",432.5,390,45,40,white)
letter = pygame.K_i
elif chars[count] == 'j':
key("J",402.5,430,45,40,white)
letter = pygame.K_j
elif chars[count] == 'k':
key("K",447.5,430,45,40,white)
letter = pygame.K_k
elif chars[count] == 'l':
key("L",492.5,430,45,40,white)
letter = pygame.K_l
elif chars[count] == 'm':
key("M",432.5,470,45,40,white)
letter = pygame.K_m
elif chars[count] == 'n':
key("N",387.5,470,45,40,white)
letter = pygame.K_n
elif chars[count] == 'o':
key("O",477.5,390,45,40,white)
letter = pygame.K_o
elif chars[count] == 'p':
key("P",522.5,390,45,40,white)
letter = pygame.K_p
elif chars[count] == 'q':
key("Q",117.5,390,45,40,white)
letter = pygame.K_q
elif chars[count] == 'r':
key("R",252.5,390,45,40,white)
letter = pygame.K_r
elif chars[count] == 's':
key("S",177.5,430,45,40,white)
letter = pygame.K_s
elif chars[count] == 't':
key("T",297.5,390,45,40,white)
letter = pygame.K_t
elif chars[count] == 'u':
key("U",387.5,390,45,40,white)
letter = pygame.K_u
elif chars[count] == 'v':
key("V",297.5,470,45,40,white)
letter = pygame.K_v
elif chars[count] == 'w':
key("W",162.5,390,45,40,white)
letter = pygame.K_w
elif chars[count] == 'x':
key("X",207.5,470,45,40,white)
letter = pygame.K_x
elif chars[count] == 'y':
key("Y",342.5,390,45,40,white)
letter = pygame.K_y
elif chars[count] == 'z':
key("Z",162.5,470,45,40,white)
letter = pygame.K_z
pygame.display.update()
true = True
while true:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == letter:
keyboard()
count += 1
pygame.display.update()
true = False
finishLevel()