isometric tiles

This commit is contained in:
StochasticMouse 2024-07-12 16:56:47 +02:00
parent 4b2a7fc721
commit 338e24357f
15 changed files with 34 additions and 14 deletions

BIN
data/images/tiles/iso/0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 301 B

BIN
data/images/tiles/iso/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 B

BIN
data/images/tiles/iso/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 B

BIN
data/images/tiles/iso/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 323 B

BIN
data/images/tiles/iso/4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 315 B

BIN
data/images/tiles/iso/5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 324 B

BIN
data/images/tiles/iso/6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 328 B

BIN
data/images/tiles/iso/7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 B

11
game.py
View File

@ -21,10 +21,11 @@ class Game:
self.player = Entity(self, 'player', (50.0,200.0), (8, 15)) self.player = Entity(self, 'player', (50.0,200.0), (8, 15))
self.assets = { self.assets = {
'player' : load_image('entities/player.png'), 'player' : load_image('entities/player.png'),
'grass' : load_images('tiles/grass/'), #'grass' : load_images('tiles/grass/'),
'stone' : load_images('tiles/stone/') #'stone' : load_images('tiles/stone/')
'iso' : load_images('tiles/iso/')
} }
self.tilemap = Tilemap(self, (32,32), 'map_1.csv') self.tilemap = Tilemap(self, (64,32), 'map_1.csv')
def run(self): def run(self):
self.status = True self.status = True
while self.status: while self.status:
@ -39,9 +40,9 @@ class Game:
if keys[pygame.K_RIGHT]: if keys[pygame.K_RIGHT]:
delta[0] += 1 delta[0] += 1
if keys[pygame.K_UP]: if keys[pygame.K_UP]:
delta[1] -= 1 delta[1] -= 0.5
if keys[pygame.K_DOWN]: if keys[pygame.K_DOWN]:
delta[1] += 1 delta[1] += 0.5
self.player.update(delta) self.player.update(delta)
self.tilemap.render(self.display) self.tilemap.render(self.display)
self.player.render(self.display) self.player.render(self.display)

View File

@ -1,5 +1,6 @@
import pygame import pygame
import numpy as np import numpy as np
from scripts.utils import to_isometric_pixel
class Entity: class Entity:
def __init__(self, game, e_type, pos, size): def __init__(self, game, e_type, pos, size):

View File

@ -1,6 +1,6 @@
import pygame import pygame
import numpy as np import numpy as np
from scripts.utils import TILE_DICT from scripts.utils import TILE_DICT, to_isometric
BASE_MAP_PATH = 'data/maps/' BASE_MAP_PATH = 'data/maps/'
@ -22,6 +22,6 @@ class Tilemap:
for x in range(self.tilemap.shape[0]): for x in range(self.tilemap.shape[0]):
for y in range(self.tilemap.shape[1]): for y in range(self.tilemap.shape[1]):
tile = self.tilemap[x,y] tile = self.tilemap[x,y]
surface.blit(self.game.assets[tile['type']][tile['variant']],tile['pos'] * self.tile_size) surface.blit(self.game.assets[tile['type']][tile['variant']],to_isometric(tile['pos'],self.tile_size))
for tile in self.offgrid_tiles: for tile in self.offgrid_tiles:
surface.blit(self.game.assets[tile['type']][tile['variant']],tile.pos) #this tile is written offgrid so no mult by tile_size surface.blit(self.game.assets[tile['type']][tile['variant']],to_isometric(tile['pos'],self.tile_size)) #this tile is written offgrid so no mult by tile_size

View File

@ -1,12 +1,13 @@
import pygame import pygame
import os import os
import numpy as np
TILE_DICT = { TILE_DICT = {
1 : ('grass',0), 1 : ('iso',0),
2 : ('stone',0), 2 : ('iso',1),
3 : ('grass',1), 3 : ('iso',2),
4 : ('stone',1), 4 : ('iso',3),
5 : ('grass',2), 5 : ('iso',4),
6 : ('stone',2) 6 : ('iso',5)
} }
BASE_IMG_PATH = 'data/images/' BASE_IMG_PATH = 'data/images/'
def load_image(path): def load_image(path):
@ -19,3 +20,20 @@ def load_images(path):
for img_name in sorted(os.listdir(BASE_IMG_PATH + path)): for img_name in sorted(os.listdir(BASE_IMG_PATH + path)):
images.append(load_image(path + '/' + img_name)) images.append(load_image(path + '/' + img_name))
return images return images
def to_isometric(coord, tilesize):
#(1,0) -> (+tilesize[0]/2,+tilesize[1]/2)
#(0,1) -> (-tilesize[0]/2,+tilesize[1]/2)
#a must be (+tilesize[0]/2,-tilesize[0]/2)
# (+tilesize[1]/2,+tilesize[1]/2)
a = np.array([[1,-1],[1,1]])
a = 0.5 * a * tilesize[:,np.newaxis]
return np.squeeze(np.matmul(a,coord[:,np.newaxis]))
def to_cartesian(coord, tilesize):
#manually calculated the inverse of a in to_isometric
a = np.array([[1,1],[-1,1]])
a = a * (1/tilesize)
return np.squeeze(np.matmul(a,coord[:,np.newaxis]))
def to_isometric_pixel(coord):
a = np.array([[1,-1],[1,1]])
a = 0.5 * a
return np.squeeze(np.matmul(a,coord[:,np.newaxis]))