gpt4 book ai didi

text - 添加新文本时,它出现在底部,其余文本上升

转载 作者:行者123 更新时间:2023-12-05 07:25:51 25 4
gpt4 key购买 nike

我想在文本框上显示文本,说明我是否命中以及我对敌人造成多少伤害,反之亦然,但我只是想不出如何让文本以这种方式显示。

这是我正在处理的代码:

def textBox(textv):
lText = []
text = font.render(str(textv),True,(1,1,1))
lText.append(text)
if len(lText) >= 10:
lText.pop(9)
screen.blit(lText[0],(20,400))
screen.blit(lText[1],(20,380))

while True:

battle_screen()
for event in pygame.event.get():

if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

screen.blit(enemy_background,(20,20))
player.drawPlayer()
enemy.newEnemy()
textBox("Daniel")
textBox("Jenny")


pygame.display.update()

最佳答案

渲染文本并将渲染的文本添加到列表中:

text_surf_list = []
text_surf = font.render("Daniel", True, (255, 255, 0))
text_surf_list.append(text_surf)
text_surf = font.render("Jenny", True, (255, 255, 0))
text_surf_list.append(text_surf)

为文本框定义一个矩形区域:

text_box_rect = pygame.Rect(20, 20, 250, 360)

遍历 reversed 中的列表命令。从下向上绘制文本。如果文本不在框中,则从列表中删除该文本(参见 How to remove items from a list while iterating? ):

text_y = box_rect.bottom - font.get_height()
for text_surf in reversed(text_surf_list[:]):
if text_y <= box_rect.top:
text_surf_list.remove(text_surf)
else:
surf.blit(text_surf, (box_rect.left + 10, text_y))
text_y -= font.get_height()

在使用pygame.Surface.set_clip 绘制文本框时设置临时剪切区域:

# set clipping
surf.set_clip(box_rect)

# draw text box
# [...]

# disable clipping
surf.set_clip(None)

最小的例子:

import pygame, random

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

def draw_scroll_text_box(surf, font, box_rect, text_surf_list):
surf.set_clip(box_rect)
text_y = box_rect.bottom - font.get_height() - 10
for text_surf in reversed(text_surf_list[:]):
if text_y <= box_rect.top:
text_surf_list.remove(text_surf)
else:
surf.blit(text_surf, (box_rect.left + 10, text_y))
text_y -= font.get_height()
pygame.draw.rect(surf, (164, 164, 164), box_rect, 5)
surf.set_clip(None)

font = pygame.font.SysFont(None, 50)
text_box_rect = pygame.Rect(20, 20, 250, 360)
text_surf_list = []
timer_event = pygame.USEREVENT+1
pygame.time.set_timer(timer_event, 200)

background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *background.get_size(), (32, 32, 32), (64, 64, 64)
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)]
[pygame.draw.rect(background, color, rect) for rect, color in tiles]

run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == timer_event:
names = ["Daniel", "Jenny", "Patrick", "Sandy", "Bob"]
text_surf = font.render(random.choice(names), True, (255, 255, 0))
text_surf_list.append(text_surf)

window.blit(background, (0, 0))
draw_scroll_text_box(window, font, text_box_rect, text_surf_list)
pygame.display.flip()
clock.tick(60)

pygame.quit()
exit()

关于text - 添加新文本时,它出现在底部,其余文本上升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54688018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com