简体   繁体   English

Pygame 零:Actor.top 无法正常工作

[英]Pygame Zero: Actor.top somehow not working

So I am currentl programming a game in Pygame Zero and I am facing a problem.所以我目前正在 Pygame 零编程游戏,我遇到了一个问题。 I don't want the character/Actor to move outside the game window.我不希望角色/演员移动到游戏 window 之外。 The Window is defined by WIDTH and HEIGHT. Window 由 WIDTH 和 HEIGHT 定义。 "Tesbold" is the Actor. “Tesbold”是演员。 Somehow this works when tried on the left and right side.当在左侧和右侧尝试时,这会以某种方式起作用。 Only the upper and lower collision somehow doesn't work.只有上下碰撞不知何故不起作用。 Here is the function I use for it.这是我使用的 function。 It is later called in the update() function.稍后在 update() function 中调用它。

def oob_check(): #Out-Of-Bounce-Check
    if Tesbold.right >= WIDTH:
        Tesbold.right = WIDTH
    elif Tesbold.left <= 0:
        Tesbold.left = 0
    elif Tesbold.top >= HEIGHT:
        Tesbold.top = HEIGHT
    elif Tesbold.bottom <= 0:
        Tesbold.bottom = 0

def update():
    oob_check()

The top left coordinate of the window is (0, 0) and the bottom right of the window is ( WIDTH , HEIGHT ). window 的左上角坐标为 (0, 0),window 的右下角坐标为 ( WIDTH , HEIGHT )。 Therfore you need to check that Tesbold.top <= 0 and Tesbold.bottom >= HEIGHT instead of Tesbold.top >= HEIGHT and Tesbold.bottom <= 0 :因此,您需要检查Tesbold.top <= 0Tesbold.bottom >= HEIGHT而不是Tesbold.top >= HEIGHTTesbold.bottom <= 0

def oob_check(): #Out-Of-Bounce-Check
    if Tesbold.right >= WIDTH:
        Tesbold.right = WIDTH
    elif Tesbold.left <= 0:
        Tesbold.left = 0
    elif Tesbold.bottom >= HEIGHT:
        Tesbold.bottom = HEIGHT
    elif Tesbold.top <= 0:
        Tesbold.top = 0

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

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