Error:
File "C:\Users\usuario\workspace\Pokemon\pokemon.py", line 184, in main
image = tiled_map.get_tile_image(player.x, player.y, tiled_map.get_layer_by_name("objetos"))
File "C:\Python27\lib\site-packages\pytmx\pytmx.py", line 446, in get_tile_image
layer = self.layers[layer]
TypeError: list indices must be integers, not TiledObjectGroup
Código:
def main():
pg.init()
FPSClock = pg.time.Clock()
DISPLAYSURFACE = pg.display.set_mode((WIDTHWINDOW,HEIGHTWINDOW))
pg.display.set_caption("Pokemon")
#backGround = Background('./map/5.png', [0,0])
backGround = Map('map/pokemon01.tmx')
tiled_map = pytmx.TiledMap('map/pokemon01.tmx')
map_img = backGround.make_map()
t=0
player = Player([int(WIDTHWINDOW/2), 100])
vx = 0
vy = 0
vel=2
obstacles_list = []
rightIsPressed,leftIsPressed,upIsPressed,downIsPressed = False,False,False,False
isMoving = False
orientations = [0]
# fill obstacle_list with obstacles objects
for tile_obj in backGround.tmx_data.objects:
obst = Obstacle(tile_obj.x, tile_obj.y, tile_obj.width, tile_obj.height)
obstacles_list.append(obst)
while True:
for event in pg.event.get():
if event.type==pg.QUIT:
pg.quit()
sys.exit()
if event.type==pg.KEYDOWN:
if event.key==pg.K_DOWN:
downIsPressed=True
vy=vel
vx=0
player.orientation = 0
if event.key==pg.K_UP:
upIsPressed=True
vy=-vel
vx=0
player.orientation = 1
if event.key==pg.K_LEFT:
leftIsPressed=True
vx=-vel
vy=0
player.orientation = 3
if event.key==pg.K_RIGHT:
rightIsPressed=True
vx=vel
vy=0
player.orientation = 2
orientations.append(player.orientation)
print(orientations)
if event.type==pg.KEYUP:
if event.key==pg.K_DOWN:
downIsPressed=False
if upIsPressed:
vy=-vel
vx=0
else:
vy=0
if event.key==pg.K_UP:
upIsPressed=False
if downIsPressed:
vy=vel
vx=0
else:
vy=0
if event.key==pg.K_LEFT:
leftIsPressed=False
if rightIsPressed:
vx=vel
vy=0
else:
vx=0
if event.key==pg.K_RIGHT:
rightIsPressed=False
if leftIsPressed:
vx=-vel
vy=0
else:
vx=0
if t>=1:
t=0
else:
t+=1
if rightIsPressed==False and leftIsPressed==False and upIsPressed==False and downIsPressed==False:
isMoving = False
else:
isMoving = True
if isMoving == False:
t=0
image = tiled_map.get_tile_image(player.x, player.y, tiled_map.get_layer_by_name("objetos"))
posx = player.x +1
posy = player.y +1
negx = player.x -1
negy = player.y -1
#if player.rect.collidepoint((image.x, image.y)):
rectToSee1 = pg.Rect(posx, player.y, SPRITEWIDTH, SPRITEHEIGHT)
rectToSee2 = pg.Rect(player.x, posy, SPRITEWIDTH, SPRITEHEIGHT)
rectToSee3 = pg.Rect(negx, player.y, SPRITEWIDTH, SPRITEHEIGHT)
rectToSee4 = pg.Rect(player.x, negy, SPRITEWIDTH, SPRITEHEIGHT)
if rectToSee1.collidepoint(image.x, image.y) or rectToSee2.collidepoint(image.x, image.y) or rectToSee3.collidepoint(image.x, image.y) or rectToSee4.collidepoint(image.x, image.y):
vx=0
vy=0
player.move(vx, vy)
player.actual_image=player.images[player.orientation][t]
#DISPLAYSURFACE.blit(pg.transform.scale(backGround.image, (WIDTHWINDOW, HEIGHTWINDOW)), backGround.rect)
DISPLAYSURFACE.blit(pg.transform.scale(map_img, (WIDTHWINDOW, HEIGHTWINDOW)), map_img.get_rect())
#DISPLAYSURFACE.blit(map_img, map_img.get_rect())
DISPLAYSURFACE.blit(pg.transform.scale(player.actual_image, (SPRITEWIDTH, SPRITEHEIGHT)), player.rect)
FPSClock.tick(30)
pg.display.update()
if __name__ == '__main__':
main()
Pretendo que en image se guarde los objetos del tiledmap tales como arboles, casas, etc. y después, coger la imagen de sprite y compararla con la posición +1, por ejemplo, respecto el sprite hacia el objeto. Si se va en dirección hacia el objeto, que no se mueva. Creo que voy bien, no sé.
Gracias.