繁体   English   中英

PyGame Sprite随机运动

[英]PyGame Sprite Random Movement

基本上,我想让我的敌人角色随机在地图上移动。 我已经实现了这一点,但是随机运动仅在它们碰到墙时才会发生(使它看起来更自然)。 有更好的方法吗? 还是应该四处移动地图,以使角色经常碰到墙?

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

任何帮助,将不胜感激!

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

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

祝您比赛顺利!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM