繁体   English   中英

如何在python 3中编译pygame

[英]how to compile pygame in python 3

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

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 = (win, "Typing Speed", 120, 80, 80, (0, 255, 255))

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

删除 = 可以解决问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM