简体   繁体   中英

Pygame Zero: Actor.top somehow not working

So I am currentl programming a game in Pygame Zero and I am facing a problem. I don't want the character/Actor to move outside the game window. The Window is defined by WIDTH and HEIGHT. "Tesbold" is the Actor. 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. It is later called in the 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 ). Therfore you need to check that Tesbold.top <= 0 and Tesbold.bottom >= HEIGHT instead of Tesbold.top >= HEIGHT and Tesbold.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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