BluTac
BluTac

Reputation: 23

Window boundaries in pygame

Basically I am trying to add some boundaries to a small game I am making, I have a sprite which can only move left to right and I am trying to prevent it from it from moving off the game window, if the sprite reaches the edge they can go no further and can then go in the opposite direction. My current code:

tankSprite = pygame.image.load('Sprites/Tank.png') #fixed apostrophe
tankSprite_width = 28


def tank(x,y):
    gameWindow.blit(tankSprite, (x,y))


def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)
    x_change = 0

    gameExit = False
    gameMove = True

    while not gameExit and gameMove == True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                 gameExit = False

            if event.type ==pygame.KEYDOWN:
               if event.key == pygame.K_LEFT:
                   x_change = -5
               if event.key == pygame.K_RIGHT:
                   x_change = 5
           if event.type == pygame.KEYUP:
               if event.key == pygame.K_LEFTor event.key == pygame.K_RIGHT:
                      x_change = 0
       x += x_change

       gameWindow.fill(white)
       tank(x,y)

       if x > display_width - tankSprite_width or x < 0:
            gameMove = False
       elif x < display_width - tankSprite_width or x > 0:
                gameMove = True

       pygame.display.update()
       clock.tick(60)


game_loop()
pygame.quit()
quit()

My current code partially works, when the sprite reaches the edge the solution closes itself rather than letting me go back. If I remove:

pygame.quit()
quit()

Then the sprite just stops and I can't move it back. In a project I did a while back I used something like:

if event.key == pygame.K_LEFT and sprite.x < 590:
        sprite.x += 5

The above code worked well from what I can remember but I can't seem to figure it out for my project now, can anyone help me out please? I wish to make it so that the sprite can't go past the screen border, when the player reaches the border they either stay or go back in the opposite direction. Thanks

p.s. Sorry about the dodgy formatting

Upvotes: 1

Views: 1276

Answers (1)

River
River

Reputation: 9093

Just split your if-statement and check the condition for each direction separately:

tankSprite = pygame.image.load('Sprites/Tank.png')
tankSprite_width = 28


def tank(x,y):
    gameWindow.blit(tankSprite, (x,y))


def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)
    x_change = 0

    gameExit = False
    gameMove = True

    while not gameExit and gameMove == True:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                 gameExit = False

            if event.type ==pygame.KEYDOWN:
               if event.key == pygame.K_LEFT:
                   x_change = -5
               if event.key == pygame.K_RIGHT:
                   x_change = 5
           if event.type == pygame.KEYUP:
               if event.key == pygame.K_LEFTor event.key == pygame.K_RIGHT:
                      x_change = 0
        #valid move, add x_change
        if((x+x_change)> 0 and (x+x_change)<(display_width - tankSprite_width):
            x += x_change

        gameWindow.fill(white)
        tank(x,y)

#deleted stuff here

        pygame.display.update()
        clock.tick(60)


game_loop()
pygame.quit()
quit()

With this method, you don't need to even use the gameMove variable anymore.

You could also get rid of the x_change variable entirely by simply applying the changes directly to x instead.

Also, I think you may have meant gameExit = True under the if statement if event.type == pygame.QUIT:, as it makes sense to exit when event pygame.QUIT is triggered.

Upvotes: 1

Related Questions