【问题标题】:how to compile pygame in python 3如何在 python 3 中编译 pygame
【发布时间】:2021-07-27 10:21:16
【问题描述】:

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

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

【问题讨论】:

  • 使用print_text(...) 而不是print_text = (...)

标签: python pygame python-3.7 python-3.9 pygame2


【解决方案1】:

你的问题在倒数第二行:

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

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

去掉=就解决了问题。

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

【讨论】:

    猜你喜欢
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-15
    相关资源
    最近更新 更多