Displaying a background

Published November 14, 2007
Advertisement
Thanks to some help from Oluseyi I managed to adapt my program so that I display a predefined map on the background.

Here is the code:

#Setup: I import all the modules hereimport sysimport osimport pygameimport randomimport mathfrom pygame.locals import *progpath = os.getcwd()+"/"keys=[False]*324snow_file_name = '\snow.bmp' #this is the filename for the sprite I will usedef kill(): #this function is for killing the window and quitting    pygame.display.quit()    pygame.quit()    returndef load_image(name, colorkey=None): #this function is for loading the image to us in the program    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)    return image, image.get_rect()class Snow(pygame.sprite.Sprite): #this class handles loading the image that I use as a mouse pointer and tracking and drawing it    def __init__(self): #initialization        pygame.sprite.Sprite.__init__(self)        self.image, self.rect = load_image(snow_file_name, -1)    def update(self): #actions for frame update        pos = pygame.mouse.get_pos()        self.rect.midtop = pos        ######################################################def main():    pygame.init()    pygame.font.init()    w=160.0 #screen width    h=128.0 #height    w_x=w/16 #number of tiles wide screen is    h_y=h/16 #number of tiles high the screen is    print(w_x)    print(h_y)    x_map_pos = 0    y_map_pos =0    map =[0,0,0,0,0,0,0,0,0,0,          0,1,0,0,0,0,0,0,1,0,          0,1,0,0,0,0,0,0,1,0,          0,1,1,0,0,0,0,1,1,0,          0,1,1,1,0,0,1,1,1,0,          0,1,1,1,1,1,1,1,1,0,          0,1,1,1,1,1,1,1,1,0,          0,0,0,0,1,1,0,0,0,0]           screen = pygame.display.set_mode((int(w),int(h)))    background_tile, background_tile_rect = load_image(snow_file_name,-1)    pygame.display.set_caption("Trial")    pygame.mouse.set_visible(0)    clock=pygame.time.Clock()    background = pygame.Surface(screen.get_size())    background = background.convert()        i=0    screen.blit(background, (0,0))    while y_map_pos < h_y:        x_map_pos = 0        while x_map_pos < w_x:            if map > 0:                screen.blit(background_tile, (x_map_pos*16,y_map_pos*16))            x_map_pos = x_map_pos + 1            i = i + 1        y_map_pos = y_map_pos + 1        pygame.display.flip() #background is now displayed    snow = Snow() #sprite instance    allsprites = pygame.sprite.RenderPlain((snow)) #container            #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        allsprites.update() #calls update function of all sprites               screen.blit(background, (0,0)) #redraw background        i=0        y_map_pos = 0        while y_map_pos < h_y:            x_map_pos = 0            while x_map_pos < w_x:                if map > 0:                    screen.blit(background_tile, (x_map_pos*16,y_map_pos*16))                x_map_pos = x_map_pos + 1                i = i + 1            y_map_pos = y_map_pos + 1        allsprites.draw(screen) #redraw sprites        pygame.display.flip() #show new screen        if __name__=="__main__": main()


It is pretty straightforward (still a bit ugly). First I define the map. Then I step through the map and increment my x position on the screen by 16 for each tile in the map. After I finish a row I increment my y position and start again.
I blit the 16x16 background_tile for every 1 in the map.

There is some ugliness going on here. The loop where I step through the map needs to be a function. Also the map is hard coded (grrrr) as is the tile size. I would also rather get the background sprites to the screen using a sprite class and its update function but I am having a bit of trouble with this. There should be some way to use one of the handy sprite containers to hold an array of these or something.

My next step is to clean up this code and then to load the map from a file.

Making large maps is cumbersome so I should be able to make up some way of reversing this process where I can draw the map in another program store it to a file and open it with this one. Obviously I will have to figure out how to snap to a tile edge for the drawing program. Another option would be to draw it randomly.
Previous Entry Now with sprites
0 likes 1 comments

Comments

a_insomniac
Where are the screenshots :)
November 25, 2007 09:52 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement