簡體   English   中英

Pygame 吃豆人 - 敵人隨機移動

[英]Pygame Pacman - Enemy Random Movement

我用 python 和 pygame 制作了一個吃豆人游戲,我有一個鬼魂在迷宮周圍隨機移動。 有誰知道我是否有辦法讓隨機敵人更受 AI 控制? 我仍然希望它是隨機的,但目前我擁有的隨機移動代碼使得幽靈有時會卡在幽靈開始的盒子里,或者它只會 go 從一個地方來回移動到另一個地方。

這是我擁有的隨機移動方法:

def get_random_direction(self):
            while True:
                number = random.randint(-2, 1)
                if number == -2:
                    x_dir, y_dir = 1, 0
                elif number == -1:
                    x_dir, y_dir = 0, 1
                elif number == 0:
                    x_dir, y_dir = -1, 0
                else:
                    x_dir, y_dir = 0, -1
                next_pos = vec(self.grid_pos.x + x_dir, self.grid_pos.y + y_dir)
                if next_pos not in self.app.walls:
                    break
            return vec(x_dir, y_dir)

我假設上面的代碼是我需要為要更改的隨機運動而編輯的部分,我只是不知道該怎么做才能更改它。

這是我的敵人的完整 class ,其中包含敵人使用的所有代碼:

        import pygame
    import random
    from settings import *

    vec = pygame.math.Vector2


    class Enemy:
        def __init__(self, app, pos, number):
            self.app = app
            self.grid_pos = pos
            self.starting_pos = [pos.x, pos.y]
            self.pix_pos = self.get_pix_pos()
            self.radius = int(self.app.cell_width//2.3)
            self.number = number
            self.colour = self.set_colour()
            self.direction = vec(0, 0)
            self.personality = self.set_personality()
            self.target = None
            self.speed = self.set_speed()

        def update(self):
            self.target = self.set_target()
            if self.target != self.grid_pos:
                self.pix_pos += self.direction * self.speed
                if self.time_to_move():
                    self.move()

            # Setting grid position in reference to pix position
            self.grid_pos[0] = (self.pix_pos[0]-TOP_BOTTOM_BUFFER +
                                self.app.cell_width//2)//self.app.cell_width+1
            self.grid_pos[1] = (self.pix_pos[1]-TOP_BOTTOM_BUFFER +
                                self.app.cell_height//2)//self.app.cell_height+1

        def draw(self):
            pygame.draw.circle(self.app.screen, self.colour,
                               (int(self.pix_pos.x), int(self.pix_pos.y)), self.radius)

        def set_speed(self):
            if self.personality in ["rapid", "frightened"]:
                speed = 1
            else:
                speed = 0.90
            return speed

        def set_target(self):
            if self.personality == "rapid" or self.personality == "sluggish":
                return self.app.player.grid_pos
            else:
                if self.app.player.grid_pos[0] > COLS//2 and self.app.player.grid_pos[1] > ROWS//2:
                    return vec(1, 1)
                if self.app.player.grid_pos[0] > COLS//2 and self.app.player.grid_pos[1] < ROWS//2:
                    return vec(1, ROWS-2)
                if self.app.player.grid_pos[0] < COLS//2 and self.app.player.grid_pos[1] > ROWS//2:
                    return vec(COLS-2, 1)
                else:
                    return vec(COLS-2, ROWS-2)

        def time_to_move(self):
            if int(self.pix_pos.x+TOP_BOTTOM_BUFFER//2) % self.app.cell_width == 0:
                if self.direction == vec(1, 0) or self.direction == vec(-1, 0) or self.direction == vec(0, 0):
                    return True
            if int(self.pix_pos.y+TOP_BOTTOM_BUFFER//2) % self.app.cell_height == 0:
                if self.direction == vec(0, 1) or self.direction == vec(0, -1) or self.direction == vec(0, 0):
                    return True
            return False

        def move(self):
            if self.personality == "random":
                self.direction = self.get_random_direction()
            if self.personality == "sluggish":
                self.direction = self.get_path_direction(self.target)
            if self.personality == "rapid":
                self.direction = self.get_path_direction(self.target)
            if self.personality == "frightened":
                self.direction = self.get_path_direction(self.target)

        def get_path_direction(self, target):
            next_cell = self.find_next_cell_in_path(target)
            xdir = next_cell[0] - self.grid_pos[0]
            ydir = next_cell[1] - self.grid_pos[1]
            return vec(xdir, ydir)

        def find_next_cell_in_path(self, target):
            path = self.BFS([int(self.grid_pos.x), int(self.grid_pos.y)], [
                            int(target[0]), int(target[1])])
            return path[1]

        def BFS(self, start, target):
            grid = [[0 for x in range(28)] for x in range(30)]
            for cell in self.app.walls:
                if cell.x < 28 and cell.y < 30:
                    grid[int(cell.y)][int(cell.x)] = 1
            queue = [start]
            path = []
            visited = []
            while queue:
                current = queue[0]
                queue.remove(queue[0])
                visited.append(current)
                if current == target:
                    break
                else:
                    neighbours = [[0, -1], [1, 0], [0, 1], [-1, 0]]
                    for neighbour in neighbours:
                        if neighbour[0]+current[0] >= 0 and neighbour[0] + current[0] < len(grid[0]):
                            if neighbour[1]+current[1] >= 0 and neighbour[1] + current[1] < len(grid):
                                next_cell = [neighbour[0] + current[0], neighbour[1] + current[1]]
                                if next_cell not in visited:
                                    if grid[next_cell[1]][next_cell[0]] != 1:
                                        queue.append(next_cell)
                                        path.append({"Current": current, "Next": next_cell})
            shortest = [target]
            while target != start:
                for step in path:
                    if step["Next"] == target:
                        target = step["Current"]
                        shortest.insert(0, step["Current"])
            return shortest

        def get_random_direction(self):
            while True:
                number = random.randint(-2, 1)
                if number == -2:
                    x_dir, y_dir = 1, 0
                elif number == -1:
                    x_dir, y_dir = 0, 1
                elif number == 0:
                    x_dir, y_dir = -1, 0
                else:
                    x_dir, y_dir = 0, -1
                next_pos = vec(self.grid_pos.x + x_dir, self.grid_pos.y + y_dir)
                if next_pos not in self.app.walls:
                    break
            return vec(x_dir, y_dir)

        def get_pix_pos(self):
            return vec((self.grid_pos.x*self.app.cell_width)+TOP_BOTTOM_BUFFER//2+self.app.cell_width//2,
                       (self.grid_pos.y*self.app.cell_height)+TOP_BOTTOM_BUFFER//2 +
                       self.app.cell_height//2)

        def set_colour(self):
            if self.number == 0:
                return (52, 235, 61)
            if self.number == 1:
                return (3, 242, 255)
            if self.number == 2:
                return (255, 158, 3)
            if self.number == 3:
                return (255, 3, 3)

        def set_personality(self):
            if self.number == 0:
                return "rapid"
            elif self.number == 1:
                return "sluggish"
            elif self.number == 2:
                return "random"
            else:
                return "frightened"

一個可能的改進可能是將方向保持一定距離。 添加當前方向和步數的屬性。 保持方向,只要有台階,敵人沒有撞牆:

class Enemy:
    def __init__(self):
        # [...]

        self.dir = (0, 0)
        self.steps = 0

    def get_random_direction(self):
        self.steps -= 1
        if self.steps > 0:
            next_pos = vec(self.grid_pos.x + self.dir[0], self.grid_pos.y + self.dir[1])
            if next_pos not in self.app.walls:
                return vec(self.dir)

        possible_directions = []
        for dir_x, dir_y in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
            next_pos = vec(self.grid_pos.x + dir_x, self.grid_pos.y + dir_y)
            if next_pos not in self.app.walls:
                possible_directions.append((dir_x, dir_y))

        self.steps = random.randint(3, 10)
        self.dir = random.choice(possible_directions)
        return vec(self.dir)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM