Pygame - Python - 360-градусный угол на кубе, ориентация курсора / указателя / мыши - PullRequest
0 голосов
/ 15 декабря 2018

Я пытаюсь решить эту проблему с углом, я заметил, что в координатах два 0,0, может быть именно это мешает кубу совершить поворот на 360 градусов, следует видео и коду.

Может кто-нибудь мне помочь?

видео здесь

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()

1 Ответ

0 голосов
/ 15 декабря 2018

Вы можете просто вычесть позицию 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запускается и передает нужные координаты центра.Используйте 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()
...