From a7bbd1c6dbed77d135345ea0519616dcddd570fe Mon Sep 17 00:00:00 2001 From: enne2 Date: Tue, 5 Mar 2024 20:04:14 +0100 Subject: [PATCH] =?UTF-8?q?Aggiornamento=20dimensioni=20generatore=20labir?= =?UTF-8?q?into=20e=20velocit=C3=A0=20cavaliere?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__pycache__/animated_gif.cpython-311.pyc | Bin 1724 -> 1688 bytes .../__pycache__/order_click.cpython-311.pyc | Bin 1549 -> 1023 bytes Effects/animated_gif.py | 10 +-- Effects/order_click.py | 2 +- Units/__pycache__/knight.cpython-311.pyc | Bin 4181 -> 4303 bytes Units/knight.py | 13 ++- main.py | 80 +++++++++--------- maze.json | 2 +- maze.py | 2 +- 9 files changed, 56 insertions(+), 53 deletions(-) diff --git a/Effects/__pycache__/animated_gif.cpython-311.pyc b/Effects/__pycache__/animated_gif.cpython-311.pyc index 2dbd25c03329bb3eb47558b0702ceb9320ea21be..3d7bd516657af7c040ebdd5914e641b7d414a7b9 100644 GIT binary patch delta 508 zcmdnPJA;>ZIWI340}!xYJD(ajkynkAA%!7@F^4gVaiWbRBh$t(Ehc>ypgc1Wf4&AJ zrURvGnK&6Z8A^aW7|3FTu@*2*W@Hwf_>P~gmbr#`0W*jv&$J9EzZ$NlmZgRv3uG!7 zEMNty(X3~#;$>i{Wy%v&W+>r-$uTgfFch&cGDHHM70gi1P{AC@P|nzsuF2w8!~|3n z@)BgICi^Y6g39!q)RJ4QnYoGSskiuY@)J|y6Z3$)lFawIz=>=V7$-3PxGO9>xt>LfU)WKS z;h-d!qdN0JHAYAE%{y2a85!j#?_$k!WiAp0xsMY>fWogx8AJ$y2mv7Ri^C>2KczG$ e)vibd$OQ#Wu@8{=zzj6u1_N&c7*4Kaa|QrYm3a^V delta 618 zcmYLFO=uHA6n-=NlWcX|jrlQp+C~$J;33t6U@HXiAQ-h+g?f?bZtA9+Y?y3m2?^;% z@Ze#?7WCjDim@ zin|zKpV?ougr?A)MblI64QZFv@6YaNiA^Z;juYUn=)c2pKPAN4QiA}ra5Zrj+^Nod znU6TZIiMnfRWRTlY{HhX3Y+LfDJ$Qmyz@$ftXU=fqE$7_Ej?{zaUhl~+YIEALn}r1 z2Ol9T2iRQ?=I%aqT<2z`xMl`QedBSxSg9@9CJnKaKrYs5X4MG7D^KhjlsYsk(GyId zDp!#eNYhBHnUG_K=5u z9Yy0iSI2-z;y;bFxF>niSU~=^7*R<+3I|a&pAinyD4+S*6%j)7eov|{i$Ta;x2mp5 v=_&pmg_FoB&FLg3jnVJ$V#P5wZ1WmD&qDrh^*fAM3xq}x>ra<|U%vhap3sq@ diff --git a/Effects/__pycache__/order_click.cpython-311.pyc b/Effects/__pycache__/order_click.cpython-311.pyc index 68498876fb3290233abdfb42948266c8c86218eb..eebe1e5599f6223d1f25a073782b34836ec048f8 100644 GIT binary patch delta 136 zcmeC>`OnU~oR^o20SI_*o=>&g$a{s6n;FOh;m>jyRl0g=huSV9@aCiAfNN=XA174d@z XULf&{!zMRBr8Fniu1I$BX4WVGtalyu delta 704 zcmey*-pj+goR^o20SMY|9Zt2~$a{s68^mP>;?D+*lix6E)-x_(1SvoVH4IryFtHS7 zNf?s>C=O>=0nI{D$AVp53TqWN0|Qt!Qx#A*vYvXJhNCG=!EZQ7HQ4YJwiI@t1t2ei zK?>^{j%6GS46A_{0vM4!QO{7!l)@Rzkir$rpvmp`k`<`tr5unb0(t)>C~RJWShpB% zv8AS^r6!kXvfN@x%go8S#Zr=(UVMu^zqBMLGcWZPYk6i$Nrqd9CQA{}w+y$qlZ#Rl zOH$+W%Mx>nK%rj52_!U`i+Dh60gw{b;*7}wOcwP&ft&`08yq|x+&$bA3NLa;E)0;=8OOd14*DLkbJ-|+yit9Bd6pIVTlQ*H~57+N{S}$Vsd7bpZt%hmr-SM N53>ZL+T^9oQ2>3Jo3H=? diff --git a/Effects/animated_gif.py b/Effects/animated_gif.py index 45e9361..7c32a1f 100644 --- a/Effects/animated_gif.py +++ b/Effects/animated_gif.py @@ -1,4 +1,4 @@ -import tkinter as tk +import pyglet import itertools class AnimatedGif: @@ -11,12 +11,12 @@ class AnimatedGif: i = 0 while True: try: - image = tk.PhotoImage(file=path, format='gif -index %i' %(i)) - resized_image = image.subsample(1) # Resize image to half - self.frames.append(resized_image) + image = pyglet.image.load_animation(path) + frame = image.get_texture(i) + self.frames.append(frame) i += 1 - except tk.TclError: + except IndexError: break def next_frame(self): diff --git a/Effects/order_click.py b/Effects/order_click.py index 3312a85..6d1c081 100644 --- a/Effects/order_click.py +++ b/Effects/order_click.py @@ -8,7 +8,7 @@ class OrderClick: def next_frame(self, canvas: Canvas) -> bool: self.radius -= 5 shift = 30 - canvas.create_oval(self.position[0]-self.radius, self.position[1]-self.radius/2+shift, self.position[0]+self.radius, self.position[1]+self.radius/2+shift, fill="", tags="effect", outline=self.color, width=2) + #canvas.create_oval(self.position[0]-self.radius, self.position[1]-self.radius/2+shift, self.position[0]+self.radius, self.position[1]+self.radius/2+shift, fill="", tags="effect", outline=self.color, width=2) if not self.radius: return False diff --git a/Units/__pycache__/knight.cpython-311.pyc b/Units/__pycache__/knight.cpython-311.pyc index cbeee0ff720f5306fe56eb89e228c570044622dc..833b22d575741470e45505e92c9ec0879cf2e47f 100644 GIT binary patch delta 1259 zcmZuwU1%It6ux)n&hG4*nQaY8x<84Vt~Q;Nq-=kng^Iopfh6 zy|W3lRJ)2ug(8$oDHWA~poSI{st-YYvF&rSA(~~L3Vkv75~&a3i=K10T`hWc?|1Jx z-?@kLGxw+LgHhv4!-x}*cRpHmKf7imXJ$aTK^)@H8Yy*mTB1@{N($^sP4!ucQRLK` z=0{2qN)&R8IP87mXkRFV+=A7WQuJDVMRBOGXnsg>)Z6S?{%zIK#CbZpCvu6DBTsKR zQP842O$SZyX)*BwJ#5BbwUkXXZfXGH51Jo9tsmCo9|3L*JR2o6Z=I!R$P=Ct~+`8?%VPyV7 zwdOX%NHEX6Cg>--yUyL9vA`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V-vq?T2NL1r}x$=vSxnP-0DG9 zS(-?uQY-{p4JR55=9!Z}H?O8EdBwaHKHF~zj5-;1URG3XI+kVejCNp}rS(BD4+K~n zLOX&N0qbH*0m{l`G8;>%EIENQWI+rB_;pLCt1KU8Y9`D4C=)@}X=EY(!Lp^x0gQ%M zW*?5WHe4aD&X|9XHLki*^ZNd_hLhC&(ds-OE-ncbtl^?Hyl4$q48-2Z7p?tJeXA|M zhBbkjxd7(HYQ1`a0VD*yb)D7l$K&Vt6U9&0@v<_kA8^Xor`wA`wh0d!0QjSQYV*=f zp!M?~w&kGr+k1PLp=<-fAi^+00|MS}{jM;)?K)ti+HRh=ha3U9qeAMineuz~jwN~u zs$o7>O!D!HeW3gA<9?bprl%7}Qpo`}0vd{ErU5D5`N;qP diff --git a/Units/knight.py b/Units/knight.py index 9c6420c..6b7edcb 100644 --- a/Units/knight.py +++ b/Units/knight.py @@ -1,3 +1,4 @@ +import pyglet import os import glob @@ -8,14 +9,14 @@ class Knight: def __init__(self, engine, position=(0,0)): self.engine = engine self.position = position - self.target = position + self.target = (8,5) self.destination = position gifs = glob.glob("KnightBasic/**/*.gif", recursive=True) - self.animation = {os.path.basename(gif): AnimatedGif(gif) for gif in gifs} + self.animation = {os.path.basename(gif): pyglet.sprite.Sprite(pyglet.resource.animation(gif)) for gif in gifs} self.state = "Walk" - self.speed = .05 # This is now the delay between updates in seconds + self.speed = .025 # This is now the delay between updates in seconds self.partial_move = 0 - self.direction = 2 + self.direction = 7 self.visited = [self.position] self.starting_position = (position) @@ -45,8 +46,6 @@ class Knight: self.position = self.destination self.destination = self.engine.get_closest_neighbor(neighbors_list,self.target) self.partial_move = 0 - print(self.partial_move) - gif = self.animation.get(f'Knight_{self.state}_dir{self.direction}.gif') return gif, screen_x, screen_y @@ -61,6 +60,6 @@ class Knight: def run_to(self, target): if self.target == target and self.state == "Run": return - self.speed = .15 + self.speed = .10 self.target = target self.state = "Run" \ No newline at end of file diff --git a/main.py b/main.py index 97b29bd..41db5f0 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,11 @@ import time -import tkinter as tk import glob import os import json +import pyglet from Units.knight import Knight from Effects.order_click import OrderClick -from tkinter import Menu - @@ -15,42 +13,54 @@ class IsometricGame: def __init__(self, width, height, data): self.width = width self.height = height - self.view_offset_x = 800 - self.view_offset_y = 0 self.cell_width = 132 self.cell_height = 66 self.cell_selected = (0, 0) + self.view_offset_x = 800 + self.view_offset_y = 500 self.last_trigger_time = 0 self.min_interval = 0.5 # Minimum interval in seconds 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.window.event + def on_draw(): + pyglet.gl.glClear(pyglet.gl.GL_COLOR_BUFFER_BIT) + self.draw_battlefield() + self.draw_units() + # self.draw_effects() + + @self.window.event + def on_mouse_scroll(x, y, scroll_x, scroll_y): + # Adjust the view offset based on the scroll direction + self.view_offset_y += scroll_y * 70 # Adjust the multiplier as needed + self.editor_mode = False self.input_lock = False - self.window = tk.Tk() files = sorted(glob.glob("Tiles/**/*.png", recursive=True)) - self.tiles = {os.path.splitext(os.path.basename(file))[0]: tk.PhotoImage(file=file) for file in files} + self.tiles = {os.path.splitext(os.path.basename(file))[0]: pyglet.sprite.Sprite(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)] - self.window.title("Campo di Battaglia RTS Isometrico") - - self.canvas = tk.Canvas(self.window, width=1920, height=1080, bg='black') - self.canvas.pack() - self.menu = Menu(self.canvas, tearoff=0) - self.window.bind('', self.on_key_press) - self.canvas.bind('', self.on_canvas_click) - self.canvas.bind('', self.on_canvas_right_click) - self.canvas.bind('', self.on_canvas_double_right_click) - self.canvas.bind('', self.calculate_coordinates) + + # self.canvas.pack() + # self.menu = Menu(self.canvas, tearoff=0) + # self.window.bind('', self.on_key_press) + # self.canvas.bind('', self.on_canvas_click) + # self.canvas.bind('', self.on_canvas_right_click) + # self.canvas.bind('', self.on_canvas_double_right_click) + # self.canvas.bind('', self.calculate_coordinates) self.knight = Knight(self, (1,1)) - self.draw_battlefield() + # self.draw_battlefield() def calculate_coordinates(self, event): if self.input_lock: @@ -132,6 +142,7 @@ class IsometricGame: 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 @@ -144,23 +155,21 @@ class IsometricGame: return abs(((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5) def draw_battlefield(self): - self.canvas.delete("cell") # Pulisce le celle precedenti prima di ridisegnare - self.canvas.delete("cell-label") - for y, row in enumerate(self.battlefield): - for x, cell in enumerate(row): + 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) - offset = (99 - cell.tile.height()) / 2 - self.canvas.create_image(screen_x, screen_y+45+offset, image=cell.tile, tags="cell") - if self.editor_mode: - self.canvas.create_text(screen_x, screen_y+45, text=str(cell.elevation), tags="cell_label") - self.canvas.create_text(screen_x+10, screen_y+45, text=str(int(cell.walkable)), tags="cell_label", fill="white" if cell.walkable else "red") - self.canvas.create_text(screen_x+10, screen_y+35, text=str(int(cell.tile.height())), tags="cell_label", fill="white" if cell.walkable else "red") - + cell.tile.x = screen_x + cell.tile.y = screen_y + self.cell_height + self.cell_height/2 + cell.tile.draw() + #self.canvas.create_image(screen_x, screen_y+45+offset, image=cell.tile, tags="cell") + - def draw_units(self): - self.canvas.delete("unit") # Pulisce le unità precedenti prima di ridisegnare + def draw_units(self): gif, screen_x, screen_y = self.knight.ai() # Calcola la posizione e l'animazione del cavaliere - self.canvas.create_image(screen_x, screen_y+35, image=gif.next_frame(), tags="unit") # Disegna il cavaliere + gif.x = screen_x - self.cell_width/2 + gif.y = screen_y + height*3 + gif.draw() + #self.canvas.create_image(screen_x, screen_y+35, image=gif.next_frame(), tags="unit") # Disegna il cavaliere def draw_effects(self): # Pulisce gli effetti precedenti prima di ridisegnare @@ -269,11 +278,6 @@ class IsometricGame: self.update() self.window.mainloop() - def update(self): - #self.draw_battlefield() - self.draw_units() - self.draw_effects() - self.window.after(50, self.update) # Call again after 100 ms class Cell: def __init__(self, walkable=False, tile=None, elevation=0, offset=0): @@ -292,4 +296,4 @@ if __name__ == "__main__": width = len(data[0]) height = len(data) game = IsometricGame(width=width, height=height, data=data) - game.run() + pyglet.app.run() diff --git a/maze.json b/maze.json index 59b78c5..7f5162e 100644 --- a/maze.json +++ b/maze.json @@ -1 +1 @@ -[[true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true], [true, false, false, false, false, false, true, false, false, false, false, false, true, false, false, false, false, false, false, false, true], [true, true, true, true, true, false, true, true, true, false, true, false, true, true, true, true, true, false, true, true, true], [true, false, false, false, false, false, false, false, false, false, true, false, true, false, false, false, true, false, false, false, true], [true, false, false, false, true, true, true, true, false, true, true, false, true, false, true, false, true, true, true, false, true], [true, false, true, false, false, false, false, false, true, false, false, false, true, false, false, false, false, false, true, false, true], [true, false, true, true, true, true, true, false, true, false, true, true, true, false, true, true, true, false, true, false, true], [true, false, false, false, false, false, false, false, false, false, false, false, true, false, true, false, false, false, true, false, true], [true, false, true, false, true, true, true, false, true, true, false, false, true, false, true, false, true, true, true, false, true], [true, false, false, false, true, false, false, false, false, false, true, false, false, false, true, false, true, false, false, false, true], [true, true, false, false, true, false, true, true, true, false, true, false, false, false, true, false, true, false, true, true, true], [true, false, false, false, true, false, false, false, true, false, false, false, true, false, false, false, true, false, false, false, true], [true, false, false, true, true, true, true, false, true, true, true, false, true, false, true, true, true, false, true, false, true], [true, false, true, false, false, false, false, false, true, false, false, false, true, false, false, false, true, false, true, false, true], [true, true, false, false, true, true, true, true, true, false, true, false, true, true, true, false, true, false, true, false, true], [true, false, false, false, true, false, false, false, false, false, true, false, true, false, false, false, true, false, true, false, true], [true, false, false, true, true, false, true, false, true, false, true, false, false, false, true, true, false, true, true, false, true], [true, false, true, false, false, false, true, false, false, false, true, false, true, false, false, false, true, false, false, false, true], [true, false, true, false, true, false, true, false, true, true, true, true, true, true, true, false, true, false, true, true, true], [true, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true], [true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, 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]] \ No newline at end of file diff --git a/maze.py b/maze.py index a978b0b..f555664 100644 --- a/maze.py +++ b/maze.py @@ -99,5 +99,5 @@ class MazeGenerator: self.window.mainloop() # Crea e avvia il generatore di labirinti -generator = MazeGenerator(20, 20) +generator = MazeGenerator(5, 5) generator.run()