user3515480
user3515480

Reputation: 11

PyGame Sprite Random Movement

basically I'm trying to make my enemy character move around the map randomly. I have sort of implemented this however the random movement only happens when they hit a wall (makes it look more natural). Is there a better way of doing this? Or should I move my map around so that characters hit the wall more often?

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

If it helps the code below is my map (1's = walls, 0's = floor)

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

Any help would be appreciated!

Upvotes: 1

Views: 4917

Answers (1)

Luigi
Luigi

Reputation: 4129

It really depends on the type of the random movement you want. There are many options:

  • Random movement every "step" (can look jerky/unnatural). One way to change your code specifically would be to have more variation/gradual incrementation in speed (it looks like your character can only move +/- 3) in every direction)

example:

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
  • I personally am a fan of your current implementation, changing speed when you hit walls. I think whatever random movement you choose, your character should change direction when you hit a wall in addition.

  • Random movement every x steps (this looks more natural than random movement every step)

example:

# 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

You can also make the step threshold a random number, like if you want to randomly change direction every 5-10 steps (in addition to when you hit a wall) you could change the if statement above to something like this:

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

Good luck with your game!

Upvotes: 1

Related Questions