First of all, I should say I'm brand new to PyGame and am pretty new to advanced Python. I'm trying to make a side-scrolling game but just making a sprite move back and forth with keyboard input is proving to be a problem. It appears that the loop is running really slowly, so when pygame.event.get() is called, it doesn't consistently "catch" any events, if that makes any sense. This is apparent when trying to close the window with the X button - it only closes if the button is clicked repeatedly. Here's the code:

import pygame, sys
from pygame.locals import *
from multiprocessing import Process

screen = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()


wasd = [False, False, False, False]

def keyUpdate():
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == K_a:
                wasd[1] = True
            if event.key == K_d:
                wasd[3] = True
        if event.type == pygame.KEYUP:
            if event.key == K_a:
                wasd[1] = False
            if event.key == K_d:
                wasd[3] = False

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, direction, image, movespeed=40):
        self.x = x
        self.y = y
        self.direction = direction
        self.image = pygame.image.load(image)
        self.movespeed = 40
        self.canMoveLeft = True
        self.canMoveRight = True
        self.width = self.image.get_width()
        self.height = self.image.get_height()

    def update(self, timePassed):
        if self.x + self.width >= MAXSCREEN_X:
            self.canMoveRight = False
            self.canMoveRight = True

        if self.x <= 0:
            self.canMoveLeft = False
            self.canMoveLeft = True

        if wasd[1] and not wasd[3] and self.canMoveLeft:
            direction = -1
        elif wasd[3] and self.canMoveRight:
            direction = 1
            direction = 0
        print(self.canMoveLeft, self.canMoveRight)

        self.x += self.direction * timePassed * self.movespeed
        screen.blit(self.image, (self.x, self.y))

player = Player(200, 200, 0, 'player.jpg')

while True:
    screen.fill((255, 255, 255))
    for event in pygame.event.get():
        if event.type == QUIT:


Like I said, I'm very new to this so I'm probably doing something really stupid. Any help here would be appreciated.

My preference for the Pygame event loop exit ...

draw animated corner lines with Python module pygame
modified from:

import pygame as pg

w = h = 500
size = 250
step = 10
pos = 0
maxlines = 40
black = (0, 0, 0)

# create line list of all corners
lines = []
for x in range(0, size+1, step):
    lines.append((0, size-x, x, 0))
for x in range(0, size+1, step):
    lines.append((w - (size-x), 0, w, x))
for x in range(0, size+1, step):
    lines.append((w, h - (size-x), w-x, h))
for x in range(0, size+1, step):
    lines.append((size-x, h, 0, h-x))

screen = pg.display.set_mode((w+1, h+1))
pg.display.set_caption('Animated Lines')
clock = pg.time.Clock()
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            # most reliable exit on x click
            raise SystemExit
        elif event.type == pg.KEYDOWN:
            # optional exit with escape key
            if event.key == pg.K_ESCAPE:
                raise SystemExit

    col = 0
    cur = pos

    for i in range(maxlines):
        x1, y1, x2, y2 = lines[cur]
        pg.draw.line(screen, (col, col, col), (x1, y1), (x2, y2))
        cur += 1
        if cur >= len(lines): cur = 0
        col += 240 / maxlines

    pos += 1
    if pos >= len(lines):
        pos = 0

This question has already been answered. Start a new discussion instead.