PYTHON&;PYGAME如何向鼠标位置移动和旋转多边形?
(编辑: 旋转对我来说已经不重要了——只是运动。我仍然很好奇,很想知道如何旋转。) 好的,我已经找到了顶点在旋转之前的位置,也找到了我想要旋转顶点的角度(弧度)。以下是我看过的链接: 编辑: 我已将顶点公式更改为旋转,但它们旋转方向错误,速度过快。我不知道为什么。代码如下:PYTHON&;PYGAME如何向鼠标位置移动和旋转多边形?,python,rotation,pygame,polygon,Python,Rotation,Pygame,Polygon,(编辑: 旋转对我来说已经不重要了——只是运动。我仍然很好奇,很想知道如何旋转。) 好的,我已经找到了顶点在旋转之前的位置,也找到了我想要旋转顶点的角度(弧度)。以下是我看过的链接: 编辑: 我已将顶点公式更改为旋转,但它们旋转方向错误,速度过快。我不知道为什么。代码如下: vertex_A = (x + (cos(self.rotation))*self.radius, y + (sin(self.rotation))*self.radius) vertex_B = (x + (cos(
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)
要将对象移向鼠标,可以使用向量。只需从鼠标位置中减去位置,将得到的矢量标准化,并以所需速度相互叠加即可。这将为您提供速度向量,您可以将其添加到每个帧的
self.pos
(还可以更新用作blit位置和碰撞检测的rect)
调用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()
你能更准确地描述一下你想要达到的目标和出了什么问题吗?我想要一个仆从(三角形)要指向鼠标并移动到它指向的地方@skrxCan你可以使用一个带有三角形的图像而不是顶点吗?是的,那很好@skrxI已经发布了一个关于pygame精灵的答案,该精灵在向量的帮助下向鼠标移动。如果您必须使用顶点,您可能应该检查变换矩阵是如何工作的(可能在上)。
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()