How to execute a repeated command in pygame?
<p>I've been struggling to get my code to work. I want my image to move continuously while pressing W,A,S, or D instead of having to repeatedly tap the key, but my code keeps having problems. Right now, it says that the video system is not initialized. I don't understand why this is popping up - I already have pygame.init() in my code.</p>

<pre><code>import pygame

#set up the initial pygame window
pygame.init()
screen = pygame.display.set_mode([900,600])

#set background color
background = pygame.Surface(screen.get_size())
background.fill([204,255,229])
screen.blit(background, (0,0))

#Pull in the image to the program
my_image = pygame.image.load("google_logo.png")

#copy the image pixels to the screen
left_side = 50
height = 50
screen.blit(my_image, [left_side, height])

#Display changes
pygame.display.flip()

moveLeft = False
moveRight = False
moveUp = False
moveDown = False

#set up pygame event loop

while True:
for event in pygame.event.get():
    print event
if event.type == pygame.quit():
    running = False
if event.type == pygame.KEYDOWN:
         # change the keyboard variables
    if event.key == K_LEFT or event.key == ord('a'):
        moveRight = False
        moveLeft = True
    if event.key == K_RIGHT or event.key == ord('d'):
        moveLeft = False
        moveRight = True
    if event.key == K_UP or event.key == ord('w'):
        moveDown = False
        moveUp = True
    if event.key == K_DOWN or event.key == ord('s'):
        moveUp = False
        moveDown = True
if event.type == pygame.KEYUP:
    if event.key == K_ESCAPE:
        pygame.quit()
        sys.exit()
    if event.key == K_LEFT or event.key == ord('a'):
        moveLeft = False
    if event.key == K_RIGHT or event.key == ord('d'):
        moveRight = False
    if event.key == K_UP or event.key == ord('w'):
        moveUp = False
    if event.key == K_DOWN or event.key == ord('s'):
        moveDown = False

pygame.quit()
</code></pre>

<p>Thanks!</p>

<p>I've had a bit of a muck around with your code and have fixed some of your little errors... Hopefully you can read through my comments some of the reasoning of why I've changed a few things. Also, your code was getting the video error because of a few reasons I believe: 1. You were constantly calling <code>pygame.quit()</code> because it was checking if the key 'escape' was not being pressed (<code>pygame.KEYUP</code>) 2. You are meant to put '<code>pygame.display.flip()</code>' into the game loop itself so it can update the screen not just once and that's it.</p>

<p>You also needed to <code>import sys</code> in order to use the <code>sys.exit()</code> call.</p>

<p>Although the video error is fixed, your image doesn't move... Hopefully you figure out why.. :) Happy Coding!</p>

<p>-Travis</p>

<pre><code>import pygame

# Initialize pygame and create the window!
pygame.init()
screen = pygame.display.set_mode((800,600))

#Creating background Surface and setting the colour.
background = pygame.Surface(screen.get_size())
background.fill((255,255,0))

# Loading a few needed variables.
my_image = pygame.image.load("dogtest.png")

left_side = 50
height = 50

moveLeft = False
moveRight = False
moveUp = False
moveDown = False


# These varibales are standard when making a game of some sort!
total_frames = 0 #How many frames have passed.
clock = pygame.time.Clock() # Use this in the main loop to set game running
                            # at a certain speed!
FPS = 60
running = True #The game condition (whether the loop runs or not!)

while running:

    # This is constantly repeating if the user presses the 'x' to close the
    # program so the program closes safely and efficiently!
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()

        if event.type == pygame.KEYDOWN: # This handles Key Presses!
            if event.key == pygame.K_ESCAPE:
                running = False
                pygame.quit()
                sys.exit()
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                moveRight = False
                moveLeft = True
            if event.key == pygame.K_RIGHT or event.key == ord('d'):
                moveLeft = False
                moveRight = True
            if event.key == pygame.K_UP or event.key == ord('w'):
                moveDown = False
                moveUp = True
            if event.key == pygame.K_DOWN or event.key == ord('s'):
                moveUp = False
                moveDown = True

        if event.type == pygame.KEYUP: # This is checking if the key ISN'T being pressed!            
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                moveLeft = False
            if event.key == pygame.K_RIGHT or event.key == ord('d'):
                moveRight = False
            if event.key == pygame.K_UP or event.key == ord('w'):
                moveUp = False
            if event.key == pygame.K_DOWN or event.key == ord('s'):
                moveDown = False

    # Blitting the Background surface and image!
    screen.blit(background, (0, 0))
    screen.blit(my_image, (left_side, height))


    clock.tick(FPS) # Sets the speed in which the code runs, (and the game!)
    total_frames += 1 # Keeps a variable storing the number of frames have passed
    pygame.display.update() # Constanly updates the screen! You can also use: pygame.display.flip()


quit()
</code></pre>

python