簡體   English   中英

PYTHON和PYGAME如何向鼠標位置移動和旋轉多邊形?

[英]PYTHON & PYGAME How to move and rotate polygon towards the mouse position?

編輯:旋轉對我來說不再重要了,只是運動。我仍然很好奇,很想知道如何進行旋轉。)

def angle_between(p1, p2):
    ang1 = np.arctan2(*p1[::-1])
    ang2 = np.arctan2(*p2[::-1])
    return (ang1 - ang2) % (2 * np.pi)  



class Minion:  # Shape: Triangle
        def __init__(self):
            self.rotation = 0
            self.position = (200,200)
            self.radius = 50
            self.vertices = ((1,1), (10,10), (1,10))

        def determine_vertices(self):
            x = self.position[0]
            y = self.position[1]
            target_pos = pygame.mouse.get_pos()
            x2 = target_pos[0]
            y2 = target_pos[1]
            # Will start off pointing to the right (pre-rotation)
            vertex_A = (x + self.radius, y)
            vertex_B = (x + (cos(2*pi/3))*self.radius, y + (sin(2*pi/3))*self.radius)
            vertex_C = (x + (cos(4*pi/3))*self.radius, y + (sin(4*pi/3))*self.radius)
            self.vertices = (vertex_A,vertex_B,vertex_C)  # NOT YET ROTATED
            # Now let's find the angle between my location and the target location
            self.rotation = angle_between((x,y),(x2,y2))
            # Here is where I am stuck

好的,所以在旋轉頂點之前,我已經找到了它們的位置,並且還找到了要將其旋轉到的角度(以弧度為單位)。 這是我看過的鏈接:

結果我不是要找的

不了解/不知道如何轉換為我的代碼

用圖形數據代替原始數據

編輯:我已將頂點公式更改為旋轉,但它們旋轉的方向和方向都太快。 我不知道為什么。 這是代碼:

vertex_A = (x + (cos(self.rotation))*self.radius, y + (sin(self.rotation))*self.radius)
vertex_B = (x + (cos((2*pi/3) + self.rotation))*self.radius, y + (sin((2*pi/3) + self.rotation))*self.radius)
vertex_C = (x + (cos((4*pi/3) + self.rotation))*self.radius, y + (sin((4*pi/3) + self.rotation))*self.radius)

要將對象移向鼠標,可以使用矢量。 只需從鼠標pos減去位置,對結果向量進行歸一化,並以所需速度將其復數即可。 這樣就可以為您提供速度矢量,您可以在每幀self.pos其添加到self.pos (還可以更新矩形,用作矩形的位置並用於碰撞檢測)。

調用Vector2.as_polar方法(它返回極坐標 )以獲取矢量的角度,然后使用它旋轉原始圖像。

import pygame as pg
from pygame.math import Vector2


class Entity(pg.sprite.Sprite):

    def __init__(self, pos, *groups):
        super().__init__(*groups)
        self.image = pg.Surface((50, 30), pg.SRCALPHA)  # A transparent image.
        # Draw a triangle onto the image.
        pg.draw.polygon(self.image, pg.Color('dodgerblue2'),
                        ((0, 0), (50, 15), (0, 30)))
        # A reference to the original image to preserve the quality.
        self.orig_image = self.image
        self.rect = self.image.get_rect(center=pos)
        self.vel = Vector2(0, 0)
        self.pos = Vector2(pos)

    def update(self):
        # Subtract the pos vector from the mouse pos to get the heading,
        # normalize this vector and multiply by the desired speed.
        self.vel = (pg.mouse.get_pos() - self.pos).normalize() * 5

        # Update the position vector and the rect.
        self.pos += self.vel
        self.rect.center = self.pos

        # Rotate the image.
        # `Vector2.as_polar` returns the polar coordinates (radius and angle).
        radius, angle = self.vel.as_polar()
        self.image = pg.transform.rotozoom(self.orig_image, -angle, 1)
        self.rect = self.image.get_rect(center=self.rect.center)


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    all_sprites = pg.sprite.Group()
    entity = Entity((100, 300), all_sprites)

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True

        all_sprites.update()
        screen.fill((30, 30, 30))
        all_sprites.draw(screen)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM