Im trying to make a simulation.
My rabbit's are supposed to be moving around randomly, but i would like them to keep moving the same direction for longer, hence, why i use the if randomnum == 1, to only change the direction 1/10 times.
But when i run the code the rabbits seem to change direction every move. What am i doing wrong?
rabbit-constructor
class Rabbit(pygame.sprite.Sprite): def init(self,x,y): super().init()
self.image = pygame.Surface([10,10])
self.image.fill(BLUE)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.xchange = 0
self.ychange = 0
self.hp = 100
rabbit-mover
for rabbit in rabbits:
randomnum = random.randint(1,10)
if randomnum == 1:
xdir = random.randint(-5,5)
ydir = random.randint(-5,5)
rabbit.xchange = xdir
rabbit.ychange = ydir
else:
continue
if rabbit.rect.x+xdir > 10 and rabbit.rect.x+xdir < 390 and rabbit.rect.y+ydir > 10 and rabbit.rect.y+ydir < 390:
rabbit.rect.x += rabbit.xchange
rabbit.rect.y += rabbit.ychange
else:
continue
Aucun commentaire:
Enregistrer un commentaire