1

这就是我现在正在做的事情,我知道必须有更好的方法,一直在寻找几天。我所做的基本上是通过 if/elif 强制检查新行的开始位置。“imgblock”是一个 16x16 像素的精灵,屏幕上有一个 16x10 的网格。在lolgrid() 部分的下面,我有图像名称的def,并且我希望它显示网格中的每个块。谁能给我一些关于如何使它更优雅的信息?我只在这里待了一个星期,我真的很感激它对我在这里的糟糕尝试的洞察力。

万分感谢

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()
4

1 回答 1

0

啊?如何使用除法,乘法和模块?

代替:

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))
    ...

做就是了:

for i in gridz:
     Grid[1] = screen.blitimgBlock(imgBlock, (i % 16) * spritesize, spritesize+resmulti*56))
于 2012-11-29T02:17:44.153 回答