我有一个作业,我们需要在其中创建函数以在python / pygame中将文本显示在屏幕上。这部分我了解。我不是,您应该创建一个创建投影的函数。我知道如何制作阴影,我只是不知道如何制作另一个函数来做到这一点,并且可以选择从现有的文本中调用。这是我到目前为止所拥有的
import pygame
import sys
pygame.init()
screenSizeX = 1080
screenSizeY = 720
screenSize = (screenSizeX,screenSizeY)
screen = pygame.display.set_mode(screenSize,0)
pygame.display.set_caption("Test Functions")
WHITE = (255,255,255)
GREEN = (0,255,0)
BLUE = (0,0,255)
RED = (255,0,0)
YELLOW = (255,255,0)
BLACK = (0,0,0)
MAGENTA = (139,0,139)
def horCenter(font, size, text, colour, y, shadow, pos):
if shadow == True:
fontTitle = pygame.font.SysFont(font, size)
textTitle = fontTitle.render(text, True, colour)
textWidth = textTitle.get_width()
screen.blit(textTitle, (screenSizeX/2 - textWidth/2, y))
def verCenter(font, size, text, colour, x):
fontTitle = pygame.font.SysFont(font, size)
textTitle = fontTitle.render(text, True, colour)
textHeight = textTitle.get_height()
screen.blit(textTitle, (x, screenSizeY/2 - textHeight/2))
def cenCenter(font, size, text, colour):
fontTitle = pygame.font.SysFont(font, size)
textTitle = fontTitle.render(text, True, colour)
textHeight = textTitle.get_height()
textWidth = textTitle.get_width()
screen.blit(textTitle, (screenSizeX/2 - textWidth/2, screenSizeY/2 - textHeight/2))
pygame.display.update()
go = True
while go:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
go = False
screen.fill(WHITE)
horCenter("Comic Sans MS", 40, "Text1", MAGENTA, 100)
verCenter("Georgia", 10, "Tex2", GREEN, 500)
cenCenter("Impact", 50, "Text3", RED)
verCenter("Verdana", 60, "89274", BLACK, 50)
pygame.display.update()
pygame.quit()
sys.exit()
答案 0 :(得分:2)
可以通过两次绘制文本来渲染阴影。第一个是文本的灰色版本,位于偏移处,然后是实际文本,位于原始位置。
def dropShadowText(screen, text, size, x, y, colour=(255,255,255), drop_colour=(128,128,128), font=None):
# how much 'shadow distance' is best?
dropshadow_offset = 1 + (size // 15)
text_font = pygame.font.Font(font, size)
# make the drop-shadow
text_bitmap = text_font.render(text, True, drop_colour)
screen.blit(text_bitmap, (x+dropshadow_offset, y+dropshadow_offset) )
# make the overlay text
text_bitmap = text_font.render(text, True, colour)
screen.blit(text_bitmap, (x, y) )
所以您可以这样称呼它:
dropShadowText(screen, "Hello World", 36, 50, 50)
答案 1 :(得分:0)
我在金斯利的回应的帮助下找到了答案。现在,您可以选择是否添加阴影,阴影的位置和颜色。也许有一种更简单的方法,但这对我有用。
import pygame
import sys
pygame.init()
screenSizeX = 1080
screenSizeY = 720
screenSize = (screenSizeX,screenSizeY)
screen = pygame.display.set_mode(screenSize,0)
pygame.display.set_caption("Test Functions")
WHITE = (255,255,255)
GREEN = (0,255,0)
BLUE = (0,0,255)
RED = (255,0,0)
YELLOW = (255,255,0)
BLACK = (0,0,0)
MAGENTA = (139,0,139)
def horCenter(font, size, text, colour, yPos, shadow, shadowPlace, shadowColour):
fontTitle = pygame.font.SysFont(font, size)
textTitle = fontTitle.render(text, True, colour)
textWidth = textTitle.get_width()
xPos = screenSizeX/2 - textWidth/2
if shadow == "True":
dropShadow(font,size,text,colour, xPos,yPos, shadowPlace, shadowColour)
else:
screen.blit(textTitle, (xPos, yPos))
def verCenter(font, size, text, colour, xPos, shadow, shadowPlace, shadowColour):
fontTitle = pygame.font.SysFont(font, size)
textTitle = fontTitle.render(text, True, colour)
textHeight = textTitle.get_height()
yPos = screenSizeY/2 - textHeight/2
if shadow == "True":
dropShadow(font,size,text,colour, xPos,yPos, shadowPlace, shadowColour)
else:
screen.blit(textTitle, (xPos, yPos))
def cenCenter(font, size, text, colour, shadow, shadowPlace, shadowColour):
fontTitle = pygame.font.SysFont(font, size)
textTitle = fontTitle.render(text, True, colour)
textHeight = textTitle.get_height()
textWidth = textTitle.get_width()
xPos = screenSizeX/2 - textWidth/2
yPos = screenSizeY/2 - textHeight/2
if shadow == "True":
dropShadow(font,size,text,colour, xPos,yPos, shadowPlace, shadowColour)
else:
screen.blit(textTitle, (xPos, yPos))
def dropShadow(font, size, text, colour, xPos,yPos, shadowPlace, shadowColour):
fontTitle = pygame.font.SysFont(font, size)
textTitle = fontTitle.render(text, True, colour)
shadowTitle = fontTitle.render(text, True, shadowColour)
if shadowPlace == "bottomRight":
newXPos = xPos +2
newYPos = yPos + 2
screen.blit(shadowTitle, (newXPos,newYPos))
screen.blit(textTitle, (xPos,yPos))
elif shadowPlace == "bottomLeft":
newXPos = xPos - 2
newYPos = yPos + 2
screen.blit(shadowTitle, (newXPos, newYPos))
screen.blit(textTitle, (xPos, yPos))
elif shadowPlace == "topRight":
newXPos = xPos - 2
newYPos = yPos - 2
screen.blit(shadowTitle, (newXPos, newYPos))
screen.blit(textTitle, (xPos, yPos))
elif shadowPlace == "topLeft":
newXPos = xPos + 2
newYPos = yPos - 2
screen.blit(shadowTitle, (newXPos, newYPos))
screen.blit(textTitle, (xPos, yPos))
pygame.display.update()
go = True
while go:
for event in pygame.event.get():
if event.type ==pygame.QUIT:
go = False
screen.fill(WHITE)
horCenter("Comic Sans MS", 40, "Thanos", MAGENTA, 100, "True", "topLeft", YELLOW)
verCenter("Georgia", 10, "HULK SMASH", GREEN, 800, "True", "bottomRight", BLACK)
cenCenter("Impact", 50, "MARVEL", RED, "True", "bottomRight", BLACK)
verCenter("Verdana", 60, "Black Widow", BLACK, 50, "True", "topRight", RED)
pygame.display.update()
pygame.quit()
sys.exit()