简体   繁体   English

Pygame简单动作:模仿玩家动作并追踪步骤的敌人

[英]Pygame Simple Movement: Enemy mimicking player movement and also retracing steps

I want to use the movement method of my player class and have the enemy copy player movements. 我想使用玩家类的移动方法,并让敌人复制玩家的移动。 I want the enemy to basically follow the player until a certain condition is met for example 3 minutes have passed, after which I would like the enemy to retrace all its steps. 我希望敌人基本上跟随玩家,直到满足特定条件(例如3分钟)为止,之后我希望敌人回撤其所有步骤。 Below is my movement method in the player's class 以下是我在球员课堂上的移动方法

class PacMan(pygame.sprite.Sprite):
    def __init__(self, x, y, radius):
        super().__init__()
        self.image = pygame.Surface([radius, radius])
        self.image.fill([250, 250, 0])
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.queue = [-speed1, 0]
        self.direction = [-speed1, 0]

    def change_direction(self, x, y):
        self.queue = [x, y]

    def move(self, level):
        self.rect.x += self.queue[0]
        self.rect.y += self.queue[1]
        hitlist = pygame.sprite.spritecollide(self, level.wall_list, False)
        if hitlist:
            self.rect.x -= self.queue[0]
            self.rect.y -= self.queue[1]
            self.rect.x += self.direction[0]
            self.rect.y += self.direction[1]
            seclist = pygame.sprite.spritecollide(self, level.wall_list, False)
            if seclist:
                self.rect.x -= self.direction[0]
                self.rect.y -= self.direction[1]
        else:
            self.direction[0] = self.queue[0]
            self.direction[1] = self.queue[1]

        if self.rect.x < -pacman_length:
            self.rect.x = board_dimensions[0] - wall_length
            self.rect.y = self.rect.y
        if self.rect.x > board_dimensions[0]:
            self.rect.x = wall_length

Next the enemy class which won't function apart from going to the right 4 pixels at a time 下一个敌人类别,除了一次向右移动4个像素外不会起作用

class Ghost(pygame.sprite.Sprite):
    def __init__(self, x, y, color,):
        super().__init__()
        self.color = color
        self.image = pygame.Surface([pacman_length, pacman_length])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.direction = [0, speed1]
        self.moves = [False, True, True, False]
        self.edible = False
        self.wait = False
        self.waitcount = 0
        self.speed = speed1

    def move(self, level):
        status1 = False
        status2 = True



        self.rect.move_ip(4, 0)
        x_change = 0
        y_change = 0

        for i in startingmovements:
            print (i)
            print(i[0])
            print (i[1])
    def change_color(self, color):
        self.image.fill(color)

I use the while loop to actually make the characters move like so: 我使用while循环实际上使字符移动如下:

        player.move(board)

        for i in board.ghost_list:
            i.move(board)

and

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            return
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.change_direction(-4, 0)
            elif event.key == pygame.K_RIGHT:
                player.change_direction(4, 0)
            elif event.key == pygame.K_DOWN:
                player.change_direction(0, 4)
            elif event.key == pygame.K_UP:
                player.change_direction(0, -4)
            elif event.key == pygame.K_SPACE:

I understand that the code has an easy solution however I'm just having one of those times where I can't seem to grasp the answer. 我知道该代码有一个简单的解决方案,但是我遇到的那一次我似乎无法把握答案。 Any help given would really be appreciated 给予的任何帮助将不胜感激

A solution would be the putting a list as an attribute to your pac-man class. 一种解决方案是将列表作为pac-man类的属性。 For example, self.steps=[] . 例如, self.steps=[] The pac-man would list his coordinates so that the ghosts could track him. 吃豆人会列出他的坐标,以便鬼可以追踪他。

Implemented into your code: 实施到您的代码中:

class PacMan(pygame.sprite.Sprite):
def __init__(self, x, y, radius):
    super().__init__()
    self.image = pygame.Surface([radius, radius])
    self.image.fill([250, 250, 0])
    self.rect = self.image.get_rect()
    self.rect.x = x
    self.rect.y = y
    self.queue = [-speed1, 0]
    self.direction = [-speed1, 0]
    #add some starting values
    self.steps=[]

Now let's add a function to have pac-man list his steps. 现在,让我们添加一个函数,让pac-man列出他的步骤。

def update_steps():
    #add current position
    self.steps.insert(0,(self.rect.x,self.rect.y))
    #delete old steps
    del self.steps[:-1]

Now for the ghost function: 现在为鬼功能:

def update_pos(steps):
    self.rect.x=steps[:-1[1]]
    self.rect.y=steps[:-1[2]]

Also, make sure to update the ghost instead of the pac-man first. 另外,请确保先更新虚影而不是pac-man。

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM