【发布时间】:2020-02-29 18:17:16
【问题描述】:
我试图在 pygame 中将任何文本绘制到屏幕上,但是当我运行程序时,它只是正常运行,但屏幕上没有出现任何文本。是不是我做错了什么?
import pygame, random, sys
from pygame.locals import *
pygame.init()
#set constants
WINDOWWIDTH = 600
WINDOWHEIGHT = 600
TEXTCOLOR = (255, 255, 255)
BACKGROUNDCOLOR = (0, 0, 255)
FPS = 40
BLACK = (0,0,0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
#set variables
rectY1 = 200
rectY2 = 200
Y1change = 0
Y2change = 0
ballX = 320
ballY = 240
ballXvel = 0
ballYvel = 0
paddle1 = pygame.Rect(18,rectY1,10,100)
paddle2 = pygame.Rect(620,rectY2,10,100)
ball = pygame.Rect(ballX,ballY,30,30)
font = pygame.font.SysFont(None, 48)
#create shapes
def drawshapes():
pygame.draw.rect(DISPLAY, WHITE, paddle1)
pygame.draw.rect(DISPLAY, WHITE, paddle2)
pygame.draw.rect(DISPLAY, WHITE, ball)
def drawText(text, font, surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
#set up display
DISPLAY = pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption('Pong')
fpsClock = pygame.time.Clock()
drawText('bruh', font, DISPLAY, (200), (200))
pygame.display.update
【问题讨论】:
-
pygame.display.update不是函数吗? -
...
pygame.display.update->pygame.display.update()。这是一个错字,你错过了括号。 -
@Rabbid76 还是不行。还有什么吗?
-
@beginnercoder 您的代码运行良好。但是由于您没有应用程序循环,因此窗口会立即打开和关闭。您必须在应用程序循环中绘制文本。请参阅How to display text in pygame? 或How to make a string's content appears on screen as we type on keyboard?。
-
找到任何教程,你应该会看到它使用循环来获取事件——并且这个循环保持程序运行直到你退出这个循环。