1

I'd like to run the application to move the character continuously as long as a key is pressed, this code only manipulates the rendered image once upon key press.

I've tried while loops in hopes that while the key is pressed, perform action, but no luck. the program runs the loop over and over effectively crashing it. I've been stuck thinking about this and it's quite literally driving me crazy.

link to see what is happening: https://youtu.be/iuNmwgUqH4c

I want him not to just move once but continue to move as long as the key is pressed.

#======
# Imports 
#======

import pygame 
import sys

#======
# Variables 
#======

pygame.init()

Game_Over = False 
WIDTH = 800
HEIGHT = 800

MSprites = [pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Still ( Down ).png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Down ) F1.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Down ) F2.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Standing Walking ( Left ).png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Left ) F1.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Left ) F2.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Still ( Right ).png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Right ) F1.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Up ) F1.png "), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Up ) F2.png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Walking ( Up ).png"), 
        pygame.image.load(r"C:\Users\Damien Santiago\Desktop\Survival Game\Male Still ( Idle ).png")]

NAME = "Survival Game"
WHITE = (225,225,225)
CLOCK = pygame.time.Clock()
FPS = 60
Player_WIDTH = 150
Player_HEIGHT = 150
P_X = 400 - Player_WIDTH
P_Y = 400 - Player_HEIGHT
P_SPEED = 10

#======
# Initialization Code
#======

while not Game_Over:

    CLOCK.tick(2)
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption(NAME)
    screen.fill(WHITE)
    screen.blit(MSprites[0],(P_X,P_Y))
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            # minor issue below ( f key for full screen )
            if event.key == pygame.K_f:
                pygame.display.toggle_fullscreen()

            if event.key == pygame.K_a:
                screen.fill(WHITE)
                screen.blit(MSprites[3],(P_X,P_Y))
                P_X = P_X - P_SPEED
                pygame.display.update()
                CLOCK.tick(FPS)
                screen.fill(WHITE)
                screen.blit(MSprites[4],(P_X,P_Y))
                P_X = P_X - P_SPEED
                pygame.display.update()

            if event.key == pygame.K_d:
                screen.fill(WHITE)
                screen.blit(MSprites[6],(P_X,P_Y))
                P_X = P_X + P_SPEED
                pygame.display.update()
                CLOCK.tick(FPS)
                screen.fill(WHITE)
                screen.blit(MSprites[7],(P_X,P_Y))
                P_X = P_X + P_SPEED
                pygame.display.update()

I don't get any errors, Just wondering how to fix this problem of the character only moving once upon key press instead of as long as the key is pressed. Thank you so much for helping me out. I really appreciate it.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
HelloWorld
  • 35
  • 5
  • pygame.image.load(r why there is a r there? – Dr Yuan Shenghai Jun 15 '19 at 02:06
  • loads the file from a direct path – HelloWorld Jun 15 '19 at 02:41
  • From my understanding, I don't see any more error in your code. Couldn't run those code because of no image. Then I notice this r. Then try to put delay in each iteration and print out all debug info to see where is going wrong? – Dr Yuan Shenghai Jun 15 '19 at 03:12
  • Set time delay and looked at the debug output but nothing besides an unhandled exception on sys.exit() is showing. when I try to handle it with try or except it gives me a parsing error. either way, nothing is showing up as an error for the characters movement. I have delays via the pygame clock for the loading of each animation frame for the character preset for when the key events are triggered. could it be the set fps at the top being 60 when the code is really only rendering 2-3 frames of animation. No, right? cause it's all based on the computer logic detecting the key being pressed. IDK – HelloWorld Jun 15 '19 at 03:50
  • I put a video above to see whats happening. – HelloWorld Jun 15 '19 at 06:57

1 Answers1

0

The pygame.KEYDOWN event occurse onyl once when a key is pressed, because of that the sprite doesn't move continuously. When the key is released, then you get notified once by a pygame.KEYUP event. You can use this events to set a state variable that indicates if a key is pressed. Set the variable on pygame.KEYDOWN and reset the varaible on pygame.KEYUP.
Fortunately pygame can do that for you. pygame.key.get_pressed() returns a list of bools, with the "pressed" state of all the keys. The states which are returned by pygame.key.get_pressed() are set when the events are handled by pygame.event.pump() or pygame.event.get().

Use pygame.key.get_pressed() to get the state of the keys after the event loop and compute the movement dependent on the state of the keys:

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(NAME)

while not Game_Over:

    CLOCK.tick(2)

    screen.fill(WHITE)
    screen.blit(MSprites[0],(P_X,P_Y))
    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            # minor issue below ( f key for full screen )
            if event.key == pygame.K_f:
                pygame.display.toggle_fullscreen()

    # get key states
    keys = pygame.key.get_pressed()

    if keys[pygame.K_a]:
        screen.fill(WHITE)
        screen.blit(MSprites[3],(P_X,P_Y))
        P_X = P_X - P_SPEED
        pygame.display.update()
        CLOCK.tick(FPS)
        screen.fill(WHITE)
        screen.blit(MSprites[4],(P_X,P_Y))
        P_X = P_X - P_SPEED
        pygame.display.update()

    if keys[pygame.K_d]:
        screen.fill(WHITE)
        screen.blit(MSprites[6],(P_X,P_Y))
        P_X = P_X + P_SPEED
        pygame.display.update()
        CLOCK.tick(FPS)
        screen.fill(WHITE)
        screen.blit(MSprites[7],(P_X,P_Y))
        P_X = P_X + P_SPEED
        pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • so like you said, I applied the get keys function but it still moves the character once each time. I moved the code outside the event loop and it still didn't work. I also tried using constant variables like True and False to get my character moving but nothing seems to work. Thank you, but it's not working for me. – HelloWorld Jun 15 '19 at 08:43
  • @DamienSantiago *"but it's not working for me"* - so you did something wrong. The code in the answer works fine. I've tested the code right now. When the key is hold down, then the sprite continuously moves. Just copy/past the code and it will work – Rabbid76 Jun 15 '19 at 10:07
  • Yeah, must've been something with the code or update for the display, either way it worked, thank you I appreciate it very much. – HelloWorld Jun 15 '19 at 10:30