From 2f05711825d6141c69798414a55a4bc7029f6d3c Mon Sep 17 00:00:00 2001 From: enne2 Date: Wed, 21 Aug 2024 23:00:17 +0200 Subject: [PATCH] chore: Refactor code to improve knight movement and update maze configuration --- .../__pycache__/animated_gif.cpython-311.pyc | Bin 1688 -> 1688 bytes .../__pycache__/order_click.cpython-311.pyc | Bin 1023 -> 1023 bytes Units/__pycache__/knight.cpython-311.pyc | Bin 4303 -> 4303 bytes main.py | 49 +++++++++--------- maze.json | 8 ++- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/Effects/__pycache__/animated_gif.cpython-311.pyc b/Effects/__pycache__/animated_gif.cpython-311.pyc index 3d7bd516657af7c040ebdd5914e641b7d414a7b9..9728f23c1262ebf2cbfb70160fa95203a7282aa2 100644 GIT binary patch delta 21 bcmbQiJA;>JIWI340}!YOu1(v>)58V;GJIWI340}!xYJD<9dr-uyyH`N7z diff --git a/Effects/__pycache__/order_click.cpython-311.pyc b/Effects/__pycache__/order_click.cpython-311.pyc index eebe1e5599f6223d1f25a073782b34836ec048f8..22635ec2500881e5501280e49318621b1822157d 100644 GIT binary patch delta 21 bcmey*{-2#^IWI340}!YOu1(v>^O+d{M0y4y delta 21 bcmey*{-2#^IWI340}$}sJfFIe=QA?^NFfHW diff --git a/Units/__pycache__/knight.cpython-311.pyc b/Units/__pycache__/knight.cpython-311.pyc index 833b22d575741470e45505e92c9ec0879cf2e47f..80ee9d1bb5bd1084e897a0961b03b4a8fe0b6408 100644 GIT binary patch delta 21 bcmX@FcwUibIWI340}!YOu1(v>b65ZXK!XL- delta 21 bcmX@FcwUibIWI340}#~pT}a)?b65ZXMY9Hp diff --git a/main.py b/main.py index 41db5f0..aab8e31 100644 --- a/main.py +++ b/main.py @@ -25,13 +25,11 @@ class IsometricGame: self.effects = [] # Create a window with a specific size and title self.window = pyglet.window.Window(width=1920, height=1080, caption='Campo di Battaglia RTS Isometrico') - + self.batch = pyglet.graphics.Batch() @self.window.event def on_draw(): - pyglet.gl.glClear(pyglet.gl.GL_COLOR_BUFFER_BIT) + self.window.clear() self.draw_battlefield() - self.draw_units() - # self.draw_effects() @self.window.event def on_mouse_scroll(x, y, scroll_x, scroll_y): @@ -43,13 +41,21 @@ class IsometricGame: self.input_lock = False files = sorted(glob.glob("Tiles/**/*.png", recursive=True)) - self.tiles = {os.path.splitext(os.path.basename(file))[0]: pyglet.sprite.Sprite(pyglet.image.load(file)) for file in files} + self.tiles = {os.path.splitext(os.path.basename(file))[0]: pyglet.image.load(file) for file in files} # self.battlefield = [[Cell(walkable = True, tile=self.tiles["landscapeTiles_067"] ) for x in range(self.width)] for y in range(self.height)] # use data to create the battlefield - self.battlefield = [[Cell(walkable = not data[y][x], tile=self.tiles["landscapeTiles_067"] - if not data[y][x] else self.tiles["landscapeTiles_066"] - ) for x in range(self.width)] for y in range(self.height)] - + def create_sprite(tile, x, y): + print(x, y) + x,y= self.iso_transform(x, y) + print(x, y) + return pyglet.sprite.Sprite(tile, y=y, x=x) + self.battlefield = [[Cell(walkable = not data[row][column], sprite=create_sprite(tile=self.tiles["landscapeTiles_067"], y=(self.height-row), x=(self.width-column)) + if not data[row][column] else create_sprite(tile=self.tiles["landscapeTiles_066"], y=(self.height-row), x=(self.width-column)) + ) for column in range(self.width)] for row in range(self.height)] + + # Crea lo sprite del tile, aggiungilo al batch (e al gruppo, se usato) + # sprite = pyglet.sprite.Sprite(img=tile_image, x=x, y=y, batch=batch) + # tile_sprites.append(sprite) # self.canvas.pack() # self.menu = Menu(self.canvas, tearoff=0) # self.window.bind('', self.on_key_press) @@ -59,8 +65,8 @@ class IsometricGame: # self.canvas.bind('', self.calculate_coordinates) - self.knight = Knight(self, (1,1)) - # self.draw_battlefield() + #self.knight = Knight(self, (1,1)) + self.draw_battlefield() def calculate_coordinates(self, event): if self.input_lock: @@ -139,10 +145,9 @@ class IsometricGame: return neighbors_list[min_distance_index] def iso_transform(self, x, y): - screen_x = (x - y) * self.cell_width / 2 + self.view_offset_x - screen_y = (x + y) * self.cell_height / 2 + self.view_offset_y + screen_x = (x-y)*self.cell_width/2 + self.view_offset_x + screen_y = (x+y)*self.cell_height/2 + self.view_offset_y return screen_x, screen_y - min_distance_index = distances.index(min(distances)) def inv_iso_transform(self, screen_x, screen_y): x = ((screen_x - self.view_offset_x) / (self.cell_width / 2) + (screen_y - self.view_offset_y) / (self.cell_height / 2)) / 2 @@ -155,12 +160,9 @@ class IsometricGame: return abs(((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5) def draw_battlefield(self): - for y, row in reversed(list(enumerate(self.battlefield))): - for x, cell in reversed(list(enumerate(row))): - screen_x, screen_y = self.iso_transform(x, y) - cell.tile.x = screen_x - cell.tile.y = screen_y + self.cell_height + self.cell_height/2 - cell.tile.draw() + for y, row in list(enumerate(self.battlefield)): + for x, cell in list(enumerate(row)): + cell.sprite.draw() #self.canvas.create_image(screen_x, screen_y+45+offset, image=cell.tile, tags="cell") @@ -274,16 +276,13 @@ class IsometricGame: def run(self): - self.draw_battlefield() self.update() self.window.mainloop() class Cell: - def __init__(self, walkable=False, tile=None, elevation=0, offset=0): - self.tile = tile - - + def __init__(self, walkable=False, sprite=None, elevation=0, offset=0): + self.sprite = sprite self.walkable = walkable self.elevation = elevation diff --git a/maze.json b/maze.json index 7f5162e..b38a45f 100644 --- a/maze.json +++ b/maze.json @@ -1 +1,7 @@ -[[true, true, true, true, true], [true, false, true, false, true], [true, false, false, false, true], [true, false, false, false, true], [true, true, true, true, true]] \ No newline at end of file +[ + [true, true, true, true, true], + [true, false, true, false, true], + [true, false, false, false, true], + [true, false, false, false, true], + [true, true, true, true, true] +]