Question

Here is what I'm doing now, and I know there must be a better way, been looking for days. What i did is basically force check through if/elif where new rows began. The 'imgblock' is a 16x16 pixel sprite and the screen holds a 16x10 grid of them. Below in the lolgrid() part I have def for the image name, and go though with each block in the grid I want it to show up. Can anyone please give me some info on how I can make this more elegant? I've only been at this for a week and I'd really appreciate it experienced insight on my bad attempt here.

Thanks a ton

import pygame, sys, os, random
from pygame.locals import *

pygame.init()

#print help(pygame.transform)

resmulti=3
spritesize=16*resmulti
resolution=(256*resmulti,224*resmulti)
framerate=60

## OBJECTS ##
clock=pygame.time.Clock()
screen=pygame.display.set_mode(resolution)

## TEST STUFF ##  
test = [random.randint(1,10) for i in range(20)]
print test


## MAIN ##      
def pngLoad(imgloc, size, flipx, flipy):
    img = pygame.image.load(imgloc).convert_alpha()
    if size > 1:
        #pygame.transform.scale(Surface, (width, height), DestSurface = None): return Surface
        img = pygame.transform.scale(img, (img.get_width()*size, img.get_height()*size))
    if flipx == 1:
        #pygame.transform.flip(Surface, xbool, ybool)
        img = pygame.transform.flip(img, True, False)
    if flipy == 1:
        img = pygame.transform.flip(img, False, True)

    return img

################################################
# GRID = 16x16 pixel blocks. 16x10.5 block grid
################################################

def lolGrid(imgBlock,*gridz):
    #print list(gridz)
    Grid = range(177)

    for i in gridz:
        if i < 16:
            Grid[i] = screen.blit(imgBlock, (i*spritesize,resmulti*56))
        elif i > 15 and i < 32:
            Grid[i] = screen.blit(imgBlock, ((i-16)*spritesize,spritesize+resmulti*56))
        elif i > 31 and i < 48:
            Grid[i] = screen.blit(imgBlock, ((i-32)*spritesize,spritesize*2+resmulti*56))
        elif i > 47 and i < 64:
            Grid[i] = screen.blit(imgBlock, ((i-48)*spritesize,spritesize*3+resmulti*56))
        elif i > 63 and i < 80:
            Grid[i] = screen.blit(imgBlock, ((i-64)*spritesize,spritesize*4+resmulti*56))  
        elif i > 79 and i < 96:
            Grid[i] = screen.blit(imgBlock, ((i-80)*spritesize,spritesize*5+resmulti*56))
        elif i > 95 and i < 112:
            Grid[i] = screen.blit(imgBlock, ((i-96)*spritesize,spritesize*6+resmulti*56))            
        elif i > 111 and i < 128:
            Grid[i] = screen.blit(imgBlock, ((i-112)*spritesize,spritesize*7+resmulti*56))            
        elif i > 127 and i < 144:
            Grid[i] = screen.blit(imgBlock, ((i-128)*spritesize,spritesize*8+resmulti*56))            
        elif i > 143 and i < 160:
            Grid[i] = screen.blit(imgBlock, ((i-144)*spritesize,spritesize*9+resmulti*56))
        elif i > 159 and i < 176:
            Grid[i] = screen.blit(imgBlock, ((i-160)*spritesize,spritesize*10+resmulti*56))

############  
#LOL IMAGES
############        
tanback = pngLoad("tanback.png", resmulti,0,0)
longblackback = pngLoad("blackbacklong.png", resmulti,0,0)
link1 = pngLoad("link1.png", resmulti,1,0)
shrub1 = pngLoad("shrub_01.png", resmulti,0,0)
grnwall1 = pngLoad("greenwall_01.png", resmulti,0,0)
grnwall2 = pngLoad("greenwall_02.png", resmulti,0,0)
grnwall3 = pngLoad("greenwall_03.png", resmulti,0,0)

#background = pngLoad("back.png")
#mous = pngLoad("link001.png")


#bif="back.png"
#mif="link001.png"
#background=pygame.image.load(bif).convert_alpha()
#mous=pygame.image.load(mif).convert_alpha()

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

    screen.blit(tanback,(0,0))
    screen.blit(longblackback,(0,resmulti*3*-56))
    #screen.fill((255,218,144), rect=None, special_flags=0)

    lolGrid(grnwall1,0,1,16,17,32,33,48,112,128,129,144,145,160,161)
    lolGrid(grnwall2,49,64)
    lolGrid(grnwall3,96,113)
    lolGrid(shrub1,2, 4, 6, 9, 11, 13, 15,18,20,22,25,27,29,31,54,57,59,61, 66, 68,86,89,91,98,100,118,121,123,125,146,147,148,149,150,151,152,153,154,155,156,157,158,159,162,163,164,165,166,167,168,169,170,171,172,173,174,175)

    print clock.get_fps()
    clock.tick(framerate)

    pygame.display.update()
Was it helpful?

Solution

D'uh? How about using division, multiplication and module?

Instead of:

for i in gridz:
    if i < 16:
        Grid[i] = screen.blit(imgBlock, (i*spritesize,resmulti*56))
    elif i > 15 and i < 32:
        Grid[i] = screen.blit(imgBlock, ((i-16)*spritesize,spritesize+resmulti*56))
    ...

Just do:

for i in gridz:
     Grid[1] = screen.blitimgBlock(imgBlock, (i % 16) * spritesize, spritesize+resmulti*56))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top