集中表面上的文本

时间:2013-11-05 20:52:35

标签: python pygame

如果我在Pygame中有两个表面,一个是窗口而另一个是包含一些文本,我如何以编程方式集中窗口内的文本表面(不是手动使用幻数)?

2 个答案:

答案 0 :(得分:1)

你总是可以用数学而不是魔术数来做,而不知道任何关于pygame的事情:

top = outer.get_height()/2 - inner.get_height()/2
left = outer.get_width()/2 - inner.get_width()/2

任何图形库中(除了表面'坐标系可以具有不同分辨率的那个)之外,它在外表面的坐标系中为您提供内表面的左上角。 / p>

答案 1 :(得分:1)

您可以使用pygame.Rect()rect.centerrect.topleft

screen_rect = screen.get_rect() # get screen rectangle
text_rect = text.get_rect() # get text rectangle
text_rect.center = screen_rect.center # center text on screen

现在您可以在位置text_rect.topleft

的屏幕上显示文字
screen.blit( text, text_rect.topleft )

rect还有其他有用的属性:

top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h

完整代码:(按空格暂停)

import sys

import pygame
from pygame.locals import *


#----------------------------------------------------------------------

class Game():

    def __init__(self):

        pygame.init()

        fpsClock = pygame.time.Clock()

        screen = pygame.display.set_mode((1200,600),0,32)

        font = pygame.font.SysFont("", 72)

        text_pause = font.render("PAUSE", True, (255, 0, 0))

        screen_rect = screen.get_rect() # get screen rect
        text_rect = text_pause.get_rect() # get text rect
        text_rect.center = screen_rect.center # center text on screen

        RUNNING = True
        PAUSED = True

        while RUNNING:

            for event in pygame.event.get():
                if event.type==QUIT:
                    RUNNING = False

                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        RUNNING = False
                    elif event.key == K_SPACE:
                        PAUSED = not PAUSED

            screen.fill(0)

            if PAUSED:
                screen.blit(text_pause, text_rect.topleft)

            pygame.display.update()

            fpsClock.tick(25)

        # --- finish ---

        pygame.quit()
        sys.exit()

#----------------------------------------------------------------------

Game().run()

您可以使用rect.center将文字置于鼠标位置中心。

完整代码:(移动鼠标)

import sys

import pygame
from pygame.locals import *


#----------------------------------------------------------------------

class Game():

    def __init__(self):

        pygame.init()

        fpsClock = pygame.time.Clock()

        screen = pygame.display.set_mode((1200,600),0,32)

        font = pygame.font.SysFont("", 72)

        text_pause = font.render("PAUSE", True, (255, 0, 0))

        screen_rect = screen.get_rect() # get screen rect
        text_rect = text_pause.get_rect() # get text rect
        text_rect.center = screen_rect.center # center text on screen

        # --- mainloop ---

        RUNNING = True
        PAUSED = True

        while RUNNING:

            # --- events ---

            for event in pygame.event.get():
                if event.type==QUIT:
                    RUNNING = False

                if event.type == KEYDOWN:
                    if event.key == K_ESCAPE:
                        RUNNING = False
                    elif event.key == K_SPACE:
                        PAUSED = not PAUSED

                if event.type == MOUSEMOTION:
                    text_rect.center = event.pos

            # --- calcuations/moves ---

            # --- draws ---

            screen.fill(0)

            if PAUSED:
                screen.blit(text_pause, text_rect.topleft)

            pygame.display.update()

            # --- FPS ---

            fpsClock.tick(25)

        # --- finish ---

        pygame.quit()
        sys.exit()

#----------------------------------------------------------------------

Game().run()