PyGame精灵随机移动

1 投票
1 回答
4814 浏览
提问于 2025-04-18 03:24

基本上,我想让我的敌人角色在地图上随机移动。我已经实现了一些功能,不过现在的随机移动只在他们碰到墙壁的时候才会发生,这样看起来更自然。有没有更好的方法呢?或者我应该调整我的地图,让角色更频繁地碰到墙壁吗?

def update(self, walls, player):
    # Check if moving left / right caused a collision
    self.rect.x += self.x_speed
    walls_hit = pygame.sprite.spritecollide(self, walls, False)
    for wall in walls_hit:
        if self.x_speed > 0:
            self.rect.right = wall.rect.left
            tmp = random.randint(0, 1)
            print(tmp)
            if tmp == 0:
                self.x_speed = 0
                self.y_speed = 3
            else:
                self.x_speed = 0
                self.y_speed = -3
        else:
            self.rect.left = wall.rect.right
            tmp = random.randint(0, 1)
            print(tmp)
            if tmp == 0:
                self.x_speed = 0
                self.y_speed = 3
            else:
                self.x_speed = 0
                self.y_speed = -3

    # Check if moving up / down caused a collision
    self.rect.y += self.y_speed
    walls_hit = pygame.sprite.spritecollide(self, walls, False)
    for wall in walls_hit:
        if self.y_speed > 0:
            self.rect.bottom = wall.rect.top
            tmp = random.randint(0, 1)
            print(tmp)
            if tmp == 0:
                self.y_speed = 0
                self.x_speed = 3
            else:
                self.y_speed = 0
                self.x_speed = -3
        else:
            self.rect.top = wall.rect.bottom
            tmp = random.randint(0, 1)
            print(tmp)
            if tmp == 0:
                self.y_speed = 0
                self.x_speed = 3
            else:
                self.y_speed = 0
                self.x_speed = -3

如果有帮助的话,下面的代码是我的地图(1代表墙壁,0代表地面)

GRID = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
        [1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1],
        [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] 

任何帮助都非常感谢!

1 个回答

1

这其实要看你想要什么样的随机移动方式。这里有很多选择:

  • 每“步”都随机移动(这样可能看起来有点突兀或不自然)。如果你想让代码更好,可以在速度上增加一些变化,比如让角色在每个方向上移动的幅度更大一点(目前看起来你的角色只能在正负3的范围内移动)。

举个例子:

self.x_speed = 6*random.random()-3
# this will set the x_speed to a float between -3 and 3

self.y_speed += random.random() - .5
# this will increment y_speed by a number in between .5 and -.5
# this is a more gradual speed change and a way to make it look more natural
  • 我个人很喜欢你现在的实现方式,就是在碰到墙壁时改变速度。我觉得无论你选择什么样的随机移动方式,角色在碰到墙壁时都应该改变方向。

  • 每x步随机移动(这样看起来比每步都随机移动更自然)。

举个例子:

# within your function, you can see the number of steps
step = 0
if step >= 5: #every 5 steps
    #set random movement of whatever type you want
    self.x_speed = 6*random.random()-3
    self.y_speed = 6*random.random()-3
    step = 0
step += 1

你还可以把step的阈值设成一个随机数,比如说如果你想让角色每5到10步随机改变方向(除了碰到墙壁时),你可以把上面的if语句改成这样:

threshold = random.randrange(5,11)
step = 0
if step >= threshold: #every 5 steps
    #set random movement of whatever type you want
    self.x_speed = 6*random.random()-3
    self.y_speed = 6*random.random()-3
    step = 0
    threshold = random.randrange(5,11)
step += 1

祝你的游戏好运!

撰写回答