Vector2 返回零值,无法进行规范化并导致代码崩溃

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

这是具体的功能,一些术语如 .center 出现白色而其他出现浅蓝色,所以我不太确定这是否是一个问题,使用 VSCODE,可能是他们的原生着色或其他东西,开始和结束的 vector2 值删除 normalise() 时显示为零

    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 = (start - end).normalize()
            print(self.direction)
        else:
            self.direction = pygame.math.Vector2(0,0)
            self.path = []

这是完整的代码

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):
        #sets up selection icon
        self.matrix = matrix 
        self.grid = Grid(matrix = matrix)
        selectionicon = pygame.image.load('selection.png').convert_alpha()
        self.scaledicon = pygame.transform.scale(selectionicon, (30,30)) 
        self.path = []

        self.Agent = pygame.sprite.GroupSingle(Agent())


    def draw_active_cell(self):
        #gets mouse position
        mouse_pos = pygame.mouse.get_pos()
        row = mouse_pos[1] // 30 
        col = mouse_pos[0] // 30
        current_cell_value = self.matrix[row][col]
        #checks if cell is a viable option
        if current_cell_value == 1:
            rect = pygame.Rect((col * 30 , row * 30),(30,30))
            screen.blit(self.scaledicon,rect)

    def create_path(self):
        
        #start cell
        start_x, start_y = self.Agent.sprite.get_coord()    
        start = self.grid.node(start_x,start_y)

        #end cell
        mouse_pos = pygame.mouse.get_pos()
        end_x = mouse_pos[0] // 30 
        end_y = mouse_pos[1] // 30
        end = self.grid.node(end_x,end_y)

        #pathcreation
        finder = AStarFinder(diagonal_movement = DiagonalMovement.always)
        self.path,_ = finder.find_path(start,end,self.grid)
        self.grid.cleanup()
        self.Agent.sprite.set_path(self.path)

    #draws a path for visualisation
    def draw_path(self):
        if self.path:
            points = []
            for point in self.path:
                x = (point[0] * 30) + 15
                y = (point[1] * 30) + 15
                points.append((x,y))
                pygame.draw.circle(screen,"#11db03",(x,y),2)
            pygame.draw.lines(screen,"#11db03",False,points,5)

    #updates self draw
    def update(self):
        self.draw_active_cell()
        self.draw_path()
        self.Agent.update()
        self.Agent.draw(screen)

class Agent(pygame.sprite.Sprite):
    #creating sprite
    def __init__(self):
        super().__init__()
        image_unscaled = pygame.image.load('agent.png').convert_alpha()
        self.image = pygame.transform.scale(image_unscaled, (30,30))
        self.rect = self.image.get_rect(center = (405,675))

        #movement variables
        self.pos = self.rect.center
        self.speed = 0.5   
        self.direction = pygame.math.Vector2(0,0)

        #path follower
        self.path = []
        self.collision_rects = []
    
    def get_coord(self):
        col = self.rect.centerx // 30
        row = self.rect.centery // 30
        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] * 30) + 15
                y = (point[1] * 30) + 15
                rect = pygame.Rect((x - 4,y - 4),(8,8))
                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 = (start - end).normalize()
            print(self.direction)
        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()
    
    def update(self):
        self.pos += self.direction * self.speed  
        self.rect.center = self.pos



pygame.init()
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
clock = pygame.time.Clock()
pygame.mouse.set_visible(False)

bg_room1 = pygame.image.load('room1scaled.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,0,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,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
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit
        if event.type == pygame.MOUSEBUTTONDOWN:
            pathfinder.create_path( )

    #creates background
    screen.blit(bg_room1,(180,0))
    #updates pathfinder function
    pathfinder.update()

    pygame.display.update()
    clock.tick(30)

函数应该给我指向下一个节点的方向

python pygame path-finding vector2
© www.soinside.com 2019 - 2024. All rights reserved.