首页 > 解决方案 > 创建曲面上的多边形

问题描述

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

图标在放置在表面时显示,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)

那么就没有问题了

标签: pythonpython-2.7pygamepygame-surface

解决方案


坐标 ( icon_position) 超出了player_icon Surface的范围。坐标必须相对于绘制多边形的表面。表面的左上角坐标始终为 (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)

推荐阅读