Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create a grid to blit sprites onto?
    primarykey
    data
    text
    <p>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.</p> <p>Thanks a ton</p> <pre><code>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 &gt; 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 &lt; 16: Grid[i] = screen.blit(imgBlock, (i*spritesize,resmulti*56)) elif i &gt; 15 and i &lt; 32: Grid[i] = screen.blit(imgBlock, ((i-16)*spritesize,spritesize+resmulti*56)) elif i &gt; 31 and i &lt; 48: Grid[i] = screen.blit(imgBlock, ((i-32)*spritesize,spritesize*2+resmulti*56)) elif i &gt; 47 and i &lt; 64: Grid[i] = screen.blit(imgBlock, ((i-48)*spritesize,spritesize*3+resmulti*56)) elif i &gt; 63 and i &lt; 80: Grid[i] = screen.blit(imgBlock, ((i-64)*spritesize,spritesize*4+resmulti*56)) elif i &gt; 79 and i &lt; 96: Grid[i] = screen.blit(imgBlock, ((i-80)*spritesize,spritesize*5+resmulti*56)) elif i &gt; 95 and i &lt; 112: Grid[i] = screen.blit(imgBlock, ((i-96)*spritesize,spritesize*6+resmulti*56)) elif i &gt; 111 and i &lt; 128: Grid[i] = screen.blit(imgBlock, ((i-112)*spritesize,spritesize*7+resmulti*56)) elif i &gt; 127 and i &lt; 144: Grid[i] = screen.blit(imgBlock, ((i-128)*spritesize,spritesize*8+resmulti*56)) elif i &gt; 143 and i &lt; 160: Grid[i] = screen.blit(imgBlock, ((i-144)*spritesize,spritesize*9+resmulti*56)) elif i &gt; 159 and i &lt; 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() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload