为什么我的pygame精灵的左走比右走快?

时间:2019-06-27 23:50:44

标签: python python-3.x pygame

我有一个精灵,代表我在pygame中的游戏玩家,但他们向左移动的速度都比向右移动的速度快。

我代码的重要部分:

class Player(pygame.sprite.Sprite):
    def __init__(self,
                 name,
                 width,
                 height,
                 image=None,
                 keys=dict(left=pygame.K_a, right=pygame.K_d, up=pygame.K_w, down=pygame.K_s),
                 jump_height=2.55,
                 move_speed=1.5,
                 pos=(0, 0)):
        super().__init__()
        self.name = name
        if image is None:
            self.rect = pygame.Rect(pos, (width, height))
        else:
            self.rect = pygame.Rect(pos, (width, height))
            self.image = pygame.Surface((width, height))
            self.image.fill(image)
        self.keys = keys
        self.width = width
        self.height = height
        self.pos = pos
        self.rect.topleft = pos
        self.VX = 0
        self.VY = 0
        self.jump_height = jump_height
        self.move_speed = move_speed
        self.haveJumped = False
        self.wins = 0

    def move(self):
        self.VX = 0.0
        pressed = pygame.key.get_pressed()
        if pressed[self.keys["left"]]:
            self.VX = -self.move_speed
            if self.rect.left > 0:
                self.rect.left += self.VX

        if pressed[self.keys["right"]]:
            self.VX = self.move_speed
            if self.rect.right < windowWidth:
                self.rect.right += self.VX


请问有人能告诉我为什么我的玩家向左移动得更快以及如何解决吗?

1 个答案:

答案 0 :(得分:5)

按照Rect docs

  

Rect对象的坐标都是整数。

Pygame的Rect用于处理像素,而您的move_speed1.5,所以

if pressed[self.keys["left"]]:
    self.VX = -self.move_speed
    if self.rect.left > 0:
        self.rect.left += self.VX

将您的矩形2移到左侧,然后

if pressed[self.keys["right"]]:
    self.VX = self.move_speed
    if self.rect.right < windowWidth:
        self.rect.right += self.VX

将您的矩形1移动到右侧。

您应该在类中使用其他变量来存储对象的位置。您可以使用已经存在的pos属性:

class Player(pygame.sprite.Sprite):
    def __init__(self,
                 name,
                 width,
                 height,
                 image=None,
                 keys=dict(left=pygame.K_a, right=pygame.K_d, up=pygame.K_w, down=pygame.K_s),
                 jump_height=2.55,
                 move_speed=1.5,
                 pos=(0, 0)):
        super().__init__()
        self.name = name
        if image is None:
            self.rect = pygame.Rect(pos, (width, height))
        else:
            self.rect = pygame.Rect(pos, (width, height))
            self.image = pygame.Surface((width, height))
            self.image.fill(image)
        self.keys = keys
        self.width = width
        self.height = height
        self.pos = pos
        self.rect.topleft = pos
        self.VX = 0
        self.VY = 0
        self.jump_height = jump_height
        self.move_speed = move_speed
        self.haveJumped = False
        self.wins = 0

    def move(self):
        self.VX = 0.0
        pressed = pygame.key.get_pressed()
        if pressed[self.keys["left"]]:
            self.VX = -self.move_speed
            if self.pos[0] > 0:
                self.pos[0] += self.VX

        if pressed[self.keys["right"]]:
            self.VX = self.move_speed
            if (self.pos[0] + self.width) < windowWidth:
                self.pos[0] += self.VX

但是请注意,您需要相应地更改渲染代码。