PyGame - 带有阴影的文本

我有一个任务,我们需要在 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()


侃侃尔雅
浏览 240回答 2
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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python