我可以创建一个矩形表面并在该表面上绘制文本,然后在pygame中将这些对象一起变薄吗 [英] can I create a rectangular surface and draw a text over that and blit these objects together in pygame

查看:80
本文介绍了我可以创建一个矩形表面并在该表面上绘制文本,然后在pygame中将这些对象一起变薄吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个矩形对象,并针对该矩形对象绘制文本,然后将这些项目在屏幕上一起显示.我正在使用pygame库来创建游戏.我对此pygame编程并不陌生.有人可以建议我使用方法吗?

I need to create a rectangular object and draw a text with respect to the rectangular object and blit these items together in the screen. I am using pygame library to create the game.I am new to this pygame programming. Can someone suggest me method if it is possible?

推荐答案

文本表面是与其他任何表面一样的表面对象.呈现文本:

A text surface is a surface object like any other surface. Render the text:

font = pygame.font.SysFont(None, 80)
text_surf = font.render('test text', True, (255, 0, 0))

创建一个表面,并在其上 blit 将文本表面:

Create a surface and blit the text surface on it:

rect_surf = pygame.Surface((400, 100))
rect_surf.fill((0, 128, 255))
rect_surf.blit(text_surf, text_surf.get_rect(center = rect_surf.get_rect().center))


最小示例:


Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((500, 300))
clock = pygame.time.Clock()

font = pygame.font.SysFont(None, 80)
text_surf = font.render('test text', True, (255, 0, 0))

rect_surf = pygame.Surface((400, 100))
rect_surf.fill((0, 128, 255))
rect_surf.blit(text_surf, text_surf.get_rect(center = rect_surf.get_rect().center))

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill(0)
    window.blit(rect_surf, rect_surf.get_rect(center = window.get_rect().center))
    pygame.display.flip()

pygame.quit()
exit()


请参阅:


See alos:

这篇关于我可以创建一个矩形表面并在该表面上绘制文本,然后在pygame中将这些对象一起变薄吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