November 27th, 2013, 06:31 PM
Getting things to overlap properly in Pygame
I am working on a board game right now. The pieces hop about the board as player roll dice. Pretty simple concept.
The way I have it coded now, I have 2 lists of coordinates to plot where on the board the pieces are supposed to go (it is a very curvy board, so a simple grid won't work).
The first list is for the first piece that lands in that square, and the second list is for the second piece that lands in that square. If more than 2 pieces try to land in that square, the dice roller rolls again to put the other pieces somewhere else (mostly because the square doesnt have enough room to fit more than 2 pieces without looking crowded).
My problem is as the pieces move about the board, sometimes they overlap each other. I would like to get the pieces to blit in the order of their y values (lowest values first) so that when they overlap the pieces closest to the viewer are on top. I tried making a list of the characters and sorting it, but for some reason it doesnt blit properly (and I suspect it has something to do with having 2 different lists to sort through). Sometimes it overlaps fine. sometimes the top stuff is on bottom and so on.
Anyways, I was wondering if there was an easy way to blit things in order of their Y value to get it to do what I am trying to do.
November 28th, 2013, 08:48 AM
Well there is a way to do it you might find easier, but it depends if you are using classes or not (you should be).
If you are using classes to represent your pieces you can have them inherit from pygame.sprite.Sprite.
Then you place all your pieces in a sprite group.
The group in this case should be pygame.sprite.LayeredUpdates().
Then every frame you can change their blit layer with pygame.sprite.LayeredUpdates.change_layer()
Sorting them by y-coordinate in your own lists could work too if you do it correctly.
Share your code,
November 30th, 2013, 02:08 AM
I am using classes, but havent done anything with pygame sprites yet, so still trying to figure that out. Will have to study that up.
I actually finally figured out what I was doing wrong right now while I was about to post my code. I mixed around 2 of my variables in my sort loop (actually put the same variable twice instead of 2 different ones) and so it was doing all sorts of weird things. It works now so I will use what I have. Thanks for helping out though! I will definitely keep the layered updates thing in mind for my next project as it sounds like it will be easier to use.