如何在形状内居中文本? [英] How to center text inside a shape?

查看:53
本文介绍了如何在形状内居中文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在圆圈内显示一个数字.数字必须居中 (x,y).怎么做?

导入pygame、syspygame.init()屏幕= pygame.display.set_mode((0, 0))黑色 = (0,0,0)白色 = (255,255,255)红色 = (255,0,0)数字=8";bigfont = pygame.font.SysFont("arial", 50)text = bigfont.render(number, True, white)定义绘制():pygame.draw.circle(屏幕,红色,(300,300),200)screen.blit(文本,(300,300))pygame.display.update()

解决方案

获取文本的边界矩形并通过文本的中心设置矩形的中心.使用矩形blit文本:

text_rect = text.get_rect()text_rect.center = (300, 300)screen.blit(文本,text_rect)

一行代码一样:

screen.blit(text, text.get_rect(center = (300, 300)))

在函数中使用此代码:

def draw_circle(x, y, text):pygame.draw.circle(屏幕,红色,(x,y),200)screen.blit(text, text.get_rect(center = (x, y)))

参见

导入pygamepygame.init()窗口 = pygame.display.set_mode((500, 500))font = pygame.font.SysFont(None, 100)时钟 = pygame.time.Clock()text = font.render("Text", True, (255, 255, 0))运行 = 真运行时:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误window_center = window.get_rect().centerwindow.fill(0)pygame.draw.circle(window, (255, 0, 0), window_center, 100)window.blit(text, text.get_rect(center = window_center))pygame.display.flip()时钟滴答(60)pygame.quit()出口()

I want to display a number inside a circle. The number must be centered (x,y). How to do it?

import pygame, sys

pygame.init()
screen= pygame.display.set_mode((0, 0))
black= (0,0,0)
white= (255,255,255)
red= (255,0,0)
number= "8"
bigfont = pygame.font.SysFont("arial", 50)
text = bigfont.render(number, True, white)

def draw():
    pygame.draw.circle(screen, red, (300,300), 200)
    screen.blit(text, (300,300))
    pygame.display.update()

解决方案

Get the bounding rectangle of the text and set the center of the rectangle by the center of the text. Use the rectangle to blit the text:

text_rect = text.get_rect()
text_rect.center = (300, 300)
screen.blit(text, text_rect)

The same with one line of code:

screen.blit(text, text.get_rect(center = (300, 300)))

Use this code in a function:

def draw_circle(x, y, text):
    pygame.draw.circle(screen, red, (x, y), 200)
    screen.blit(text, text.get_rect(center = (x, y)))

See pygame.Surface.blit:

[...] The dest argument can either be a pair of coordinates representing the position of the upper left corner of the blit or a Rect, where the upper left corner of the rectangle will be used as the position for the blit.


Minimal example:

import pygame

pygame.init()
window = pygame.display.set_mode((500, 500))
font = pygame.font.SysFont(None, 100)
clock = pygame.time.Clock()
text = font.render("Text", True, (255, 255, 0))

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

    window_center = window.get_rect().center

    window.fill(0)
    pygame.draw.circle(window, (255, 0, 0), window_center, 100)
    window.blit(text, text.get_rect(center = window_center))
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

这篇关于如何在形状内居中文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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