如何添加文本到pygame矩形 [英] How to add text into a pygame rectangle

查看:785
本文介绍了如何添加文本到pygame矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经来到在pygame绘制一个矩形,但我需要能够得到像Hello到这个矩形的文本。我该怎么办? (



这是我的代码:

  import pygame 
import sys
from pygame.locals import *

white =(255,255,255)
black = 0,0,0)


类Pane(object):
def __init __(self):

pygame.init b pygame.display.set_caption('Box Test')
self.screen = pygame.display.set_mode((600,400),0,32)
self.screen.fill((white))
pygame.display.update()

def addRect(self):
self.rect = pygame.draw.rect(self.screen,(black),(175, 200,100),2)
pygame.display.update()

def addText(self):
#这是我想从

如果__name__ =='__main__':
Pan3 = Pane()
Pan3.addRect()
while True:
for event in pygame.event.get ():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit();感谢您的时间。

>解决方案

您必须先创建一个 Font (或 SysFont )对象。调用 呈现 方法将返回 Surface ,您可以在屏幕上或任何其他 Surface

  import pygame 
从pygame.locals导入


white =(255,255,255)
black =(0,0,0)
b
$ b class Pane(object):
def __init __(self):
pygame.init()
self.font = pygame.font.SysFont('Arial', 25)
pygame.display.set_caption('Box Test')
self.screen = pygame.display.set_mode((600,400),0,32)
self.screen.fill white))
pygame.display.update()


def addRect(self):
self.rect = pygame.draw.rect(self.screen, (黑色),(175,75,200,100),2)
pygame.display.update()

def addText(self):
self.screen.blit (self.font.render('Hello!',True,(255,0,0)),(200,100))
pygame.display.update()

如果__name__ =='__main__':
Pan3 = Pane()
Pan3.addRect()
Pan3.addText()
while True:
for event in pygame.event .get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit();



请注意,你的代码似乎有点奇怪,因为通常你在主循环中做所有的绘图,而不是事先。另外,当你在程序中大量使用文本时,考虑缓存 Font.render 的结果,因为它是一个非常缓慢的操作。


I have come as far as drawing a rectangle in pygame however I need to be able to get text like "Hello" into that rectangle. How can I do this? (If you can explain it as well that would be much appreciated. Thank-you)

Here is my code:

import pygame
import sys
from pygame.locals import *

white = (255,255,255)
black = (0,0,0)


class Pane(object):
    def __init__(self):

        pygame.init()
        pygame.display.set_caption('Box Test')
        self.screen = pygame.display.set_mode((600,400), 0, 32)
        self.screen.fill((white))
        pygame.display.update()

    def addRect(self):
        self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
        pygame.display.update()

    def addText(self):
        #This is where I want to get the text from

if __name__ == '__main__':
    Pan3 = Pane()
    Pan3.addRect()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit();

Thank you for your time.

解决方案

You first have to create a Font (or SysFont) object. Calling the render method on this object will return a Surface with the given text, which you can blit on the screen or any other Surface.

import pygame
import sys
from pygame.locals import *

white = (255,255,255)
black = (0,0,0)


class Pane(object):
    def __init__(self):
        pygame.init()
        self.font = pygame.font.SysFont('Arial', 25)
        pygame.display.set_caption('Box Test')
        self.screen = pygame.display.set_mode((600,400), 0, 32)
        self.screen.fill((white))
        pygame.display.update()


    def addRect(self):
        self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
        pygame.display.update()

    def addText(self):
        self.screen.blit(self.font.render('Hello!', True, (255,0,0)), (200, 100))
        pygame.display.update()

if __name__ == '__main__':
    Pan3 = Pane()
    Pan3.addRect()
    Pan3.addText()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit();

Note that your code seems a little bit strange, since usually you do all the drawing in the main loop, not beforehand. Also, when you make heavy use of text in your program, consider caching the result of Font.render, since it is a very slow operation.

这篇关于如何添加文本到pygame矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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