Python Forum
[PyGame] Simple code for isometric 2D games
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Simple code for isometric 2D games
#1
Thumbs Up 
Hi all!
I haven't found any posts about this so I'm posting a simple code to create your own 2D isometric surface.

Hope you´ find it useful! ;)

[Image: HQWpzad.png]

the needed assets attached

and the code:
import pygame
from pygame.locals import *
import sys

pygame.init()

DISPLAYSURF = pygame.display.set_mode((640, 480), DOUBLEBUF)    #set the display mode, window title and FPS clock
pygame.display.set_caption('Map Rendering Demo')
FPSCLOCK = pygame.time.Clock()

map_data = [
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1]
]               #the data for the map expressed as [row[tile]].

wall = pygame.image.load('wall.png').convert_alpha()  #load images
grass = pygame.image.load('grass.png').convert_alpha()

TILEWIDTH = 64  #holds the tile width and height
TILEHEIGHT = 64
TILEHEIGHT_HALF = TILEHEIGHT /2
TILEWIDTH_HALF = TILEWIDTH /2

for row_nb, row in enumerate(map_data):    #for every row of the map...
    for col_nb, tile in enumerate(row):
        if tile == 1:
            tileImage = wall
        else:
            tileImage = grass
        cart_x = row_nb * TILEWIDTH_HALF
        cart_y = col_nb * TILEHEIGHT_HALF  
        iso_x = (cart_x - cart_y) 
        iso_y = (cart_x + cart_y)/2
        centered_x = DISPLAYSURF.get_rect().centerx + iso_x
        centered_y = DISPLAYSURF.get_rect().centery/2 + iso_y
        DISPLAYSURF.blit(tileImage, (centered_x, centered_y)) #display the actual tile

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()

    pygame.display.flip()
    FPSCLOCK.tick(30)
Source: with the help of https://stackoverflow.com/questions/2062...-in-python
Josselin likes this post

Attached Files
Image(s)
       
Reply
#2
Thanks for posting this! It helped with my implementation here:
https://python-forum.io/Thread-split-How...#pid131053
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Isometric game pygame Tiled howardberger 1 478 Jan-31-2024, 10:01 PM
Last Post: deanhystad
  [PyGame] Isometric Movement on Tiled Map Josselin 0 2,325 Nov-02-2021, 06:56 AM
Last Post: Josselin
  [split] How to display isometric maps with pygame? mattwins 6 6,143 Nov-17-2020, 12:54 PM
Last Post: mattwins
  How to display isometric maps with pytmx? Piethon 18 9,703 Feb-19-2020, 04:33 PM
Last Post: nilamo
  Simple Code, "Video System not Initialized" DaxDaTrade 2 13,659 May-25-2018, 01:07 PM
Last Post: MegasXLR
  Fun games to help with Low_Ki_ 2 4,050 May-10-2017, 01:23 AM
Last Post: Low_Ki_

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020