使矩形对象可滚动
Make a rect object scrollable
我正在将国际象棋移动日志绘制到一个矩形对象上 - 但是在某个点之后,文本从底部消失并被切断。我想知道是否可以使矩形表面滚动以便我可以看到整个移动日志。
绘制移动日志的代码如下:
我就是这个意思
正如您在第 21 次移动后看到的那样 - 日志超出移动日志区域的底部
def drawMoveLog(screen, gs, font): #draws move log
moveLogArea = p.Rect(BOARD_WIDTH, 0, MOVE_LOG_PANEL_WIDTH, MOVE_LOG_PANEL_HEIGHT)
p.draw.rect(screen , p.Color("gray"), moveLogArea)
moveLog = gs.moveLog
moveText = []
for i in range(0, len(moveLog), 2): #go through move log 2 at a time
moveString = "|| " + str(i//2 + 1) + ". " + str(moveLog[i]) + " "# to keep move 2 and 2 the same
if i + 1 < len(moveLog): # before i continue want to make sure black moved
moveString += str(moveLog[i + 1]) + " "
moveText.append(moveString)
movesPerRow = 1
padding = 5
lineSpacing = 4
textY = padding
#make 3 moves go in 1 line
for i in range(0, len(moveText), movesPerRow):
text = ""
for j in range (movesPerRow):
if i + j < len(moveText):
text += moveText[i+j]
textObject = font.render(text, True, p.Color('Black'))
textLocation = moveLogArea.move(padding, textY)
screen.blit(textObject, textLocation)
textY += textObject.get_height() + lineSpacing
创建一个函数,以足够高的透明 pygame.Surface
呈现文本以包含完整文本。要创建透明的 Surface 对象,您必须设置标志 pygame.SRCALPHA
:
def createMoveLog(gs, font): #draws move log
moveLog = gs.moveLog
moveText = []
for i in range(0, len(moveLog), 2): #go through move log 2 at a time
moveString = "|| " + str(i//2 + 1) + ". " + str(moveLog[i]) + " "# to keep move 2 and 2 the same
if i + 1 < len(moveLog): # before i continue want to make sure black moved
moveString += str(moveLog[i + 1]) + " "
moveText.append(moveString)
movesPerRow = 1
padding = 5
lineSpacing = 4
no_of_lines = (len(moveText)+movesPerRow-1) // movesPerRow
line_height = font.get_height() + lineSpacing
text_size = (MOVE_LOG_PANEL_WIDTH, no_of_lines * line_height + 2*padding)
text_surface = p.Surface(text_size, p.SRCALPHA)
textY = padding
#make 3 moves go in 1 line
for i in range(0, len(moveText), movesPerRow):
text = ""
for j in range (movesPerRow):
if i + j < len(moveText):
text += moveText[i+j]
textObject = font.render(text, True, p.Color('Black'))
text_surface.blit(textObject, (0, textY))
textY += textObject.get_height() + lineSpacing
return text_surface
呈现全文并使用 pygame.Surface.subsurface
创建一个新表面,该表面引用高度为 MOVE_LOG_PANEL_HEIGHT
的矩形区域。
scroll 参数是 [0.0, 1.0] 范围内的一个值,用于线性滚动文本。滚动为0.0时显示文字顶部,滚动为1.0时显示文字底部:
def drawMoveLog(screen, gs, font, scroll): #draws move log
moveLogArea = p.Rect(BOARD_WIDTH, 0, MOVE_LOG_PANEL_WIDTH, MOVE_LOG_PANEL_HEIGHT)
p.draw.rect(screen , p.Color("gray"), moveLogArea)
text_surface = createMoveLog(gs, font)
dy = text_surface.get_height() - MOVE_LOG_PANEL_HEIGHT
if dy > 0:
text_offset = int(dy * scroll)
test_rect = text_surface.get_rect()
sub_rect = p.Rect(0, text_offset, MOVE_LOG_PANEL_WIDTH, MOVE_LOG_PANEL_HEIGHT)
sub_text_surface = text_surface.subsurface(sub_rect)
screen.blit(sub_text_surface, moveLogArea)
else:
screen.blit(text_surface, moveLogArea)
对比
的渲染
moveLog = ["d3", "Nh6", "e3", "Rg8", "f3", "b6", "g3", "a5", "h3", "Ra6", "c3", "g6", "b3", "a4"]
与 scroll = 0.0
和 scroll = 1.0
。
我正在将国际象棋移动日志绘制到一个矩形对象上 - 但是在某个点之后,文本从底部消失并被切断。我想知道是否可以使矩形表面滚动以便我可以看到整个移动日志。
绘制移动日志的代码如下:
我就是这个意思
正如您在第 21 次移动后看到的那样 - 日志超出移动日志区域的底部
def drawMoveLog(screen, gs, font): #draws move log
moveLogArea = p.Rect(BOARD_WIDTH, 0, MOVE_LOG_PANEL_WIDTH, MOVE_LOG_PANEL_HEIGHT)
p.draw.rect(screen , p.Color("gray"), moveLogArea)
moveLog = gs.moveLog
moveText = []
for i in range(0, len(moveLog), 2): #go through move log 2 at a time
moveString = "|| " + str(i//2 + 1) + ". " + str(moveLog[i]) + " "# to keep move 2 and 2 the same
if i + 1 < len(moveLog): # before i continue want to make sure black moved
moveString += str(moveLog[i + 1]) + " "
moveText.append(moveString)
movesPerRow = 1
padding = 5
lineSpacing = 4
textY = padding
#make 3 moves go in 1 line
for i in range(0, len(moveText), movesPerRow):
text = ""
for j in range (movesPerRow):
if i + j < len(moveText):
text += moveText[i+j]
textObject = font.render(text, True, p.Color('Black'))
textLocation = moveLogArea.move(padding, textY)
screen.blit(textObject, textLocation)
textY += textObject.get_height() + lineSpacing
创建一个函数,以足够高的透明 pygame.Surface
呈现文本以包含完整文本。要创建透明的 Surface 对象,您必须设置标志 pygame.SRCALPHA
:
def createMoveLog(gs, font): #draws move log
moveLog = gs.moveLog
moveText = []
for i in range(0, len(moveLog), 2): #go through move log 2 at a time
moveString = "|| " + str(i//2 + 1) + ". " + str(moveLog[i]) + " "# to keep move 2 and 2 the same
if i + 1 < len(moveLog): # before i continue want to make sure black moved
moveString += str(moveLog[i + 1]) + " "
moveText.append(moveString)
movesPerRow = 1
padding = 5
lineSpacing = 4
no_of_lines = (len(moveText)+movesPerRow-1) // movesPerRow
line_height = font.get_height() + lineSpacing
text_size = (MOVE_LOG_PANEL_WIDTH, no_of_lines * line_height + 2*padding)
text_surface = p.Surface(text_size, p.SRCALPHA)
textY = padding
#make 3 moves go in 1 line
for i in range(0, len(moveText), movesPerRow):
text = ""
for j in range (movesPerRow):
if i + j < len(moveText):
text += moveText[i+j]
textObject = font.render(text, True, p.Color('Black'))
text_surface.blit(textObject, (0, textY))
textY += textObject.get_height() + lineSpacing
return text_surface
呈现全文并使用 pygame.Surface.subsurface
创建一个新表面,该表面引用高度为 MOVE_LOG_PANEL_HEIGHT
的矩形区域。
scroll 参数是 [0.0, 1.0] 范围内的一个值,用于线性滚动文本。滚动为0.0时显示文字顶部,滚动为1.0时显示文字底部:
def drawMoveLog(screen, gs, font, scroll): #draws move log
moveLogArea = p.Rect(BOARD_WIDTH, 0, MOVE_LOG_PANEL_WIDTH, MOVE_LOG_PANEL_HEIGHT)
p.draw.rect(screen , p.Color("gray"), moveLogArea)
text_surface = createMoveLog(gs, font)
dy = text_surface.get_height() - MOVE_LOG_PANEL_HEIGHT
if dy > 0:
text_offset = int(dy * scroll)
test_rect = text_surface.get_rect()
sub_rect = p.Rect(0, text_offset, MOVE_LOG_PANEL_WIDTH, MOVE_LOG_PANEL_HEIGHT)
sub_text_surface = text_surface.subsurface(sub_rect)
screen.blit(sub_text_surface, moveLogArea)
else:
screen.blit(text_surface, moveLogArea)
对比
的渲染moveLog = ["d3", "Nh6", "e3", "Rg8", "f3", "b6", "g3", "a5", "h3", "Ra6", "c3", "g6", "b3", "a4"]
与 scroll = 0.0
和 scroll = 1.0
。