简体   繁体   中英

create function for rendering text in one line

I'm trying to write a function to render text on the screen in one line.

The below code is giving me a TypeError:

 TypeError: argument 1 must be pygame.Surface, not None

Essentially, I don't want to do something like textsurface = font.render("some text", False, (200, 200, 200) screen.blit(text_surface, (100, 300)

Would rather somehow be able to do it all in one line?

Not sure what I am doing wrong here.

# Imports
import sys
import pygame

# Configuration
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)

# System Font
font = pygame.font.SysFont('Garamond', 30)

def write_text(text,anti_alias,colour):
    font.render(text,anti_alias,colour)

# Game loop.
while  True:
    screen.fill((20, 20, 20))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(write_text('haloooo', False, (200, 200, 200)), (100, 300))

    pygame.display.flip()

Okay so I realised that write_text() wasn't returning anything...

The below now works as intended.

def write_text(text,anti_alias,colour):
    return font.render(text,anti_alias,colour)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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