1 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
問題是您在表面積外繪制箭頭線。每個(gè)表面的左上角坐標(biāo)為 (0, 0),因此它們都有自己的(本地)坐標(biāo)系。由于您使用屏幕的中心坐標(biāo)來繪制線條,因此不會(huì)在可見表面區(qū)域中繪制箭頭。
我會(huì)使用局部坐標(biāo)在 while 循環(huán)之前的原始表面上繪制線條,然后旋轉(zhuǎn)它并在循環(huán)內(nèi)獲得一個(gè)新的矩形。
import math
import pygame
pygame.init()
CLOCK = pygame.time.Clock()
GRAY = pygame.color.THECOLORS['gray50']
WHITE = pygame.color.THECOLORS['white']
screen = pygame.display.set_mode((800, 600))
screen_rect = screen.get_rect() # A rect with the size of the screen.
surface = pygame.Surface((50, 50), pygame.SRCALPHA)
# surface.fill((0, 50, 150)) # Fill the surface to make it visible.
# Use the local coordinate system of the surface to draw the lines.
pygame.draw.line(surface, WHITE, (0, 0), (25, 0), 1)
pygame.draw.line(surface, WHITE, (0, 0), (25, 25), 1)
pygame.draw.line(surface, WHITE, (0, 0), (0, 25), 1)
# I rotate it so that the arrow is pointing to the right (0° is right).
surface = pygame.transform.rotate(surface, -135)
rect = surface.get_rect()
angle = 0
# If you're using Python 3.6+, you can use f-strings.
print(f"Size of the screen {screen.get_size()}")
print(f"Center of the screen {screen_rect.center}")
def calculate_angle(mouse_position):
dx = mouse_position[0] - screen_rect.centerx
dy = mouse_position[1] - screen_rect.centery
return math.atan2(dy, dx)
main_loop = True
while main_loop:
for event in pygame.event.get():
# You shouldn't use `pygame.key.get_pressed` in the event loop.
if (event.type == pygame.QUIT
or event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
main_loop = False
pos = pygame.mouse.get_pos()
angle = math.degrees(calculate_angle(pos))
screen.fill(GRAY)
# Now just rotate the original surface and get a new rect.
rotated_surface = pygame.transform.rotate(surface, -angle)
rect = rotated_surface.get_rect(center=(screen_rect.center))
screen.blit(rotated_surface, rect)
pygame.display.update()
CLOCK.tick(30)
pygame.quit()
我還嘗試改進(jìn)了一些其他內(nèi)容(查看評(píng)論)。
添加回答
舉報(bào)