繁体   English   中英

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

[英]How do I draw text onto the screen in pygame?

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

暂无
暂无

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

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