Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Saving sprites in a list  (Read 5063 times)

0 Members and 1 Guest are viewing this topic.

maoam

  • Newbie
  • *
  • Posts: 3
    • View Profile
[Solved] Saving sprites in a list
« on: June 30, 2013, 08:17:48 pm »
I'm new to python and I'm trying to make a list of sprites, all with the same texture but with different positions. I try and append the sprites with new positions onto my list, but all the sprites in the list end up having the same position. Any help?

def stageLoader():
    try:
        texture = sf.Texture.from_file("Brown Block.png")
    except:
        exit(1)
    tileSprite = sf.Sprite(texture)
    spriteMap = []

    for xtile in range(8):
        for ytile in range(4):
            tileSprite.position = (xtile*100, ytile*170)
            spriteMap.append(tileSprite)
    return spriteMap

stageMap = stageLoader()
for tile in range(len(stageMap)):
        print(stageMap[tile].position)
 

My output is this:
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
700.0x, 510.0y
« Last Edit: November 04, 2013, 10:14:30 am by Sonkun »

Benoit87

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Saving sprites in a list
« Reply #1 on: July 01, 2013, 10:28:02 am »
It's a classic error when you have not learn how to use objects in Python correctly  ::)
You must know that in Python, when you do this :
>>> list = []
>>> print(list)
[]
>>> list2 = list
>>> list.append(5)
>>> print(list)
[5]
>>> print(list2)
[5]

Both lists are the same.
It's exacly what happen when you append your list with the sprite. You modify the position of the same sprite always.

This is a solution :
import sfml as sf

def stageLoader():
    texture = sf.Texture.from_file("Brown Block.png")
    spriteMap = []

    for xtile in range(8):
        for ytile in range(4):
            tileSprite = sf.Sprite(texture)
            tileSprite.position = (xtile*100, ytile*170)
            spriteMap.append(tileSprite)

    return spriteMap

stageMap = stageLoader()
for tile in range(len(stageMap)):
        print(stageMap[tile].position)
I create a new object each time (with the same name, but it's still a new instance).

And remove your "try: ... except: ..." if you got an error in this block, how can you know that ? You'll got an error farther, and you'll don't know that it's just here.

Have a nice day !
« Last Edit: July 01, 2013, 10:30:07 am by Benoit87 »

maoam

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Saving sprites in a list
« Reply #2 on: July 01, 2013, 01:02:16 pm »
Ahh, I understand. Thanks for the help, it works now