Nojo Wizard
Nojo Wizard

Reputation: 1

Stop moving when let go key

I am trying to move Left and Right and whenever I let go of the right arrow it keeps moving right and same with the left side of things. I have been messing around with this and cant figure it out.

import pygame

# Intialize the pygame
pygame.init()

# create the screen
screen = pygame.display.set_mode((800,600))

# Player
PlayerHealth = 100
Playerinven = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
playerImg = pygame.image.load('001-alien.png')

# Player Movment
playerX = 370
playerY = 480
XLlabel = False
XRlabel = False
playerX_change = 0
def player(x, y):
    screen.blit(playerImg, (x , y))

# Game Loop
running = True
while running:

    # RGB - Red,Green,Blue
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                XLlabel = True
            elif event.key == pygame.K_RIGHT:
                XRlabel = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                XLlabel = False
            if event.key == pygame.K_RIGHT:
                XRlabel = False
        if XLlabel == True and XRlabel == True:
            playerX_change = 0
        elif XLlabel == True:
            playerX_change = -0.3
        elif XRlabel == True:
            playerX_change = 0.3
        elif XLlabel == False and XRlabel == False:
            player_change = 0
    playerX += playerX_change
    if playerX <= 0:
        playerX = 0
    if playerX >= 736:
        playerX = 736
    player(playerX, playerY)
    pygame.display.update()

This is my first time trying to make a game so it is kinda stressing me out XD

Upvotes: 0

Views: 162

Answers (2)

Bibhav
Bibhav

Reputation: 1757

Error:

  • You have written player_change = 0 where there must be playerX_change = 0.

Code:

# Player
PlayerHealth = 100
Playerinven = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
playerImg = pygame.image.load('001-alien.png')

# Player Movment
playerX = 370
playerY = 480
XLlabel = False
XRlabel = False
playerX_change = 0
def player(x, y):
    screen.blit(playerImg, (x , y))

# Game Loop
running = True
while running:

    # RGB - Red,Green,Blue
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                XLlabel = True
            elif event.key == pygame.K_RIGHT:
                XRlabel = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                XLlabel = False
            if event.key == pygame.K_RIGHT:
                XRlabel = False
        if XLlabel == True and XRlabel == True:
            playerX_change = 0
        elif XLlabel == True:
            playerX_change = -0.3
        elif XRlabel == True:
            playerX_change = 0.3
        elif XLlabel == False and XRlabel == False:
            playerX_change = 0    #just a small error you have written player_change = 0
    playerX += playerX_change
    if playerX <= 0:
        playerX = 0
    if playerX >= 736:
        playerX = 736
    player(playerX, playerY)
    pygame.display.update()

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881783

Not sure if this is the cause of your problem but your handling of the keys seems a little "tortured".

For a start, you should generally never use if XLlabel == True, preferring instead the more succinct if XLlabel.

The key handling can then be made something like:

# Initial left/right key state flags.

left_down, right_down = False, False

for event in pygame.event.get():
    # Process events, keys up/down sets flags.

    if event.type == pygame.QUIT:
        running = False

    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            left_down = True
        elif event.key == pygame.K_RIGHT:
            right_down = True

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT:
            left_down = False
        elif event.key == pygame.K_RIGHT:
            right_down = False

    # Event processing done, evaluate key states to get velocity.

    velocity = 0
    if left_down:
        velocity -= 0.3
    if right_down:
        velocity += 0.3

    # Adjust player position by velocity.

   ...

Upvotes: 0

Related Questions