Draw Snow with WASD

Published November 05, 2007
Advertisement
I took my event handler out a step and added drawing to the screen. I draw a 16x16 .bmp of snow I made in paint to the screen. You can move it around with the WASD keys. It leaves a trail like the spraypaint feature in paint. I guess the next step is to add a clearing function.

 #Setupimport sysimport osimport pygameimport randomimport mathfrom pygame.locals import *progpath = os.getcwd()+"/"keys=[False]*324snow_file_name = '\snow.bmp'def kill():    pygame.display.quit()    pygame.quit()    returndef load_image(name, colorkey=None):    fullname ='c:\Python24' + name    try:        image = pygame.image.load(fullname)    except pygame.error, message:        print 'Cannot load image:,%s', name        raise SystemExit, message    image = image.convert()    if colorkey is not None:        if colorkey is -1:            colorkey = image.get_at((0,0))        image.set_colorkey(colorkey, RLEACCEL)    print('hi')    return image #, image.get_rect()def main():    pygame.init()    pygame.font.init()    w=320.0    h=200.0    x=0    y=0    window = pygame.display.set_mode((int(w*2),int(h)))    pygame.display.set_caption("Trial")    screen = pygame.display.get_surface()    clock=pygame.time.Clock()    snow_surface = load_image(snow_file_name)    #Main loop    while True:        #Event handling        clock.tick(60)        for event in pygame.event.get():            if event.type==QUIT:                kill()                return            if event.type==KEYDOWN:                if event.key==K_ESCAPE:                    kill()                    return                keys[event.key]=True            elif event.type==KEYUP:                keys[event.key]=False                print event        if keys[K_w]:            y = y - 5            if y < 0:                y = 0                        if keys[K_s]:            y = y + 5            if y > 200:                y = 200        if keys[K_a]:            x = x - 5            if x < 0:                x = 0        if keys[K_d]:            x = x + 5            if x > 640:                x = 640                screen.blit(snow_surface, (x,y))         pygame.display.flip()         if __name__=="__main__": main()    


Definately need to clean it up and comment it better before I move on but I keep changing everything as I experiment (all the more reason to have good comments).

The next big step after screen clearing will be to have starting graphics on the screen stored in an array.

I want to do this with file loading instead of hard coding it into the program. It looks like file handling is really easy to do in Python.
Previous Entry Python
Next Entry Now with sprites
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement