0

I have a data structure I created myself, it's called characters.

This data structure has the following method

addCharacters(self, Character, number)

This allows the user to add as many characters to the screen. In this method, an array initialized in the __init__ constructor is appended to add the Character object.

I have a class screen, in this class I have the following method

addCharacter(self, Character, x, y)

this draws the character on the screen like so:

self.screen.blit(character.image,(x,y)

I call these methods in the main class where the code is run. a while loop to represent the game running is created. in this while loop the objects must be drawn, so screen.blit must happen in the while loop.

Here is the problem: when I call the method myarray.addCharacter(character,4) outside the while loop, and

for characters in charactersList:
     screen.addCharacter(character, random.randrange(0, screen.width), random.randrange(0,   world.screen))

The image erratically blits on the screen at different locations. I understand this is because of the for loop inside the while loop. I can't add the character outside the while loop as it won't draw to the screen.

Is there a way I can set the location of each object in this data structure all at a different position without constant blitting?

Thanks in advance!

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Pro-grammer
  • 862
  • 3
  • 15
  • 41
  • I want all characters at random locations Could you show me an example please? – Pro-grammer Dec 25 '13 at 22:23
  • BTW, is your data structure really called `characters`? If so, there might be another problem: After your `for` loop, that name will be bound to the last item from the `characterList` iterable and the class will no longer be available. – tobias_k Dec 26 '13 at 11:19
  • you explained it well, thanks I manged to do it:) – Pro-grammer Dec 29 '13 at 11:41

1 Answers1

1

Not sure I understood the question correctly. As I understand it, you want to draw your "characters" at random positions of the screen, but those positions have to stay the same in each iteration of the outer while loop. I see two possibilities of doing this:

  1. Either, add position attributes x and y to the Character class itself, if possible, or
  2. use a dictionary for mapping Character objects to their (x, y) position.

I don't know what that Character class is (your own class, or from some library?), so the first may not be possible. For the second approch, Character objects need to be hashable. If that's not possible, too, you might be able to create a hashable wrapper class, or just use the object's id for the mapping.

Code for approach 2 might look somewhat like this:

# create dictionary, defaulting to new random positions
randpos = lambda: (random.randrange(0, width), random.randrange(0, height))
d = collections.defaultdict(randpos)

for i in range(10):             # corresponds to your outer 'while' loop
    print "outer loop", i
    for k in range(5):          # your 'for characters...' loop
        x, y = d[k]             # get x, y from dict, default to random position
        print x, y              # use position
Community
  • 1
  • 1
tobias_k
  • 81,265
  • 12
  • 120
  • 179