Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Ilmiont   Members   -  Reputation: 195

Like
0Likes
Like

Posted 14 December 2013 - 09:45 AM

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

Sponsor:

#2 mousetail   Members   -  Reputation: 994

Like
2Likes
Like

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[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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS