如何向鼠标旋转精灵并将其移动?

时间:2020-11-12 13:55:18

标签: python pygame position pygame-surface

在我的代码中,精灵的坐标似乎没有改变,这让我感到困惑。放置(200, 200)与放置(900,100000)相同。所以基本上我不能在指定位置协调精灵。你能帮忙吗?

将学分转给Ann Zen 但是我身上的精灵问题 Pygame sprite not turning accordingly to mouse

我的代码:

import pygame
from math import atan2, degrees
# tank = pygame.image.load('Sprite0.png')
wn = pygame.display.set_mode((400, 400))

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('Sprite0.png')
        self.x = 0
        self.y = 0
        self.rect = self.image.get_rect()



    def point_at(self, x, y):
        rotated_image = pygame.transform.rotate(self.image, degrees(atan2(x-self.rect.x, y-self.rect.y)))
        new_rect = rotated_image.get_rect(center=self.rect.center)
        wn.fill((255, 255, 255))
        wn.blit(rotated_image, new_rect.topleft)




player = Player()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEMOTION:
            player.point_at(*pygame.mouse.get_pos())

    # wn.blit(tank, Player)
    pygame.display.update()

2 个答案:

答案 0 :(得分:3)

请仔细阅读How to rotate an image(player) to the mouse direction?的答案。角度的计算

degrees(atan2(x-self.rect.x, y-self.rect.y))

是偶然的。之所以有效,是因为atan2(x, y) == atan2(-y, x)-pi/2

向量( x y )的角度为atan2(y, x)。由于y轴通常指向上方,因此y轴需要反转(-y),但在PyGame坐标系中,y轴指向下方。您的 Sprite 最有可能指向上方,并且您要计算:

angle = degrees(atan2(self.rect.y - y, x - self.rect.x)) - 90

分别

direction = pygame.math.Vector2(x, y) - self.rect.center
angle = direction.angle_to((0, -1))

另请参阅How to know the angle between two points?


Sprite 绘制在rect属性中存储的位置。您根本不需要xy属性。只需设置矩形(rect)的位置即可。
x y 参数添加到构造函数中:

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('Sprite0.png')
        self.rect = self.image.get_rect(center = (x, y))

添加方法move并使用pygame.Rect.move_ip更改 Sprite 的位置:

class Player(pygame.sprite.Sprite):
    # [...]

    def move(self, x, y):
        self.rect.move_ip(x, y)

要更改 Sprite 的位置时,调用move

keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
    player.move(0, -1)
if keys[pygame.K_s]:
    player.move(0, 1)
if keys[pygame.K_a]:
    player.move(-1, 0)
if keys[pygame.K_d]:
    player.move(1, 0)

分别

keys = pygame.key.get_pressed()
player.move(keys[pygame.K_d]-keys[pygame.K_a], keys[pygame.K_s]-keys[pygame.K_w])

Sprite 应该始终包含在pygame.sprite.Group中。参见pygame.sprite.Group.draw()

将包含的Sprite绘制到Surface参数。这会将Sprite.image属性用于源曲面,并将Sprite.rect用于位置。

添加一个 Group 并将 Sprite 添加到 Group

player = Player(200, 200)
all_sprites = pygame.sprite.Group(player)

要绘制中的所有精灵时,调用draw

all_sprites.draw(wn)

确保旋转后的图像存储在image属性中:

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.original_image = pygame.image.load('Sprite0.png')
        self.image = self.original_image
        self.rect = self.image.get_rect(center = (x, y))

    def point_at(self, x, y):
        direction = pygame.math.Vector2(x, y) - self.rect.center
        angle = direction.angle_to((0, -1))
        self.image = pygame.transform.rotate(self.original_image, angle)
        self.rect = self.image.get_rect(center=self.rect.center)

    # [...]

最小示例: repl.it/@Rabbid76/PyGame-SpriteRotateToMouse

