我想在python中爲pygame製作一個腳本來繪製一個帶有文本居中的按鈕,但是當我在屏幕上blit時,它會傳遞給x和y我給它,而不是按比例居中的位置。我想能夠將它集中到一組(x,y,w,h)。我將如何做到這一點?這裏是我的代碼:pygame傳中 - 中心
# Imports
import pygame
class Text:
'Centered Text Class'
# Constructror
def __init__(self, text, (x,y,w,h), color = (0,0,0)):
self.x = x
self.y = y
self.w = w
self.h = h
# Start PyGame Font
pygame.font.init()
font = pygame.font.SysFont("sans", 20)
self.txt = font.render(text, True, color)
# Draw Method
def Draw(self, screen):
coords = (self.x, self.y)
screen.blit(self.txt, coords)
編輯:評論,是的,我知道,但我只用X和Y的臨時變量,因爲我不知道該中心的x和y將文本居中。 (我想知道如何將它的CENTER居中,而不是它的左上角)
但寬度和高度都無關,與X和Y位置。 – cdonts
按比例居中的位置是什麼? – martineau