围绕迷宫的敌人运动pygame

时间:2017-11-20 14:13:50

标签: python pygame

我在python中编程很新,我决定创建一个python的基本版本的游戏。我在使用A.I时遇到了麻烦,当你运行代码时,红色区块(敌人)会精神错乱,我试图让它追逐玩家。除了我试图让另一个敌人跟随迷宫周围的随机路径,但在我开始发展白发后放弃了。

def move_to_player(self, Player):
    if Player.rect.x > enemy.rect.x:
        enemy.changespeed(1,0)

    elif Player.rect.x < enemy.rect.x:
        enemy.changespeed(-1,0)

    elif Player.rect.y > enemy.rect.y:
        enemy.changespeed(0,1)

    elif Player.rect.y < enemy.rect.y:
        enemy.changespeed(0,-1)

如果有帮助的话,这是正在使用的两个类。任何帮助都将非常感激。

class Player(pygame.sprite.Sprite):


    #must have constructor line, will always be called
    def __init__(self, x, y):
        # Call the parent's constructor
        pygame.sprite.Sprite.__init__(self)

        # Set dimensions of player
        self.image = pygame.Surface([25, 25])
        self.image.fill(white) # fill enitre image with "white"

        # The passed in co-ordinates are top left corner where x and y start
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

        # Speed of player
        self.x_Change = 0
        self.y_Change = 0

    def changespeed(self, x, y):
        #change speed of player
        self.x_Change += x
        self.y_Change += y   

    def update(self):

        # Move along x axis
        self.rect.x += self.x_Change

        # Did player hit a wall
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)  # false so it doesn't remove the wall, true would
        for block in block_hit_list:
            # If moving right, place player to the left side of wall

            if self.x_Change > 0:
                self.rect.right = block.rect.left
            else:
                #  if  moving left, do the opposite.
                self.rect.left = block.rect.right

        # Move along y axis
        self.rect.y += self.y_Change

        # Did player hit a wall
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:

            # Do same as above but on the y axis
            if self.y_Change > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom



class Enemy(pygame.sprite.Sprite):


    #must have constructor line, will always be called
    def __init__(self, x, y):
        # Call the parent's constructor
        pygame.sprite.Sprite.__init__(self)

        # Set dimensions of enemy
        self.image = pygame.Surface([25, 25])
        self.image.fill(red) # fill enitre image with "white"

        # The passed in co-ordinates are top left corner where x and y start
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

        # Speed of player
        self.x_Change = 0
        self.y_Change = 0

    def changespeed(self, x, y):
        #change speed of player
        self.x_Change += x
        self.y_Change += y 


    def move_to_player(self, Player):
        if Player.rect.x > enemy.rect.x:
            enemy.changespeed(1,0)

        elif Player.rect.x < enemy.rect.x:
            enemy.changespeed(-1,0)

        elif Player.rect.y > enemy.rect.y:
            enemy.changespeed(0,1)

        elif Player.rect.y < enemy.rect.y:
            enemy.changespeed(0,-1)




    def update(self):

        # Move along x axis
        self.rect.x += self.x_Change

        # Did player hit a wall
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)  # false so it doesn't remove the wall, true would
        for block in block_hit_list:
            # If moving right, place player to the left side of wall

            if self.x_Change > 0:
                self.rect.right = block.rect.left
            else:
                #  if  moving left, do the opposite.
                self.rect.left = block.rect.right

        # Move along y axis
        self.rect.y += self.y_Change

        # Did player hit a wall
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:

            # Do same as above but on the y axis
            if self.y_Change > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

1 个答案:

答案 0 :(得分:0)

def move_to_player(self, Player):
    if Player.rect.x > enemy.rect.x:
        enemy.changespeed(1,0)

    elif Player.rect.x < enemy.rect.x:
        enemy.changespeed(-1,0)

    elif Player.rect.y > enemy.rect.y:
        enemy.changespeed(0,1)

    elif Player.rect.y < enemy.rect.y:
        enemy.changespeed(0,-1)

上面的代码是一种逻辑,而不是您已经编写了一组条件以使其以特定方式遵循的AI。

如果您想使用AI,则可能需要研究A-star寻路。这就是当今大多数游戏中所使用的。

此外,关于红色方块的想法。可能是因为您没有正确设置每秒的帧数,所以屏幕获得的更新太多地改变了敌人的动作。