使用 A* 寻路和 pygame 进行游戏,但精灵和视觉效果未按预期方式工作

问题描述 投票:0回答:0

我正在制作一款自上而下的越狱游戏,它使用寻路让玩家移动角色并让守卫追你。我分阶段这样做;第一阶段只是设置寻路,接下来是创建网格和玩家,显然允许玩家在地图上移动。这一切都很完美。然而,当我加入守卫时,问题就来了。我所做的只是为守卫创建一个新类,进行基本的精灵设置,然后在寻路类中重用囚犯代码,并为守卫稍微调整一下。然而,当我认为我做对了一切并按下运行时。虽然它跑了,但守卫根本没有移动,玩家到达他们按下的区域所走的路径出现半秒然后消失。(角色仍然移动到玩家点击的地方)

这是我试过的:

import pygame, sys

from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder
from pathfinding.core.diagonal_movement import DiagonalMovement

class Pathfinder:
    def __init__(self, matrix):

        self.matrix = matrix
        self.grid = Grid(matrix=matrix)
        self.select_surf = pygame.image.load('selection.png').convert_alpha()

        self.path = []

        #prisoner
        self.prisoner = pygame.sprite.GroupSingle(Prisoner(self.empty_path))
        self.guard = pygame.sprite.GroupSingle(Guard(self.empty_path))

    def empty_path(self):
        self.path = []




    def draw_plot_point(self):
        mouse_pos = pygame.mouse.get_pos()
        row = mouse_pos[1] // 32
        col = mouse_pos[0] // 32
        current_plot_value = self.matrix[row][col]
        if current_plot_value == 1:
            rect = pygame.Rect((col * 32, row * 32),(32 , 32))
            screen.blit(self.select_surf, rect)

    def create_path(self):

       #this is for the start point
        start_x, start_y = self.prisoner.sprite.get_coord()
        start = self.grid.node(start_x, start_y)

        #this is for the end point
        mouse_pos = pygame.mouse.get_pos()
        end_x, end_y = mouse_pos[0] // 32, mouse_pos[1] // 32
        end = self.grid.node(end_x, end_y)

        #this is for the path
        finder = AStarFinder(diagonal_movement = DiagonalMovement.always)
        self.path,_ = finder.find_path(start, end, self.grid)
        self.grid.cleanup()
        self.prisoner.sprite.set_path(self.path)

    def create_path_guard(self):

        #start point which is where the guard is
        start_x,start_y = self.guard.sprite.get_coord()
        start = self.grid.node(start_x, start_y)

        #this is for the end point of the movement of the guard
        prisoner_pos = pygame.prisoner.sprite.get_coord()
        end_x, end_y = prisoner_pos[0] // 32, prisoner_pos[1] // 32
        end = self.grid.node(end_x, end_y)

        #this is for the path of the guard
        finder = aStarFinder(diagonal_movement = DiagonalMovement.always)
        self.path,_ = finder.find_path(start, end, self.grid)
        self.grid.cleanup()
        self.guard.sprite.set_path(self, path)


    def draw_path(self):
        if self.path:
            points = []
            for point in self.path:
                x = (point[0] * 32) + 16
                y = (point[1] * 32) + 16
                points.append((x,y))
                pygame.draw.circle(screen,'#4a4a4a', (x,y),10)
            pygame.draw.lines(screen, '#4a4a4a', False, points, 5)

    def update(self):
        self.draw_plot_point()
        self.draw_path()

        self.prisoner.update()
        self.prisoner.draw(screen)

        self.guard.update()
        self.guard.draw(screen)


