簡體   English   中英

PyGame - 帶陰影的文本

[英]PyGame - Text with a Drop-shadow

我有一個任務,我們需要創建函數以在 python/pygame 中將文本顯示到屏幕上。 這部分我明白了。 我不知道的是你應該創建一個 function 來創建投影。 我知道如何制作陰影我只是不知道如何制作另一個 function 來做它並且可以選擇從預先存在的文本中調用。 這是我到目前為止所擁有的

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()

可以通過兩次繪制文本來渲染陰影。 第一個是偏移位置的文本的灰色版本,然后是原始位置的實際文本。

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)

我在金斯利的回應的幫助下找到了答案。 現在,您可以選擇是否添加陰影,陰影的位置和顏色。 也許有一種更簡單的方法,但這對我有用。

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()

如果你想要一個透明的陰影,你必須:

def renderTextDropShadow(font, text, color, dx, dy, shadowColor, shadowAlpha):
    textSize = font.size(text)
    surf = pygame.Surface((textSize[0] + abs(dx), textSize[1] + abs(dy)), pygame.SRCALPHA)
    shadowSurf = font.render(text, True, shadowColor)
    shadowSurf.set_alpha(shadowAlpha)
    textSurf = font.render(text, True, color)
    surf.blit(shadowSurf, (max(0, dx), max(0, dy)))
    surf.blit(textSurf, (max(0, -dx), max(0, -dy)))
    return surf

最小的例子:

import pygame

def renderTextDropShadow(font, text, color, dx, dy, shadowColor=(127, 127, 127), shadowAlpha = 127):
    textSize = font.size(text)
    surf = pygame.Surface((textSize[0] + abs(dx), textSize[1] + abs(dy)), pygame.SRCALPHA)
    shadowSurf = font.render(text, True, shadowColor)
    shadowSurf.set_alpha(shadowAlpha)
    textSurf = font.render(text, True, color)
    surf.blit(shadowSurf, (max(0, dx), max(0, dy)))
    surf.blit(textSurf, (max(0, -dx), max(0, -dy)))
    return surf

pygame.init()
window = pygame.display.set_mode((700, 200))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 150)

textSurf = renderTextDropShadow(font, 'Hello World!', (0, 0, 0), -15, 10)

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (255, 255, 255), (255, 164, 196)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
    pygame.draw.rect(background, color, rect)

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

    window.blit(background, (0, 0))
    window.blit(textSurf, textSurf.get_rect(center = window.get_rect().center))
    pygame.display.flip()

pygame.quit()
exit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM