-1

I'm stuck trying to change the sprite blitted to the screen (when moving upwards) to a back view of the sprite.

Currently the sprite is just blitted to the white background on each key press at the most recent x and y coordinates and not fluidly moving along the screen (i want to move the sprite continuously on set trajectories, based on arrow key press, like in snake).

This is my first stab at programming anything on python so Leyman's terms if possible!

lead_x_change = 0
lead_y_change = 0

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                lead_x_change = -5
            if event.key == pygame.K_RIGHT:
                lead_x_change = 5
    #above are two snips of code to show the method i'm using to move the sprite 

            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_DOWN:
                gameDisplay.fill(white)
                gameDisplay.blit(spriteX,(lead_x,lead_y))
                pygame.display.update()
            if event.key == pygame.K_UP:
                gameDisplay.fill(white)
                gameDisplay.blit(spriteY,(lead_x,lead_y))
                pygame.display.update()

    lead_y += lead_y_change        
    lead_x += lead_x_change
    clock.tick(60)

pygame.quit()
quit()
Blckknght
  • 100,903
  • 11
  • 120
  • 169
Croutom
  • 1
  • 1
  • Managed to sort the problem after a bit of tinkering by inserting: `gameDisplay.fill(white) if lead_y_change >= 0: gameDisplay.blit(spriteX,(lead_x,lead_y)) if lead_y_change < 0: gameDisplay.blit(spriteY,(lead_x,lead_y))` – Croutom Feb 16 '16 at 20:50

1 Answers1

0

I think there are two plausible ways you can change your sprite depending on the direction you're moving.

First off, lets fix your current drawing logic, so that you draw the sprite regardless of keypresses:

while not gameExit:
    for event in pygame.event.get(): # event loop handles setting player speed
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                lead_x_change = -5
            if event.key == pygame.K_RIGHT:
                lead_x_change = 5

    lead_y += lead_y_change  # update logic moves the position
    lead_x += lead_x_change

    gameDisplay.fill(white)  # draw logic draws the background and the sprite
    gameDisplay.blit(spriteX,(lead_x,lead_y))
    pygame.display.update()

    clock.tick(60)

The approach to the sprite is to change it at the same place you change your speed (in the event code), then use the changed sprite later when you actually draw the scene.

player_sprite = spriteX

while not gameExit:
    for event in pygame.event.get(): # event loop handles setting speed and the sprite
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                lead_x_change = -5
                player_sprite = spriteX
            if event.key == pygame.K_UP:
                lead_y_change = -5
                player_sprite = spriteY

    lead_y += lead_y_change  # update logic moves the position
    lead_x += lead_x_change

    gameDisplay.fill(white)  # draw logic draws the background and the sprite
    gameDisplay.blit(player_sprite,(lead_x,lead_y))
    pygame.display.update()

    clock.tick(60)

The second approach is to limit the event code to just setting the speed, and then later examining the speed that has been set to pick a sprite to draw.

player_sprite = spriteX

while not gameExit:
    for event in pygame.event.get(): # event loop handles setting speed only
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                lead_x_change = -5
            if event.key == pygame.K_UP:
                lead_y_change = -5

    lead_y += lead_y_change  # update logic moves the position
    lead_x += lead_x_change

    if lead_y_change < 0:    # set sprite based on speed
        player_sprite = spriteY
    else:
        player_sprite = spriteX

    gameDisplay.fill(white)  # draw logic draws the background and the sprite
    gameDisplay.blit(player_sprite,(lead_x,lead_y))
    pygame.display.update()

    clock.tick(60)

For very simple logic with only a few different sprites, the event driven sprite changes may be easiest. If you want animations or other more complicated sprite changing behavior, the second approach may be better, since you will be able to handle all the sprite logic in one place.

Blckknght
  • 100,903
  • 11
  • 120
  • 169