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

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

## 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.
Ilmiont

##### Share on other sites

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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 15
• 21
• 26
• 11