python - 创建曲面上的多边形
问题描述
我正在尝试在新表面上绘制一个箭头,这样我就可以旋转它,但是当我在我创建的新表面上绘制它时,它不会出现。
图标在放置在表面时显示,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)
那么就没有问题了
解决方案
坐标 ( 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)
推荐阅读
- azure - 将根域指向 Azure Function
- xamarin - Xamarin Visual Studio for MAC. Automatic Provisioning failed: An App ID with Identifier '*' is not available
- nginx - Nginx 使用通配符 HTTP 请求清除缓存的实际方法是什么?
- r - Google Analytics API - R 中来自 Google.Adwords 的货币
- python - 将 fsolve 函数从 maple 重写为 python
- javascript - JavaScript 中的继承和原型
- java - 检测字符串包含一个数字,然后在它之前插入一个空格
- jquery - 选择日期时如何更改输入值
- javascript - 在 ng-repeat 中显示/隐藏按钮 onclick
- javascript - 返回上一页有效,但没有带有 react-native 的动画