PyGame精灵随机运动

时间:2014-04-19 17:28:48

标签: python pygame

基本上我试图让我的敌人角色随机地在地图上移动。我有点实现了这个,但随机运动只发生在他们撞墙时(让它看起来更自然)。有没有更好的方法呢?或者我应该移动我的地图,以便角色更频繁地撞到墙上?

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' s =墙,0' s =楼层)

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 个答案:

答案 0 :(得分:0)

这实际上取决于你想要的随机运动的类型。有很多选择:

  • 每个随机移动"步骤" (可以看起来生涩/不自然)。特别是改变你的代码的一种方法是在每个方向上有更多变化/逐渐增加的速度(看起来你的角色只能移动+/- 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

祝你好运!