Spawning an alien to chase the player I have just made a simple script which spawns an alien that chases the player. I want to move as much of the script into functions so as to minimize the amount of code, to make it run better when my game gets a lot bigger. I am new to functions and classes, and I want to know what can be turned into a class in another script and how to go about doing it. I do not want just code, I want to be able to understand what I'm doing with it as well.

import pygame, sys, random, time, math
from pygame.locals import *
pygame.init()

bifl = 'screeing.jpg'
milf = 'character.png'
alien = 'alien_1.png'

screen = pygame.display.set_mode((640, 480))
background = pygame.image.load(bifl).convert()
mouse_c = pygame.image.load(milf).convert_alpha()
nPc = pygame.image.load(alien).convert_alpha()

x, y = 0, 0
movex, movey = 0, 0

z, w = random.randint(10, 480), random.randint(10, 640)
movez, movew = 0, 0

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

        if event.type == KEYDOWN:
            if event.key == K_w:
                movey = - 4
            elif event.key == K_s:
                movey = + 4
            elif event.key == K_a:
                movex = - 4
            elif event.key == K_d:
                movex = + 4

        if event.type == KEYUP:
             if event.key == K_w:
                movey = 0
             elif event.key == K_s:
                movey = 0
             elif event.key == K_a:
                movex = 0
             elif event.key == K_d:
                movex = 0

    if w &lt; x:
        movew =+ 0.4
    if w &gt; x:
        movew =- 0.4
    if z &lt; y:
        movez =+ 0.4
    if z &gt; y:
        movez =- 0.4

    x += movex
    y += movey
    w += movew
    z += movez
    print('charecter pos: ' + str(x) + str(y))
    print('alien pos: ' + str(w) + str(z))

    chpos = x + y
    alpos = w + z
    print(alpos, chpos)
    if chpos == alpos:
        pygame.quit()
        sys.exit()




    screen.blit(background, (0, 0))
    screen.blit(mouse_c, (x, y))
    screen.blit(nPc, (w, z))

    pygame.display.update()
Long multiple choice decisions in python work well as dictionaries, rather than elifs.  

def handle_event(event):
    OnKeyDown = {K_w:(0, 4), K_s:(0,-4), K_a:(-4, 0), K_d:(4,0)}
    OnKeyUp = {K_w:(False, True), K_s:(False, True), K_a:(True, False), K_d:(True, False)}

    if event.type == KEYUP:
         delta = OnKeyDown[event]
         movex += delta[0]
         movey += delta[1]
    elif event.type == KEYDOWN:
         delta = OnKeyUp[event]
         if delta[0]: movex = 0
         if delta[1]: movey = 0

   x += movex
   y += movey