如何在Python3中编译pygame

uemypmqf  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(240)

我不知道这段代码的确切错误,我没有得到任何错误,但我的代码没有执行。当我运行代码时,我看到了rect的代码运行,运行后,我看到屏幕上出现了黄色的RECTANGLE,与代码相关的打印文本不会执行。实际上,我认为这段代码中的def没有运行,也没有显示任何错误,而且代码末尾的打印文本也没有运行

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()
kgqe7b3p

kgqe7b3p1#

您的问题在最后一行的第二行:

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

在这里,您用一个元组覆盖定义的函数(print_text),然后在游戏循环中调用该元组。
移除=解决了问题。

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

相关问题