动画与“获取区域”,鼠标位置和点击播放

2024-04-25 12:13:28 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

要知道鼠标是否在屏幕上,只需执行以下操作:

xMouse, yMouse

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

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

enter image description here

我怎么知道老鼠在D区?你知道吗


Tags: keyimageselfgamegetifwidthregion

热门问题