Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pygame中在屏幕上绘制文本?_Python_Pygame - Fatal编程技术网

Python 如何在pygame中在屏幕上绘制文本?

Python 如何在pygame中在屏幕上绘制文本?,python,pygame,Python,Pygame,我试图在pygame中在屏幕上绘制任何文本,但当我运行程序时,它只是正常运行,但屏幕上没有显示任何文本。我做错什么了吗 import pygame, random, sys from pygame.locals import * pygame.init() #set constants WINDOWWIDTH = 600 WINDOWHEIGHT = 600 TEXTCOLOR = (255, 255, 255) BACKGROUN

我试图在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

idk如果这真的是您正在谈论的循环,那么它就是:

running = True
while running:
    # RGB - Red, Green, Blue
    screen.fill((0, 0, 128 ))
    #background Image
    screen.blit(background, (0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

pygame.display.update不是一个函数吗
pygame.display.update
->
pygame.display.update()
。这是一个打字错误,你漏掉了括号。@Rabbid76它仍然不起作用。还有别的吗?@初学者你的代码很好用。但由于没有应用程序循环,窗口会立即打开和关闭。您必须在应用程序循环中绘制文本。请参阅或。查找任何教程,您应该会看到它使用循环来获取事件,并且该循环会保持程序运行,直到您退出该循环。