1

My character is able to move properly. However, when no key is being pressed, a problem arises. How do I make it so when no KEY is being pressed, the sprite will face the same direction it was last facing? For example, if I press the UP, it would face up and then when I don't press any other key, it would still face UP? Same with the other directions too. Also, how I would display my character when first starting up the game? Like, my character only shows up when a key is pressed and remains invisible when not.

Here's my code:

Sprites:

# Protagonist Sprites
protagR = [pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_R1_1.png")]
protagL = [pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_L1_1.png")]
protagU = [pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_U1_1.png")]
protagD = [pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_3.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_2.png"), pygame.image.load("ASG2_Graphics/JoArrow_D1_1.png")]
# Bullet Sprites
bulletsP = [pygame.image.load("ASG2_Graphics/Bullet_1.png"), pygame.image.load("ASG2_Graphics/Bullet_2.png"), pygame.image.load("ASG2_Graphics/Bullet_3.png"), pygame.image.load("ASG2_Graphics/Bullet_4.png")]
bulletsE = [pygame.image.load("ASG2_Graphics/E_Bullet_1.png"), pygame.image.load("ASG2_Graphics/E_Bullet_2.png"), pygame.image.load("ASG2_Graphics/E_Bullet_3.png")]
# Enemy Sprites
enemyR = [pygame.image.load("ASG2_Graphics/Enemy_R1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_R1_5.png")]
enemyL = [pygame.image.load("ASG2_Graphics/Enemy_L1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_L1_5.png")]
enemyU = [pygame.image.load("ASG2_Graphics/Enemy_U1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_U1_5.png")]
enemyD = [pygame.image.load("ASG2_Graphics/Enemy_D1_1.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_2.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_3.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_4.png"), pygame.image.load("ASG2_Graphics/Enemy_D1_5.png")]
# Background Sprite
bg = pygame.image.load("Star_Crusaders_MAP.png")

main()

def main():
    board = player(30, 45, 64, 64, 0)
    while True:
        movePlayer(board) 

class player():

class player():
    def __init__(self, x, y, width, height, walkCount):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5
        self.up = False
        self.down = False
        self.left = False
        self.right = False
        self.up = False
        self.down = False
        self.walkCount = walkCount

movePlayer()

    def movePlayer(board):
        clock = pygame.time.Clock()
        run = True

        while run:
            clock.tick(15)

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

            arrowKeys = pygame.key.get_pressed()
            if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
                board.x -= board.vel
                board.left = True
                board.right = False
                board.up = False
                board.down = False
            elif arrowKeys[pygame.K_RIGHT] and board.x < 485 - board.width - board.vel:
                board.x += board.vel
                board.right = True
                board.left = False
                board.up = False
                board.down = False
            elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
                board.y -= board.vel
                board.right = False
                board.left = False
                board.up = True
                board.down = False
            elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
                board.y += board.vel
                board.right = False
                board.left = False
                board.up = False
                board.down = True
            elif arrowKeys[pygame.K_ESCAPE]:  # ESC key
                stopGame()
            else:
                board.right = False
                board.left = False
                board.up = False
                board.down = False
                board.walkCount = 0

            redrawGameWindow(board)

redrawGameWindow()

def redrawGameWindow(board):
    window.blit(bg, (0,0))
    dest = (board.x, board.y)

    if board.walkCount + 1 >= 30:
        board.walkCount = 0
    if board.left:
        window.blit(protagL[0], dest)
        # currentSprite = protagL[0]
    elif board.right:
        window.blit(protagR[0], dest)
        # currentSprite = protagR[0]
    elif board.up:
        window.blit(protagU[0], dest)
        # currentSprite = protagU[0]
    elif board.down:
        window.blit(protagD[0], dest)
        # currentSprite = protagD[0]
    # stillSprite(currentSprite, board)
    pygame.display.update()

As you can see, I tried having a currentSprite() variable and a stillSprite() function to try and store the last key pressed but to no avail. Any help on this would be greatly appreciated, thank you.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Haqeem Wan
  • 83
  • 7
  • maybe remove `else:` in which you set `False` for all directions - `board.right`, `board.left`, `board.up`, `board.down` – furas Feb 09 '20 at 23:50
  • Ah, apologies for the late update, I actually solved the issue and forgot to update this post. My fault. Here's what happened: I ended up using a very similar method to the one you gave, but instead of using last_key, I used a variable I named last_direction. – Haqeem Wan Feb 13 '20 at 18:35
  • Thank you so much for the help! Cheers to you, good sir/madame – Haqeem Wan Feb 13 '20 at 18:37

1 Answers1

2

The issue is, that the states board.left, board.right, board.up, board.down are reset when the key is released, because the movement has to be stopped. Since the image should keep the direction, this states can't be used for that requirement. You have to add an additional state.

State the key which was pressed last in an attribute (e.g.: board.last_key):

arrowKeys = pygame.key.get_pressed()
if arrowKeys[pygame.K_LEFT] and board.x > 25 + board.vel:
    board.x -= board.vel
    board.left, board.right, board.up, board.down = True, False, False, False
    board.last_key = pygame.K_LEFT
elif arrowKeys[pygame.K_RIGHT] and board.x < 510 - board.width - board.vel:
    board.x += board.vel
    board.left, board.right, board.up, board.down = False, True, False, False
    board.last_key = pygame.K_RIGHT
elif arrowKeys[pygame.K_UP] and board.y > 40 + board.vel:
    board.y -= board.vel
    board.left, board.right, board.up, board.down = False, False, True, False
    board.last_key = pygame.K_UP
elif arrowKeys[pygame.K_DOWN] and board.y < 450 - board.height - board.vel:
    board.y += board.vel
    board.left, board.right, board.up, board.down = False, False, False, True
    board.last_key = pygame.K_DOWN
elif arrowKeys[pygame.K_ESCAPE]:  # ESC key
    stopGame()
else:
    board.left, board.right, board.up, board.down = False, False, False, False
    board.walkCount = 0

Choose the image dependent on board.last_key:

def redrawGameWindow(board):
    window.blit(bg, (0,0))
    dest = (board.x, board.y)

    if board.walkCount + 1 >= 30:
        board.walkCount = 0
    if board.last_key == pygame.K_LEFT:
        window.blit(protagL[0], dest)
        # currentSprite = protagL[0]
    elif board.last_key == pygame.K_RIGHT:
        window.blit(protagR[0], dest)
        # currentSprite = protagR[0]
    elif board.last_key == pygame.K_UP:
        window.blit(protagU[0], dest)
        # currentSprite = protagU[0]
    elif board.last_key == pygame.K_DOWN:
        window.blit(protagD[0], dest)
        # currentSprite = protagD[0]
    # stillSprite(currentSprite, board)
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174