1

How can I add a sprite when no keys are pressed? I have each four directions covered when the arrow keys are pressed but when they are released the sprite goes obviously but cant think of how to add it. I tried adding else statements and other things best thing i got was the standing forward sprite being underneath the others but cant seem to get it to go when one of the arrow keys is pressed and return when they are relased.

Any help appreciated. Thanks in advance. my code:

import pygame
pygame.init()

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN = (0, 200, 0)
BLUE = (0, 0, 200)

# background
background = pygame.image.load('Playing Game state 2.png')

# character
standingforward = pygame.image.load('standingforward.png')
standingdownward = pygame.image.load('standingdownwards.png')
standingleft = pygame.image.load('standingleft.png')
standingright = pygame.image.load('standingright.png')

# player variables
x = 375
y = 525
w = 50
h = 50
vel = 0.5
screenWidth = 800
screenHeight = 575

screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("FROGGER")
sprite = pygame.draw.rect

running = True
while running:

    # sprites
    screen.blit(background, (0, 0))
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        screen.blit(standingleft, (x, y))
    if keys[pygame.K_RIGHT]:
        screen.blit(standingright, (x, y))
    if keys[pygame.K_DOWN]:
        screen.blit(standingdownward, (x, y))
    if keys[pygame.K_UP]:
        screen.blit(standingforward, (x, y))

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

    pygame.display.flip()

    # controls
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        x -= vel
        if x < 0:
            x = 0
    if keys[pygame.K_RIGHT]:
        x += vel
        if x > screenWidth-w:
            x = screenWidth-w
    if keys[pygame.K_UP]:
        y -= vel
        if y < 0:
            y = 0

    if keys[pygame.K_DOWN]:
        y += vel
        if y > screenHeight-h:
            y = screenHeight-h


pygame.quit()
htfsam
  • 11
  • 2
  • you should `screen.blit(currentSprite, (x, y))` after the check of a key stroke. Previous to that, you should declare a variable to hold which sprite should be shown, and when you press a key, you should allocate the correct sprite to this variable, eg `currentSprite = standingright `. Good Luck! – Bolovsky Mar 18 '20 at 17:14
  • @Bolovsky thanks for comment, however this is my first project so unsure on what that means fully – htfsam Mar 18 '20 at 17:23

1 Answers1

0

Add a variable that refers to the current image. Change the variable when a key is pressed. Draw the current image in the application loop:

current_image = standingright

running = True
while running:

       
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        current_image = standingleft
    if keys[pygame.K_RIGHT]:
        current_image = standingright
    if keys[pygame.K_DOWN]:
        current_image = standingdownward
    if keys[pygame.K_UP]:
        current_image = standingforward

    screen.blit(background, (0, 0))
    screen.blit(current_image, (x, y))

    # [...]
Rabbid76
  • 202,892
  • 27
  • 131
  • 174