-1

我不确切知道此代码的错误我没有收到任何错误,但我的代码没有执行。当我运行我的代码时,我看到了矩形的代码运行,运行后我在屏幕上看到了黄色的矩形,与代码相关的打印文本没有执行。我认为这段代码中的实际定义没有运行,也没有显示任何错误,而且我的代码末尾的打印文本也没有运行

import pygame
import sys
import random
from pygame.locals import *

pygame.init()

width = 800
height = 600
black = (0, 0, 0)
red = (255, 0, 0)
input_text = ''
typing = False


win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Type Speed Program")


def print_text(screen, massage, x, y, font_s, clr):
    font = pygame.font.Font(None, font_s)
    text = font.render(massage, True, clr)
    screen.blit(text, (x, y))
    pygame.display.update()


def get_sentence():
    t_f = open('new.txt').read()
    sentences = t_f.split("\n")
    sentence = random.choice(sentences)
    pygame.display.update()
    return sentence


def start_game():
    sentence_s = get_sentence()
    print_text = (win, sentence_s, 100, 250, 40, (255, 0, 0))
    pygame.draw.rect(win, (255, 255, 0), (75, 300, 650, 50), 3)
    pygame.display.update()


start_game()


while True:
   # win.fill(red)
    win.fill(black, (75, 300, 650, 50))
    print_text(win, input_text, 80, 310, 35, (255, 255, 255))
    for event in pygame.event.get():

        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            if event.button == 1:
                if (75 <= x <= 725) and (300 <= y <= 350):
                    typing = True
                    input_text = ''
        elif event.type == KEYDOWN:
            if typing:
                if event.key == K_RETURN:
                    print(input_text)
                if event.key == K_BACKSPACE:
                    input_text = input_text[:-1]
                else:
                    input_text += event.unicode

    print_text = (win, "Typing Speed", 120, 80, 80, (0, 255, 255))
    pygame.display.update()
4

1 回答 1

0

您的问题在倒数第二行:

print_text = (win, "Typing Speed", 120, 80, 80, (0, 255, 255))

在那里你用一个元组覆盖你定义的函数(print_text),然后你在游戏循环中调用这个元组。

删除 = 可以解决问题。

print_text(win, "Typing Speed", 120, 80, 80, (0, 255, 255))
于 2021-07-27T10:36:24.117 回答