亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Pygame:試圖讓整個精靈穿過邊界而不僅僅是左上角的像素

Pygame:試圖讓整個精靈穿過邊界而不僅僅是左上角的像素

qq_笑_17 2022-12-06 15:23:24
我正在嘗試根據 astar 尋路算法讓我的精靈移動。然而,在我實現它之后,我意識到精靈的移動只是按照左上角的像素。這意味著如果算法告訴它在越過邊界后向上移動,一旦左上角像素越過該邊界,它就會這樣做。然而,這意味著整個精靈實際上并沒有穿過,如果它上方有障礙物就會導致碰撞。有沒有辦法告訴它在向上移動之前向左移動更多] 1def astar_ghost(pac,ghost):    maze=astar.create_maze(screen_width,screen_height,obstacles) #creates a maze of 0s and 1s. The 1s represent the obstacles    start=(ghost.gridloc[0],ghost.gridloc[1])    end=(ghost.goal_node[0],ghost.goal_node[1])    goal_node=astar.astar(maze,start,end)    if goal_node==None:        pass    else:        ghost.goal_node=goal_node    game=True    if ghost.goal_node[0]<ghost.gridloc[0]:#left         print('move left')         game=collision(pac,ghost) #collision is another function that checks for collision and returns True or False. If False, the game will be over         ghost.left=True         ghost.right=False         ghost.up=False         ghost.down=False     elif ghost.goal_node[0]>ghost.gridloc[0]:#right         print('move right')         game=collision(pac,ghost)         ghost.left=False         ghost.right=True         ghost.up=False         ghost.down=False    elif ghost.goal_node[1]<ghost.gridloc[1]:#up         print('move up')         game=collision(pac,ghost)         ghost.left=False         ghost.right=False         ghost.up=True         ghost.down=False    elif ghost.goal_node[1]>ghost.gridloc[1]:#down        print('move down')        game=collision(pac,ghost)        ghost.left=False        ghost.right=False        ghost.up=False        ghost.down=True
查看完整描述

1 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

您在這里問了幾個不同的問題。我會在這里回答我認為你想問的問題:有沒有辦法檢查整個精靈是否已經越過邊界,而不僅僅是左上角?. 所以,我的回答(請注意,這僅在您的邊界線是線性的情況下才有效):您需要單獨檢查每個角,然后,如果所有角都返回True,那么您繼續前進。例子:


def collision(sprite1, boundary):

    def internal_collision(point, boundary):

        ... # The actual math happens here, returns True/False

    corners = []

    for h in [0, 1]:

        for j in [0, 1]:

            corners.append([sprite1.rect.x+(h*sprite1.rect.width),

                            sprite1.rect.y+(j*sprite1.rect.height)])

    corner_check = []

    for corner in corners:

        corner_check.append(internal_collision(corner, boundary))

    return all(corner_check)

我不知道你的代碼是如何工作的,所以我盡量保持它的可塑性和可理解性,這樣你就可以在你自己的代碼中重新實現它。


查看完整回答
反對 回復 2022-12-06
  • 1 回答
  • 0 關注
  • 116 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號