【问题标题】:PyGame Sprite Random MovementPyGame Sprite 随机运动
【发布时间】:2014-04-19 17:28:48
【问题描述】:

基本上我试图让我的敌人角色在地图上随机移动。我已经实现了这一点,但是随机运动仅在它们撞到墙上时发生(使其看起来更自然)。有没有更好的方法来做到这一点?还是应该移动我的地图,让角色更频繁地撞墙?

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]] 

任何帮助将不胜感激!

【问题讨论】:

    标签: python pygame


    【解决方案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
    

    祝你的游戏好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 2020-05-03
      • 2015-03-24
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多