如何让敌人与方块接触后随机改变方向

问题描述 投票:2回答:1

[基本上,我的目标是重新创建一个炸弹人自上而下的游戏,我希望敌人向四个方向(上,下,左,右)移动。我也希望他们在触摸墙壁时随机改变方向。我正在尝试在pygame上创建它,我想在敌人的类中做到这一点,但这不起作用

class enemy(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)        
        self.image = pygame.image.load(os.path.join('player.png')).convert()        
        self.rect = pygame.Rect(32, 32, 32, 32) #spawnx,spawny,width,height
        directions = [(-2,0),(0,-2),(2,0),(0,2)]
        godirection = random.choice(directions)
        print(godirection)

    def move(self, x, y):
        if x != 0:
            self.movedirection(x, 0)
        if y != 0:
            self.movedirection(0, y)

    def movedirection(self, x, y):
        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
                    changedirection = True
                if x < 0:       # Moving left Hit the right side of the wall
                    self.rect.left = block.rect.right
                    changedirection = True
                if y > 0:       # Moving down Hit the top side of the wall
                    self.rect.bottom = block.rect.top
                    changedirection = True
                if y < 0:       # Moving up Hit the bottom side of the wall
                    self.rect.top = block.rect.bottom
                    changedirection = True


    def  movmethod(self,godirection,changedirection):
        while True:
            move(godirection)
            if changedirection == True:
                godirection=random.choice(directions)
                changedirection = False

当前有一个主程序while循环,但是我正在努力使movmethod工作,如果您需要更多代码或完整程序来理解此内容,请告诉我谢谢

python pygame collision
1个回答
2
投票

它确实希望将方向更改为非冲突方式。 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

显然,运动逻辑需要更改以合并返回结果:

def move(self, x, y):
    return self.movedirection( x, y )

def movmethod(self,godirection,changedirection):
    while True:
        godirection = move( godirection )

但是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 )

在主循环中调用update()

© www.soinside.com 2019 - 2024. All rights reserved.