2

i am new to Pygames and have been trying to figure out how to stop my Player1 (rect) from going off the edge of the screen, have tried so much and can't seem to understand what to do, any help is greatly appreciated

Here is my code so far...

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
user4157124
  • 2,809
  • 13
  • 27
  • 42
FreshApple
  • 49
  • 3

1 Answers1

1

Restrict the top left position of the player (player1XPos, player1YPos) to the bounds of the screen. If the player goes out off bounds, then reset the moving direction (player1Direction):

while gameState != "exit":
    # [...]

    # 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

    # limit the player to the bounds
    if player1XPos < 0:
        player1XPos = 0
        player1Direction = ""
    if player1XPos > 400:
        player1XPos = 400
        player1Direction = ""
    if player1YPos < 0:
        player1YPos = 0
        player1Direction = ""
    if player1YPos > 400:
        player1YPos = 400
        player1Direction = ""

    screen.fill(black)
    player1 = pygame.draw.rect(screen, red, (player1XPos, player1YPos, 100, 100))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174