使用" get_region",鼠标位置和点击播放器中的动画

时间:2018-01-30 21:06:27

标签: python pyglet

我有三个问题,希望你能帮我找到答案。

1)我终于设法在Pyglet上创建了一个Player类。但现在我有一个问题,我无法解决。这是班级:

class player(object):

    speed = 5
    walk = False
    walkPos = "down"

    def __init__(self, game, x, y):
        self.game = game
        self.groups = self.game.spritesPlayer

        self.image = pyglet.resource.image("character.png")
        self.image_runDown1 = self.image.get_region(x=0, y=144, width=48, height=48)
        self.image_standDown = self.image.get_region(x=48, y=144, width=48, height=48)
        self.image_runDown2 = self.image.get_region(x=144, y=144, width=48, height=48)
        self.image_runUp1 = self.image.get_region(x=0, y=0, width=48, height=48)
        self.image_standUp = self.image.get_region(x=48, y=0, width=48, height=48)
        self.image_runUp2 = self.image.get_region(x=144, y=0, width=48, height=48)
        self.image_runLeft1 = self.image.get_region(x=0, y=96, width=48, height=48)
        self.image_standLeft = self.image.get_region(x=48, y=96, width=48, height=48)
        self.image_runLeft2 = self.image.get_region(x=144, y=96, width=48, height=48)
        self.image_runRight1 = self.image.get_region(x=0, y=48, width=48, height=48)
        self.image_standRight = self.image.get_region(x=48, y=48, width=48, height=48)
        self.image_runRight2 = self.image.get_region(x=144, y=48, width=48, height=48)

        self.runDownList = [self.image_runDown1, self.image_standDown, self.image_runDown2, self.image_standDown]
        self.runDown = pyglet.image.Animation.from_image_sequence(self.runDownList, 0.3)
        self.runUpList = [self.image_runUp1, self.image_standUp, self.image_runUp2, self.image_standUp]
        self.runUp = pyglet.image.Animation.from_image_sequence(self.runUpList, 0.3)
        self.runLeftList = [self.image_runLeft1, self.image_standLeft, self.image_runLeft2, self.image_standLeft]
        self.runLeft = pyglet.image.Animation.from_image_sequence(self.runLeftList, 0.3)
        self.runRightList = [self.image_runRight1, self.image_standRight, self.image_runRight2, self.image_standRight]
        self.runRight = pyglet.image.Animation.from_image_sequence(self.runRightList, 0.3)

        self.standImage = self.image_standDown
        self.sprite = pyglet.sprite.Sprite(self.standImage)
        self.vx = x * self.game.tilesize
        self.vy = y * self.game.tilesize
        self.groups["player"] = self.sprite

        self.keyboard = key.KeyStateHandler()

    def _key_press(self):
        if self.keyboard[key.D]:
            self.vx += self.speed
            self.standImage = self.image_standRight
        if self.keyboard[key.A]:
            self.vx -= self.speed
            self.standImage = self.image_standLeft
        if self.keyboard[key.W]:
            self.vy += self.speed
            self.standImage = self.image_standUp
        if self.keyboard[key.S]:
            self.vy -= self.speed
            self.standImage = self.image_standDown
        if self.keyboard[key.F]:
            self.vx = self.game.mouse_x
            self.vy = self.game.mouse_y

    def _update(self):
        self.drawImage = self.standImage
        self._key_press()

        # Animation of the walk

        self.sprite = pyglet.sprite.Sprite(self.drawImage)
        self.sprite.x = self.vx
        self.sprite.y = self.vy
        self.groups["player"] = self.sprite

当我用以下内容插入动画时:

if self.walk:
    if self.walkPos == "down":
        self.drawImage = self.runDown

动画保持固定在第一帧,下次不会更新。所以没有显示动画。

2)在Player类中,用以下方法检测击键:key.KeyStateHandler()是否有与鼠标类似的东西?

3)enter image description here

要知道鼠标是否在A上,只需执行:

xMouse, yMouse

# A
if xMouse > (xPlayer + xPlayer.width/2) and yMouse < (yPlayer + xPlayer.height/2)

问题是我想在第二张图片中得到结果。

enter image description here

我如何知道鼠标是否在D?

1 个答案:

答案 0 :(得分:0)

想象一下,您将第二张图像旋转45°(逆时针),然后您将处于与第一张图像相同的状态。因此,您只需首先计算鼠标的旋转(M周围的M)坐标,然后使用您在图像1中使用的简单测试进行测试。