![](/img/trans.png)
[英]How put static initial sprite and after give a random movement on pygame?
[英]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]]
任何帮助,将不胜感激!
这实际上取决于您想要的随机运动的类型。 有很多选择:
例:
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.