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.