• Advertisement
Sign in to follow this  

How to render a 2D isometric world in Python/Pygame

This topic is 1501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

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,
Ilmiont

Share this post


Link to post
Share on other sites
Advertisement

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[0]+self.offset[0]+surface.get_width()/2,
             self.realpos[1]+self.offset[1])
        surface.blit(self.image,pos)
    def update(self,*args): pass
    def recalculateRealpos(self):
        self.realpos=(int((self.isopos[0]-self.isopos[1])*2),
                      int((self.isopos[0]+self.isopos[1])))
        self.offset=(-self.image.get_width()/2,-self.image.get_height()-2*self.isopos[2]+48)
    def __gt__(self, other):
        if math.ceil(self.realpos[1]/16.0)!=math.ceil(other.realpos[1]/16.0):
            return self.realpos[1]>other.realpos[1]
        else:
            return self.level>other.level
    def __lt__(self, other):
        if math.ceil(self.realpos[1]/16.0)!=math.ceil(other.realpos[1]/16.0):
            return self.realpos[1]<other.realpos[1]
        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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement