April 27th, 2013, 08:51 AM
SOLVED - How to Sort a sprite.Group with pygame?
I have a list with Zombie obj these zombies have a y cord. To get them drawn properly by pygame i fill the list in that order that the lowest y cord goes in first. Then they are properly dawn. (generateWave() Method will do this)
So now to spice things up i want to make this random. With x cords this is no problem but with the y cords it is.
I made a list with available cords so that the zombies are not too close together, but they should or could overlap a bit, as long at the zombie with the higher y cords gets drawn last. To draw him last the list with the zombies in it should be sorted so that the first one in the list has the lowest y cords who is available.
So all this is done and it should work. The list gets sorted after adding the amount of zombies but they are still drawn like i did not sort them.
I think this has to do with the sprite group and they are not sorted in the sprite group. How can i sort a sprite group with python ?
Is this correct or could be error elsewhere?
How can i do it ?
Code is here: http://www.fast-files.com/getfile.aspx?file=62120
Thanks for help
Last edited by ManuelNewbie; April 27th, 2013 at 10:00 AM.
Reason: Solved the problem
April 27th, 2013, 09:13 AM
Well I can't download your file. It takes me to an error page.
But, it does sound like this is an issue with using sprite groups. The standard pygame.sprite.Group.update() function updates the members in an arbitrary order.
You could look into using pygame.sprite.OrderedUpdates. This will render the sprites in the order in which they were added to the group, but this could be a pain for you too. There is also a pygame.sprite.LayeredUpdates. This allows you to specify a layer attribute for your sprite (or add to group with 'layer' kwarg). This might work for you.
Honestly I wouldn't do it this way. I personally feel that the pygame classes sprite.Sprite and sprite.Group are useless. They really don't do anything that you can't do yourself.
If you create your own sprite class and add these sprites to a list which is drawn in the order it appears, there is no problem.
If you can get that download working or you can post your code here (depending on its length) I will take a closer look.
Comments on this post
Last edited by Mekire; April 27th, 2013 at 09:19 AM.
Reason: Added info on sprite.LayeredUpdates
April 27th, 2013, 09:59 AM
Thanks for the fast replay.
Yes i looked into the pygame.sprite.OrderedUpdates but that does not help, because i found out when the object instance is created it gets added to the sprite group and the sorting in the other list does do nothing because it is already added the wrong way to the sprite group.
I solved this by not adding it to the sprite group automaticly. Now i add it by hand and all is solved now i think. I feel a bit stupid tho that i did not think of this earlier...
I added now the random generation of enemys and it is a nice game now. It works quite well for the moment but is not balanced.
You can have a look at my solution here or just give it a play:
Thanks for the suggestion with the own sprite class i will look into it.
April 27th, 2013, 10:43 AM
OK so, visually I like it quite a bit, but I have to say in terms of code you have a lot of work to do. Global variables are not the answer. You are going to look back at that code later in life and cry. Also pygame is completely capable of handling all your timing needs for a program of this type; there is no need for the threading.
In terms of classes there is no need for setter/getters in python. Attributes are not private; they can be modified directly. Also pretty much everything that isn't either a Global CONSTANT or below a line that says:
should be in a class (or at the very least a part of a function).
if __name__ == "__main__":
A couple specific details that directly impact the user; enchant isn't part of the standard library; you should note that your program requires it if you haven't done so somewhere. Also your program doesn't properly process pygame.QUIT events. You placed a break statement before the pygame.quit()/sys.exit() so it never actually runs them and the program hangs.
Anyway, I do really like the look of the game. The most important thing you should really consider is your use of global variables. If you find yourself thinking you need one, or you find yourself considering using the global keyword for any need other than declaring a new global CONSTANT within a function, you really need to step back and ask yourself what you are doing wrong.
Sorry to sound hyper-critical. The first game I wrote was rampant with the same exact problems,
April 27th, 2013, 11:48 AM
I like to learn more and appreciate your help ver much.
I thought it is considered bad style if you would modify vars direly. (At least it was when i learned Java in school)
I thought globals are needed so that every object of a class can access the same value of a var?
It is good to know that others also started out like this. :-D
April 27th, 2013, 12:15 PM
Anything that is declared in the global scope can be accessed by any function or class without using the global keyword. Any such things should be CONSTANTS and should follow the naming convention of being written in all CAPS so that when you see one, you don't have to spend twenty minutes figuring out where it came from.
Global variables should not be used. Functions should be passed the values they need and return the values you need to retain. Classes and Object Oriented programming allow this to be done in the most effective way. Instances of classes retain their information; you need to take advantage of this.
I think you need to think bigger with some of your classes. A stage should be a class. It would have attributes like lists of current zombies and similar. Think in objects. Ask yourself what objects go inside other objects. If I have an instance of a class SteeringWheel, it probably should be an attribute inside a class Car.
Before you even code think about what classes you want. Perhaps draw a diagram so you can visualize what classes you need and how they would communicate with other classes.
Anyway, explaining this in words is difficult. Here is one of my programs if you would like to take a look. It was my first game and was originally written as badly as yours if not worse. I just recently rewrote the whole thing:
I'm sure in a couple years I will look back at it again and be disgusted at how terrible I was now. That's the name of the game,
April 27th, 2013, 12:27 PM
I tried to put some of the stuff in defines and then it always complains that it some var is not assigned yet. This is the exact same reason why i use globals because the defs seem not to know where the other vars are defined outside of themselfes...
How can i fix that ?
Broken code here will not work: http://www.sendspace.com/file/kx1a1y
Yes that idea with the diagrams is a good one. I might have to redo the whole thing but that has to wait got some exams coming up....
April 27th, 2013, 07:38 PM
Wow! dcross is fun. Great soundtrack.
[/code] are essential for python code and Makefiles!
April 27th, 2013, 10:20 PM
Thanks. Still one of my favorites even though it was my first.
Originally Posted by b49P23TIvg