创建的曲面上的多边形

时间:2019-06-17 21:55:54

标签: python python-2.7 pygame pygame-surface

我试图在新的表面上绘制箭头,以便随后旋转它,但是当我在创建的新表面上绘制箭头时,它不会显示。

将图标放置在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)

那么就没有问题了

1 个答案:

答案 0 :(得分:0)

坐标 (icon_position) 超出了 player_icon 表面。坐标必须相对于绘制多边形的表面。曲面的左上角坐标始终为 (0, 0),即使该曲面稍后 blit 位于不同位置。

如果 icon_position 中的坐标是绝对窗口坐标,则必须相对于 player_icon Surface 平移坐标:

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)