【问题标题】:Polygons on created surfaces创建曲面上的多边形
【发布时间】:2019-10-31 12:33:15
【问题描述】:

我正在尝试在新表面上绘制一个箭头,这样我就可以旋转它,但是当我在我创建的新表面上绘制它时,它不会显示出来。

图标在放在game_display 表面时显示,但在放在player_icon 表面时不显示

import pygame

pygame.init()

white = (255,255,255)
black = (0,0,0)
grey = (175,175,175)

display_width = 1365
display_height = 700
game_display = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption("First Person Shooter")
game_display.fill(black)
clock = pygame.time.Clock()
player_rotation = 0
player_icon_height = 15
player_icon_width = 15

def draw_map():
    player_icon = pygame.Surface((player_icon_width, player_icon_height))
    player_icon.fill(black)
    player_icon.set_colorkey(black)
    icon_position = [[int(player_location_x),int(player_location_y - (player_icon_height / 2))],[int(player_location_x - (player_icon_width / 2)),int(player_location_y + (player_icon_height / 2))],[int(player_location_x),int(player_location_y)],[int(player_location_x + (player_icon_width / 2)),int(player_location_y + (player_icon_height / 2))]]

    #This next line is the issue
    pygame.draw.polygon(player_icon, white, icon_position)
    blitted = game_display.blit(player_icon, [player_location_x - (player_icon_width / 2), player_location_y - (player_icon_height / 2)])
    player_icon_rotated = pygame.transform.rotate(player_icon, player_rotation)
    player_icon_rotated_rect = player_icon_rotated.get_rect()
    player_icon_rotated_rect.center = blitted.center
    game_display.blit(player_icon_rotated, player_icon_rotated_rect)
    pygame.display.flip()
    pygame.display.update()

draw_map()

它不会出现在新的表面上,但如果我将这条线替换为:

pygame.draw.polygon(game_display, white, icon_position)

那就没有问题了

【问题讨论】:

  • 添加带有指针的屏幕并引用它...您在文本中迷失了我。还可以尝试通过简单的打印语句进行调试以查看一些值。这样,您可以在各个步骤检查您的输出。最坏的情况使用“import sys, print X, sys.stdout.flush()。也让它成为一个小的工作示例......使用 `__if name__ == main:.. 通常;p)
  • 我只是不明白相同的声明如何在某些表面上可见,而在其他表面上却不可见。在这种情况下,我真的不明白 print 语句如何提供帮助。不过感谢您的帮助。

标签: python python-2.7 pygame pygame-surface


【解决方案1】:

坐标 (icon_position) 超出了player_icon 表面的范围。坐标必须相对于在其上绘制多边形的 Surface。表面的左上角坐标始终为 (0, 0),即使该表面较晚blit 位于不同的位置。

如果icon_position 中的坐标是绝对窗口坐标,则必须将坐标相对于player_icon 进行平移表面

rel_icon_position = []
for x, y in icon_position:
    rel_icon_position.append((x - blitted.x, y - blitted.y))
pygame.draw.polygon(player_icon, white, icon_position)

分别

rel_icon_position = [(x - blitted.x, y - blitted.y) for x, y in icon_position]
pygame.draw.polygon(player_icon, white, icon_position)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 2021-11-08
    • 2013-12-11
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多