How to render a 2D isometric world in Python/Pygame,
Members - Reputation: 211
Posted 14 December 2013 - 09:45 AM
I'm wondering how to render a 2D isometric world in Python/Pygame.
I know the theory of the isometric view because I found a decent tutorial online but it got complicated when it came to actually displaying the tiles and wasn't written in Python although I did understand the majority of the C++ used and the pseudocode.
Therefore I understand how to convert cartesian coords to isometric ones (x = x - y, y = (x + y) / 2) but am not sure how to relate this to Python and then render to the screen in Pygame, preferably as sprites.
Any help muchly appreciated.
Thanks in advance,
Crossbones+ - Reputation: 2509
Posted 15 December 2013 - 04:27 AM
The code I used for my game is like this:
import math,pygame class Square(object): def __init__(self, image, gridpos,level=0): self.image=image self.gridpos=gridpos self.isopos=[int(i*16) for i in gridpos] self.recalculateRealpos() self.level=level def draw(self, surface): pos=(self.realpos+self.offset+surface.get_width()/2, self.realpos+self.offset) surface.blit(self.image,pos) def update(self,*args): pass def recalculateRealpos(self): self.realpos=(int((self.isopos-self.isopos)*2), int((self.isopos+self.isopos))) self.offset=(-self.image.get_width()/2,-self.image.get_height()-2*self.isopos+48) def __gt__(self, other): if math.ceil(self.realpos/16.0)!=math.ceil(other.realpos/16.0): return self.realpos>other.realpos else: return self.level>other.level def __lt__(self, other): if math.ceil(self.realpos/16.0)!=math.ceil(other.realpos/16.0): return self.realpos<other.realpos else: return self.level<other.level
All you need to do is sort your list of objects and it looks fine. The system also supports relative scrolling, though it got to slow for me with larger rooms. Note that my tile size here was 64*64 and you might need to switch up the numbers if you have different sizes or angles. You can download the source for my game if you want to see how it is used (see my signature)
I am not sure if this is what you are looking for, but I hope it helps.
never mind my signature, I haven't updated it since 2016