Python Can';Don’我似乎无法阻止rect离开屏幕边缘

Python Can';Don’我似乎无法阻止rect离开屏幕边缘,python,pygame,Python,Pygame,我是Pygames的新手,一直在想如何阻止我的Player1(rect)离开屏幕边缘,尝试了很多,似乎不知道该怎么做,非常感谢您的帮助 这是我到目前为止的代码 import pygame # accesses pygame files import sys # to communicate with windows # game setup ################ only runs once pygame.init() # starts the game engine cl

我是Pygames的新手,一直在想如何阻止我的Player1(rect)离开屏幕边缘,尝试了很多,似乎不知道该怎么做,非常感谢您的帮助

这是我到目前为止的代码

import pygame  # accesses pygame files
import sys  # to communicate with windows

# game setup ################ only runs once

pygame.init()  # starts the game engine
clock = pygame.time.Clock()  # creates clock to limit frames per second
FPS = 60  # sets max speed of main loop
SCREENSIZE = SCREENWIDTH, SCREENHEIGHT = 500, 500  # sets size of screen/window
screen = pygame.display.set_mode(SCREENSIZE)  # creates window and game screen

# set variables for colors RGB (0-255)

white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
yellow = (255, 255, 0)
green = (0, 255, 0)

gameState = "running"  # controls which state the games is in
player1XPos = 200
player1YPos = 200
player1Direction = ""
player1Speed = 5

# game loop #################### runs 60 times a second!


while gameState != "exit":  # game loop - note:  everything in the mainloop is indented one tab
    for event in pygame.event.get():  # get user interaction events
        if event.type == pygame.QUIT:  # tests if window's X (close) has been clicked
            gameState = "exit"  # causes exit of game loop

#EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN





#EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN #EDGE SCREEN  





        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                #print ("left")
                player1XPos -= 0
                player1Direction = "left"


            if event.key == pygame.K_RIGHT:
                #print ("right")
                player1XPos += 0 
                player1Direction = "right"


            if event.key == pygame.K_UP:
                #print ("up")
                player1YPos -= 0
                player1Direction = "up"    

            if event.key == pygame.K_DOWN:
                #print ("down")
                player1YPos += 0
                player1Direction = "down"


                #increase and decrease player1 speed below


        if event.type == pygame.KEYDOWN:              
            if event.key == ord('w'):
                player1Speed = 2.5 
        if event.type == pygame.KEYDOWN:              
            if event.key == ord('q'):
                player1Speed = 5      


    # Player 1 Event handler code now...
    if player1Direction == "left":
        player1XPos -= player1Speed        
    elif player1Direction == "right":
        player1XPos += player1Speed
    if player1Direction == "up":
        player1YPos -= player1Speed    
    if player1Direction == "down":
        player1YPos += player1Speed






    screen.fill(black)
    player1 = pygame.draw.rect(screen, red, (player1XPos, player1YPos, 100, 100))


    #player1 invisible and visible after spacebar


    if event.type == pygame.KEYDOWN:              
        if event.key == pygame.K_SPACE:
            player1 = pygame.draw.rect(screen, black, (player1XPos, player1YPos, 100, 100))


    # your code starts here ##############################





    # your code ends here ###############################
    pygame.display.flip()  # transfers build screen to human visable screen
    clock.tick(FPS)  # limits game to frame per second, FPS value
    pygame.display.update()
# out of game loop ###############
print("The game has closed")  # notifies user the game has ended
pygame.quit()   # stops the game engine
sys.exit()  # close operating system window

将播放机的左上角位置(
player1XPos
player1YPos
)限制在屏幕范围内。如果球员出界,则重置移动方向(
player1Direction
):

在游戏状态下!=“退出”:
# [...]
#玩家1事件处理程序代码现在。。。
如果player1Direction==“left”:
player1XPos-=player1速度
elif Player1方向==“右”:
player1XPos+=player1速度
如果player1Direction==“向上”:
player1YPos-=player1速度
如果player1Direction==“down”:
player1YPos+=player1速度
#将玩家限制在边界内
如果player1XPos<0:
player1XPos=0
player1Direction=“”
如果player1XPos>400:
player1XPos=400
player1Direction=“”
如果player1YPos<0:
player1YPos=0
player1Direction=“”
如果player1YPos>400:
player1YPos=400
player1Direction=“”
屏幕填充(黑色)
player1=pygame.draw.rect(屏幕,红色,(player1XPos,player1YPos,100100))

问题解决了吗?请阅读并考虑你发现最有用的东西。