Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使敌方在接触拦网后随机改变方向_Python_Pygame_Collision - Fatal编程技术网

Python 如何使敌方在接触拦网后随机改变方向

Python 如何使敌方在接触拦网后随机改变方向,python,pygame,collision,Python,Pygame,Collision,哎呀,我想删除它,但它不允许我,对不起,它真的想把方向改成非冲突的方式。movedirection()知道它的行进方向,因此可以从选项中删除该方向 def movedirection(self, x, y): next_movement = ( x, y ) # no change self.rect.x = self.rect.x + x self.rect.y = self.rect.y + y # If you collide with a wall,

哎呀,我想删除它,但它不允许我,对不起,

它真的想把方向改成非冲突的方式。
movedirection()
知道它的行进方向,因此可以从选项中删除该方向

def movedirection(self, x, y):
    next_movement = ( x, y )  # no change
    self.rect.x = self.rect.x + x
    self.rect.y = self.rect.y + y

    # If you collide with a wall, move out based on velocity
    for block in block_list:
        if self.rect.colliderect(block.rect):
            if x > 0:       # Moving right Hit the left side of the wall
                self.rect.right = block.rect.left
                directions = [(-2,0),(0,-2),(0,2)]  # L, D, U
            elif x < 0:       # Moving left Hit the right side of the wall
                self.rect.left = block.rect.right
                directions = [( 2,0),(0,-2),(0,2)]  # R, D, U
            if y > 0:       # Moving down Hit the top side of the wall
                self.rect.bottom = block.rect.top
                directions = [(-2,0),(2,0),(0,-2)]  # L, R, D
            elif y < 0:       # Moving up Hit the bottom side of the wall
                self.rect.top = block.rect.bottom
                directions = [(-2,0),(2,0),(0,2)]   # L, R, U     

            # pick a new random direction that does not re-collide
            next_movement = random.choice( directions )

    # The enemy either continues, or turned
    return next_movement
但是
movmethod()
仍然有一个讨厌的无限循环(它永远不会返回)。我将使用
update()
函数,按照PyGame Sprite类通常处理这个问题的方式,对运动进行建模。这将处理移动、位图更改等

def update( self ):
    x, y = self.godirection  # NOTE: now a member variable
    self.rect.x = self.rect.x + x
    self.rect.y = self.rect.y + y

    # If you collide with a wall, move out based on velocity
    for block in block_list:
        if self.rect.colliderect( block.rect ):
            if x > 0:       # Moving right Hit the left side of the wall
                self.rect.right = block.rect.left
                directions = [(-2,0),(0,-2),(0,2)]  # L, D, U
            elif x < 0:       # Moving left Hit the right side of the wall
                self.rect.left = block.rect.right
                directions = [( 2,0),(0,-2),(0,2)]  # R, D, U
            if y > 0:       # Moving down Hit the top side of the wall
                self.rect.bottom = block.rect.top
                directions = [(-2,0),(2,0),(0,-2)]  # L, R, D
            elif y < 0:       # Moving up Hit the bottom side of the wall
                self.rect.top = block.rect.bottom
                directions = [(-2,0),(2,0),(0,2)]   # L, R, U     

            # pick a new random direction that does not re-collide
            self.godirection = random.choice( directions )
def更新(自):
x、 y=self.godirection#注意:现在是一个成员变量
self.rect.x=self.rect.x+x
self.rect.y=self.rect.y+y
#如果与墙碰撞,请根据速度向外移动
对于块列表中的块:
如果self.rect.collide rect(block.rect):
如果x>0:#向右移动会击中墙的左侧
self.rect.right=block.rect.left
方向=[(-2,0)、(0,-2)、(0,2)]#L,D,U
elif x<0:#向左移动会击中墙的右侧
self.rect.left=block.rect.right
方向=[(2,0),(0,-2),(0,2)]#R,D,U
如果y>0:#向下移动时击中墙的顶部
self.rect.bottom=block.rect.top
方向=[(-2,0),(2,0),(0,-2)]#L,R,D
elif y<0:#向上移动时碰到墙的底部
self.rect.top=block.rect.bottom
方向=[(-2,0)、(2,0)、(0,2)]#L,R,U
#拾取不重新碰撞的新随机方向
self.godirection=random.choice(方向)

在主循环中调用
update()

您好,谢谢您的帮助,我尝试了这个方法,但是使用x,y=self.godirection无效,所以我尝试了另一种方法,但是问题是,当调用函数时,它将永久改变方向。我通过在函数@Kingsley的开头添加self direction=random来实现这一点
def update( self ):
    x, y = self.godirection  # NOTE: now a member variable
    self.rect.x = self.rect.x + x
    self.rect.y = self.rect.y + y

    # If you collide with a wall, move out based on velocity
    for block in block_list:
        if self.rect.colliderect( block.rect ):
            if x > 0:       # Moving right Hit the left side of the wall
                self.rect.right = block.rect.left
                directions = [(-2,0),(0,-2),(0,2)]  # L, D, U
            elif x < 0:       # Moving left Hit the right side of the wall
                self.rect.left = block.rect.right
                directions = [( 2,0),(0,-2),(0,2)]  # R, D, U
            if y > 0:       # Moving down Hit the top side of the wall
                self.rect.bottom = block.rect.top
                directions = [(-2,0),(2,0),(0,-2)]  # L, R, D
            elif y < 0:       # Moving up Hit the bottom side of the wall
                self.rect.top = block.rect.bottom
                directions = [(-2,0),(2,0),(0,2)]   # L, R, U     

            # pick a new random direction that does not re-collide
            self.godirection = random.choice( directions )