class Guard(pygame.sprite.Sprite):
    def __init__(self,empty_path):

        #basic setup of guard
        super().__init__()
        self.image = pygame.image.load('guard1.png').convert_alpha()
        self.rect = self.image.get_rect(center = (60, 60))


        #movement of guard
        self.pos = self.rect.center
        self.speed = 2
        self.direction = pygame.math.Vector2(0, 0)

        #path of guard
        self.path = []
        self.collision_rects = []
        self.empty_path = empty_path

    def get_coord(self):
        col = self.rect.centerx // 32
        row = self.rect.centery // 32
        return (col, row)

    def set_path(self, path):
        self.path = path
        self.create_collision_rects()
        self.get_direction()

    def create_collision_rects(self):
        if self.path:
            self.collision_rects = []
            for point in self.path:
                x = (point[0] * 32) + 16
                y = (point[1] * 32) + 16
                rect = pygame.Rect((x - 2, y - 2), (4, 4))
                self.collision_rects.append(rect)

    def get_direction(self):
        if self.collision_rects:
            start = pygame.math.Vector2(self.pos)
            end = pygame.math.Vector2(self.collision_rects[0].center)
            self.direction = (end - start).normalize()
        else:
            self.direction = pygame.math.Vector2(0, 0)
            self.path = []

    def check_collisions(self):
        if self.collision_rects:
            for rect in self.collision_rects:
                if rect.collidepoint(self.pos):
                    del self.collision_rects[0]
                    self.get_direction()

        else:
            self.empty_path()

    def update(self):
        self.pos += self.direction * self.speed
        self.check_collisions()
        self.rect.center = self.pos
        if self.rect.left < 0:
            self.rect.left = 0
        elif self.rect.right > SCREEN_WIDTH:
            self.rect.right = SCREEN_WIDTH
        if self.rect.top <= 0:
            self.rect.top = 0
        elif self.rect.bottom >= SCREEN_HEIGHT:
            self.rect.bottom = SCREEN_HEIGHT


class Prisoner(pygame.sprite.Sprite):
    def __init__(self,empty_path):

        #basic setup of the sprite
        super().__init__()
        self.image = pygame.image.load('prisoner2.png').convert_alpha()
        self.rect = self.image.get_rect(center = (1420, 100))


        #movement of the prisoner
        self.pos = self.rect.center
        self.speed = 3
        self.direction = pygame.math.Vector2(0, 0)

        #path of prisoner
        self.path = []
        self.collision_rects = []
        self.empty_path = empty_path


    def get_coord(self):
        col = self.rect.centerx // 32
        row = self.rect.centery // 32
        return (col, row)


    def set_path(self,path):
        self.path = path
        self.create_collision_rects()
        self.get_direction()

    def create_collision_rects(self):
        if self.path:
            self.collision_rects = []
            for point in self.path:
                x = (point[0] * 32) + 16
                y = (point[1] * 32) + 16
                rect = pygame.Rect((x - 2,y - 2),(4,4))
                self.collision_rects.append(rect)

    def get_direction(self):
       if self.collision_rects:
            start = pygame.math.Vector2(self.pos)
            end = pygame.math.Vector2(self.collision_rects[0].center)
            self.direction = (end - start).normalize()
       else:
           self.direction = pygame.math.Vector2(0,0)
           self.path = []

    def check_collisions(self):
        if self.collision_rects:
            for rect in self.collision_rects:
                if rect.collidepoint(self.pos):
                    del self.collision_rects[0]
                    self.get_direction()
                    
        else:
            self.empty_path()

    def update(self):
        self.pos += self.direction * self.speed
        self.check_collisions()
        self.rect.center = self.pos
        if self.rect.left < 0:
            self.rect.left = 0
        elif self.rect.right > SCREEN_WIDTH:
            self.rect.right = SCREEN_WIDTH
        if self.rect.top <= 0:
            self.rect.top = 0
        elif self.rect.bottom >= SCREEN_HEIGHT:
            self.rect.bottom = SCREEN_HEIGHT

pygame.init()
SCREEN_HEIGHT = 800
SCREEN_WIDTH = 1535
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))

clock = pygame.time.Clock()
bg_surf = pygame.image.load('prison1.png').convert()

matrix = [
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    ]

pathfinder = Pathfinder(matrix)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            pathfinder.create_path()
    screen.blit(bg_surf,(0,0))
    pathfinder.update()
    pygame.display.update()
    clock.tick(60)

它没有给我错误代码,只是运行但没有做它应该做的事情。

我不确定我做错了什么,有人可以帮忙吗?

python pygame a-star
© www.soinside.com 2019 - 2024. All rights reserved.