import pygame
wn = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.original_image = pygame.image.load('Sprite0.png')
        self.image = self.original_image
        self.rect = self.image.get_rect(center = (x, y))
        self.velocity = 5

    def point_at(self, x, y):
        direction = pygame.math.Vector2(x, y) - self.rect.center
        angle = direction.angle_to((0, -1))
        self.image = pygame.transform.rotate(self.original_image, angle)
        self.rect = self.image.get_rect(center=self.rect.center)

    def move(self, x, y):
        self.rect.move_ip(x * self.velocity, y * self.velocity)


player = Player(200, 200)
all_sprites = pygame.sprite.Group(player)

while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    player.point_at(*pygame.mouse.get_pos())
    keys = pygame.key.get_pressed()
    player.move(keys[pygame.K_d]-keys[pygame.K_a], keys[pygame.K_s]-keys[pygame.K_w])

    wn.fill((255, 255, 255))
    all_sprites.draw(wn)
    pygame.display.update()

如果要沿鼠标方向移动对象,则必须添加类型pygame.math.Vecotr2directionposition属性。根据方向,在point_at中改变方向,在move中改变位置。 rect属性必须更新。


最小示例: repl.it/@Rabbid76/PyGame-SpriteFollowMouse

import pygame
wn = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.original_image = pygame.image.load('Sprite0.png')
        self.image = self.original_image
        self.rect = self.image.get_rect(center = (x, y))
        self.direction = pygame.math.Vector2((0, -1))
        self.velocity = 5
        self.position = pygame.math.Vector2(x, y)

    def point_at(self, x, y):
        self.direction = pygame.math.Vector2(x, y) - self.rect.center
        if self.direction.length() > 0:
            self.direction = self.direction.normalize()
        angle = self.direction.angle_to((0, -1))
        self.image = pygame.transform.rotate(self.original_image, angle)
        self.rect = self.image.get_rect(center=self.rect.center)

    def move(self, x, y):
        self.position -= self.direction * y * self.velocity
        self.position += pygame.math.Vector2(-self.direction.y, self.direction.x) * x * self.velocity
        self.rect.center = round(self.position.x), round(self.position.y)


player = Player(200, 200)
all_sprites = pygame.sprite.Group(player)

while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEMOTION:
            player.point_at(*event.pos)

    keys = pygame.key.get_pressed()
    player.move(keys[pygame.K_d]-keys[pygame.K_a], keys[pygame.K_s]-keys[pygame.K_w])

    wn.fill((255, 255, 255))
    all_sprites.draw(wn)
    pygame.display.update()

答案 1 :(得分:2)

删除elif event.type == pygame.MOUSEMOTION:块,然后将player.point_at(*pygame.mouse.get_pos())直接放入while循环中。

创建一个时钟,以使精灵不会从屏幕上滑出。

最后,添加

    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]:
        player.rect.y -= 1
    if keys[pygame.K_s]:
        player.rect.y += 1
    if keys[pygame.K_a]:
        player.rect.x -= 1
    if keys[pygame.K_d]:
        player.rect.x += 1

控制播放器。

示例:

import pygame
from math import atan2, degrees

wn = pygame.display.set_mode((400, 400))

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 40), pygame.SRCALPHA)
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect(topleft=(185, 180))

    def point_at(self, x, y):
        rotated_image = pygame.transform.rotate(self.image, degrees(atan2(x-self.rect.x, y-self.rect.y)))
        new_rect = rotated_image.get_rect(center=self.rect.center)
        wn.fill((0, 0, 0))
        wn.blit(rotated_image, new_rect.topleft)

player = Player()
clock = pygame.time.Clock() # Create the clock

while True:
    clock.tick(30) # Use the clock
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()

    keys = pygame.key.get_pressed() # Get al the pressed keys
    if keys[pygame.K_w]:
        player.rect.y -= 1
    if keys[pygame.K_s]:
        player.rect.y += 1
    if keys[pygame.K_a]:
        player.rect.x -= 1
    if keys[pygame.K_d]:
        player.rect.x += 1
        
    player.point_at(*pygame.mouse.get_pos()) # Put this here
    pygame.display.update()

输出:

enter image description here