移动精灵在pygame中随时间点击

问题描述 投票:1回答:1

我正在尝试使用代码,我可以点击一个精灵,点击一个位置,它会在那里移动一段时间。

这是我的代码:

def update(self, mousePosX, mousePosY, mouseDown):
    #Walk
    self.walkCool += 1

    self.rect.x += self.xIncrement
    self.rect.y += self.yIncrement

    if self.rect.x == self.targetX:
            self.yIncrement = 0
            self.xIncrement = 0

    if self.selected == True:

        pg.draw.line(screen, (18, 219, 255), (self.rect.x + self.size//2, self.rect.y + self.size//2) , (mousePosX, mousePosY), 3)


        if mouseDown[0] and self.walkCool > 20:  

            self.targetX = mousePosX
            self.targetY = mousePosY
            self.dy = self.targetY - self.rect.y
            self.dx = self.targetX - self.rect.x
            print(self.dx,self.dy)

            self.yIncrement = self.dy/self.dx
            if self.dy > 0 and self.dx < 0:
                self.yIncrement = self.yIncrement * -1
            if self.dx < 0:
                self.xIncrement = -1
            elif self.dx > 0:
                self.xIncrement = 1
            else:
                self.xIncrement = 0

上面的代码用于我的sprite类中的更新函数(每次更新循环时调用它)。我的问题是,如果我得到一个dx / dy <1那么它似乎没有移动到确切的点击位置,我也无法让它移动到( - ,+)象限。不知道如何解决这些问题。

python pygame
1个回答
0
投票

首先使用vectors让您的生活更轻松。当用户单击鼠标按钮时设置精灵的目标。计算目标的矢量(这里称为heading),归一化并将其缩放到所需的速度,然后将其用作速度矢量。当精灵接近目标时,你需要减速,否则它会快速来回移动。

import pygame as pg
from pygame.math import Vector2


class Player(pg.sprite.Sprite):

    def __init__(self, pos, *groups):
        super().__init__(*groups)
        self.image = pg.Surface((30, 50))
        self.image.fill(pg.Color('dodgerblue1'))
        self.rect = self.image.get_rect(center=pos)
        self.vel = Vector2(0, 0)
        # Store the actual position as another vector because
        # rect coordinates can only be integers.
        self.pos = Vector2(pos)
        self.max_speed = 5
        self.goal = Vector2(pos)
        self.goal_radius = 40

    def update(self):
        self.pos += self.vel  # Update the position vector first.
        self.rect.center = self.pos  # Update the rect afterwards.

        # This vector points to the goal.
        heading = self.goal - self.pos
        distance = heading.length()
        # Normalize it, so that we can scale it to the desired length/speed below.
        if heading:  # Can't normalize a zero vector.
            heading.normalize_ip()

        if distance > self.goal_radius:
            # Move with maximum speed.
            self.vel = heading * self.max_speed
        else:
            # Slow down when we're approaching the goal.
            self.vel = heading * (distance/self.goal_radius * self.max_speed)


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

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.MOUSEBUTTONDOWN:
                # Set the goal of the player.
                player.goal = Vector2(event.pos)

        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()
© www.soinside.com 2019 - 2024. All rights reserved.