Début de gestion et de rendu de map

This commit is contained in:
Yannis 2023-12-22 17:09:17 +01:00
parent e020fd2b4c
commit 6849ed2de4
Signed by: yannis
SSH key fingerprint: SHA256:Bz8K8QiTYUudf8MlthTM9MCLfgiYf/U1md3V9g9Wo14
6 changed files with 142 additions and 1 deletions

BIN
assets/tiles.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

78
maps/map0.tmj Normal file
View file

@ -0,0 +1,78 @@
{ "compressionlevel":-1,
"height":40,
"infinite":true,
"layers":[
{
"chunks":[
{
"data":[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1,
1, 1, 1, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 1, 1,
1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 1, 2, 3, 1, 1, 1,
1, 1, 1, 3, 2, 1, 1, 1, 1, 1, 2, 3, 1, 1, 1, 1,
1, 1, 1, 3, 2, 1, 1, 1, 1, 2, 3, 1, 1, 1, 1, 1,
1, 1, 1, 3, 2, 1, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1,
1, 1, 1, 3, 2, 1, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 3, 2, 1, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 3, 2, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 3, 2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
"height":16,
"width":16,
"x":0,
"y":0
},
{
"data":[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2,
2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2,
2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2,
2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 3, 3, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
"height":16,
"width":16,
"x":16,
"y":0
}],
"height":16,
"id":1,
"name":"Calque de Tuiles 1",
"opacity":1,
"startx":0,
"starty":0,
"type":"tilelayer",
"visible":true,
"width":32,
"x":0,
"y":0
}],
"nextlayerid":2,
"nextobjectid":1,
"orientation":"orthogonal",
"renderorder":"right-down",
"tiledversion":"1.8.4",
"tileheight":16,
"tilesets":[
{
"firstgid":1,
"source":"..\/..\/..\/Documents\/mapping\/6 NSI RPG\/tiles.tsx"
}],
"tilewidth":16,
"type":"map",
"version":"1.8",
"width":100
}

5
src/camera.py Normal file
View file

@ -0,0 +1,5 @@
class Camera:
def __init__(self):
self.x = 0
self.y = 0
self.zoom = 1.

View file

@ -1,4 +1,5 @@
from src.event_handler import EventHandler
from src.map_manager import MapManager
from src.renderer import Renderer
import pygame
@ -8,9 +9,17 @@ class Engine:
def __init__(self):
# L'initialisation de Pygame est nécéssaire pour tous les modules
pygame.init()
self.clock = pygame.time.Clock()
self.running = False
self.renderer = Renderer(self)
self.event_handler = EventHandler(self)
self.map_manager = MapManager()
self.map_manager.load_new("maps/map0.tmj")
self.renderer.load_tile_set("assets/tiles.png", 16)
def loop(self):
"""Fonction à lancer au début du programme et qui va lancer les updates dans une boucle.
@ -18,6 +27,7 @@ class Engine:
self.running = True
while self.running:
self.update()
self.clock.tick(60.)
def update(self):
"""Fonction qui regroupe toutes les updates des composants. Elle permet de mettre à jour le jeu quand on

21
src/map_manager.py Normal file
View file

@ -0,0 +1,21 @@
import json
class MapManager:
"""Stocke les cartes du jeu."""
def __init__(self):
# structure : [layers[chunks[x, y]{width, height, data [tiles id]}]
self.map_layers = []
def load_new(self, file_path: str):
"""Charge une map infinie au format tiled."""
with open(file_path, "r") as file:
data = json.loads(file.read())
for layer in data["layers"]:
self.map_layers.append(layer["chunks"])
print(self.map_layers)
def get_tile_at(self, x: int, y: int, layer: int):
"""Done l'id de la tile aux coordonnées données et à la couche choisie."""
return

View file

@ -1,13 +1,40 @@
from pygame import display
from pygame import display, image
import src.engine as engine
class Renderer:
"""Classe contenant le moteur de rendu. On utilise, pour cela la bibliothèque Pygame."""
def __init__(self, core: 'engine.Engine'):
self.engine = core
self.window = display.set_mode((600, 600))
self.tiles = []
def load_tile_set(self, file_path: str, tile_size: int):
"""Charge le jeu de tuiles en utilisant le fichier donné et la taille donnée."""
tile_set = image.load(file_path).convert_alpha()
# Scan tout le tile set et le découpe pour créer des tiles de {tile_size} px de hauteur et de largeur
for y in range(tile_set.get_height()//tile_size):
for x in range(tile_set.get_width()//tile_size):
tile = tile_set.subsurface((x*tile_size, y*tile_size, tile_size, tile_size))
self.tiles.append(tile)
print(self.tiles)
def update(self):
"""Fait le rendu du jeu."""
self.window.fill((255, 255, 255))
self.render_map()
display.update()
def render_map(self):
x_offset = 0
y_offset = 0
for i in range(4):
self.window.blit(self.tiles[i], (i*16, 0))
self.rendered_surface = None