我正在尝试解决这个角度问题,我注意到坐标中有两个0,0,也许是阻止立方体旋转360度的原因,它遵循视频和代码。

有人能帮我吗?

video here

import pygame
import sys
import os
import math

def main():
    pygame.init()

    clock = pygame.time.Clock()
    screen = pygame.display.set_mode([1000, 500])
    pygame.display.set_caption('Example 1')

    player = pygame.image.load(os.path.join('img', 'rect.png')).convert_alpha() #path to cube ./img/rect.png

    pygame.font.init()

    font = pygame.font.get_default_font()
    font_angle = pygame.font.SysFont(font, 44, True)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()


        clock.tick(60)
        screen.fill((255, 255, 255)) #screen background color

        player_rect = player.get_rect() #player rect (for center position)

        mouse_x, mouse_y = pygame.mouse.get_pos() #mouse position (x, y)

        #to define angle - start
        hypo = math.sqrt(math.pow(mouse_x - (player_rect[0] + player_rect.centerx), 2) +
                         math.pow(mouse_y - (player_rect[1] + player_rect.centery), 2))

        cos = (mouse_x - (player_rect[0] + player_rect.centerx)) / hypo
        sin = (mouse_y - (player_rect[1] + player_rect.centery)) / hypo

        angle = (180 / math.pi) * - math.atan2(sin, cos)
        #end

        newplayer = pygame.transform.rotate(player, angle) #rotate cube


        screen.blit(newplayer, [300, 100]) #show cube in screen

        text = font_angle.render(str("%.2f" % angle), 1, (255, 0, 0)) #show angle in mouse position

        screen.blit(text, ((mouse_x+20), mouse_y)) #show text

        pygame.display.update() #update frames

main()

最佳答案

您可以从player_rect.centerx位置减去mouse_x位置(与y相同)并将其传递给math.atan2

dist_x = mouse_x - player_rect.centerx
dist_y = mouse_y - player_rect.centery
angle = -math.degrees(math.atan2(dist_y, dist_x))


在while循环开始之前创建rect并传递所需的中心坐标。旋转图像后,使用get_rect创建新的矩形,并传递前一个矩形的中心坐标以使其居中。并在player_rect(即在其topleft坐标处)将图像变白。

import pygame
import sys
import os
import math


def main():
    pygame.init()
    clock = pygame.time.Clock()
    screen = pygame.display.set_mode([1000, 500])

    # player = pygame.image.load(os.path.join('img', 'rect.png')).convert_alpha() #path to cube ./img/rect.png
    player = pygame.Surface((50, 30), pygame.SRCALPHA)
    pygame.draw.polygon(player, (100, 200, 255), [(0, 0), (50, 15), (0, 30)])
    player_rect = player.get_rect(center=(500, 250)) # player rect (for center position)

    font = pygame.font.get_default_font()
    font_angle = pygame.font.SysFont(font, 44, True)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        clock.tick(60)
        screen.fill((255, 255, 255))
        mouse_x, mouse_y = pygame.mouse.get_pos()
        dist_x = mouse_x - player_rect.centerx
        dist_y = mouse_y - player_rect.centery
        angle = -math.degrees(math.atan2(dist_y, dist_x))

        newplayer = pygame.transform.rotate(player, angle)
        # Create a new rect and pass the center of the old rect.
        player_rect = newplayer.get_rect(center=player_rect.center)

        screen.blit(newplayer, player_rect) # Blit it at the player_rect.
        text = font_angle.render(str("%.2f" % angle), 1, (255, 0, 0))
        screen.blit(text, ((mouse_x+20), mouse_y))
        pygame.display.update()

main()

关于python - Pygame-Python-多维数据集上的360度角,光标/指针/鼠标方向,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53796277/

10-10 11:17