python – pygame sprite的列表丢失了它的第一个元素,并获得了最
发布时间:2020-12-20 11:15:27 所属栏目:Python 来源:网络整理
导读:我有一个函数加载一个精灵表,找到一个精灵块,然后将每个精灵放入一个列表.在将精灵附加到列表中之前,它会将其blit到屏幕上.一旦它完成加载精灵,它将遍历列表,随着它的进行blitting每个精灵.两组blits应该是相同的,但是从列表中删除第一个sprite,并且重复最后
我有一个函数加载一个精灵表,找到一个精灵块,然后将每个精灵放入一个列表.在将精灵附加到列表中之前,它会将其blit到屏幕上.一旦它完成加载精灵,它将遍历列表,随着它的进行blitting每个精灵.两组blits应该是相同的,但是从列表中删除第一个sprite,并且重复最后一个sprite.两组blits看起来像这样:
每个精灵按照它附加到列表的顺序进行blitting,从左到右,从上到下,所以第一个精灵是左上角,最后一个是右下角. 这是加载精灵的函数: def assembleSprites(name,screen): """Given a character name,this function will return a list of all that character's sprites. This is used to populate the global variable spriteSets""" spriteSize = (35,35) spritesheet = pygame.image.load("./images/patchconsprites.png") sprites = [] start = charCoords[name] char = list(start) image = pygame.Surface((35,35)) # load each sprite and blit them as they're added to the list for y in range(5): char[0] = start[0] for x in range(9): rect = (char[0],char[1],char[0]+spriteSize[0],char[1]+spriteSize[1]) image.blit(spritesheet,(0,0),rect) image = image.convert() colorkey = image.get_at((0,0)) image.set_colorkey(colorkey,RLEACCEL) screen.blit(image,(x*40,y*40)) pygame.display.update() sprites.append(image) char[0] += spriteSize[0]+2 char[1] += spriteSize[1]+2 # check that the list was constructed correctly count = 0 for y in range(6,11): for x in range(9): screen.blit(sprites[count],y*40)) count += 1 pygame.display.update() return sprites 有人看到我如何搞砸列表? 解决方法image.blit(spritesheet,rect) 您没有在循环周围重新初始化图像,它仍然是您在上一次迭代中使用的表面,即已在列表中的表面.每次循环循环时,都会覆盖您在上一步中附加到列表中的精灵. 我建议在行之前而不是在循环开始之前抓取一个新的image = pygame.Surface((35,35)). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |