Search code examples
pythonpygamegame-physics

Why won't my Movement code work?


I am trying to make a character move around. My problem is that when I run the program it immediately stops responding so I don't even know what the problem is. Here is my code.

import pygame, sys
from pygame.locals import*

pygame.init()
DISPLAYSURF = pygame.display.set_mode((780, 500), 0, 32)

FPS = 30
fpsClock = pygame.time.Clock()

sprite = pygame.image.load('CharacterFront.png')
spritex = 50
spritey = 50
charLeft = False
charRight = False
charUp = False
charDown = False

while True:

DISPLAYSURF.blit(sprite,(spritex,spritey))

for event in pygame.event.get():
    if event.type==QUIT:
        pygame.quit()
        sys.exit()

    if event.type == KEYDOWN:
        if (event.key == K_LEFT):
            charLeft = True
        elif (event.key == K_d):
            charRight = True
        elif (event.key == K_w):
            charUp = True
        elif (event.key == K_s):
            charDown = True
    if event.type == KEYUP:
        if (event.key == K_LEFT):
            charLeft = False
        elif (event.key == K_d):
            charRight = False
        elif (event.key == K_w):
            charUp = False
        elif (event.key == K_s):
            charDown = False

while charLeft == True:
    spritex -= 10
    sprite=pygame.image.load('CharacterLeft.png')
while charRight == True:
    spritex += 10
    sprite=pygame.image.load('CharacterRight.png')
while charUp == True:
    spritey -= 10
    sprite=pygame.image.load('CharacterBack.png')
while charDown == True:
    spritey += 10
    sprite=pygame.image.load('CharacterFront.png')


pygame.display.update()
fpsClock.tick(FPS)

I have already tried many different ways to do this but the closest I got caused the character to get pasted over and over and I had to spam the directions to actually move more than 10 pixels.


Solution

  • Apart from what jonrsharpe said, you should not load the sprite every time a keypress is done. Instead load all your images before, and just blit them when necessary.

    So your code will look like this:

    sprite_back = pygame.image.load('CharacterBack.png')
    sprite_front = pygame.image.load('CharacterFront.png')
    sprite_right = pygame.image.load('CharacterRight.png')
    sprite_left = pygame.image.load('CharacterLeft.png')
    
    sprite = sprite_front
    
    while True:
    
        DISPLAYSURF.blit(sprite,(spritex,spritey))
    
        if charLeft == True:
            spritex -= 10
        elif charRight == True:
            spritex += 10
        elif charUp == True:
            spritey -= 10
        elif charDown == True:
            spritey += 10
    
        for event in pygame.event.get():
            if event.type==QUIT:
                pygame.quit()
                sys.exit()
    
            if event.type == KEYDOWN:
                if (event.key == K_LEFT):
                    charLeft = True
                    sprite=sprite_left
                elif (event.key == K_d):
                    charRight = True
                    sprite=sprite_right
                elif (event.key == K_w):
                    charUp = True
                    sprite=sprite_back
                elif (event.key == K_s):
                    charDown = True
                    sprite=sprite_front
            if event.type == KEYUP:
                if (event.key == K_LEFT):
                    charLeft = False
                elif (event.key == K_d):
                    charRight = False
                elif (event.key == K_w):
                    charUp = False
                elif (event.key == K_s):
                    charDown = False