PYTHON& PYGAME如何向鼠标位置移动和旋转多边形? [英] PYTHON & PYGAME How to move and rotate polygon towards the mouse position?

查看:472
本文介绍了PYTHON& PYGAME如何向鼠标位置移动和旋转多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

( 旋转对我而言已不再重要,仅是运动.我仍然很好奇,但是很想知道如何进行轮换.)

( The rotation doesn't matter too much to me anymore - just the movement. I am still curious and would love to know how to do the rotation however.)

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

好的,因此在旋转顶点之前,我已经找到了它们的位置,并且还找到了要将其旋转到的角度(以弧度为单位).这是我看过的链接:

Okay, so I have found the location of my vertices before they are rotated, and have also found the angle (in radians) in which I want to rotate it towards. Here are the links I've looked at:

结果不是我想要的

不了解/不知道如何转换为我的代码

图形数据而不是原始数据

我将顶点公式更改为旋转,但是它们旋转的方向错误,并且旋转的速度太快.我不知道为什么.这是代码:

I have changed my vertex formulas to rotate but they rotate the wrong direction and way too fast. I have no clue why. Here is the code:

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中(还可以更新用作blit位置并用于碰撞检测的rect).

To move an object towards the mouse, you can use vectors. Just subtract the position from the mouse pos, normalize the resulting vector and mutliply it by the desired speed. That gives you the velocity vector which you can add to the self.pos each frame (also update the rect which serves as the blit position and for collision detection).

调用Vector2.as_polar方法(它返回极坐标)以获取角度向量,然后用它来旋转原始图像.

Call the Vector2.as_polar method (it returns the polar coordinates) to get the angle of the vector and then use it to rotate the original image.

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

这篇关于PYTHON& PYGAME如何向鼠标位置移动和旋转多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