Compare commits

...

2 Commits
tk ... master

  1. BIN
      Units/__pycache__/knight.cpython-311.pyc
  2. 62
      Units/hound.py
  3. 66
      Units/knight.py
  4. 63
      Units/zombie.py
  5. BIN
      assets/Hound/Idle/Idle_dir1.gif
  6. BIN
      assets/Hound/Idle/Idle_dir2.gif
  7. BIN
      assets/Hound/Idle/Idle_dir3.gif
  8. BIN
      assets/Hound/Idle/Idle_dir4.gif
  9. BIN
      assets/Hound/Idle/Idle_dir5.gif
  10. BIN
      assets/Hound/Idle/Idle_dir6.gif
  11. BIN
      assets/Hound/Idle/Idle_dir7.gif
  12. BIN
      assets/Hound/Idle/Idle_dir8.gif
  13. BIN
      assets/Hound/Walk/Walk_dir1.gif
  14. BIN
      assets/Hound/Walk/Walk_dir2.gif
  15. BIN
      assets/Hound/Walk/Walk_dir3.gif
  16. BIN
      assets/Hound/Walk/Walk_dir4.gif
  17. BIN
      assets/Hound/Walk/Walk_dir5.gif
  18. BIN
      assets/Hound/Walk/Walk_dir6.gif
  19. BIN
      assets/Hound/Walk/Walk_dir7.gif
  20. BIN
      assets/Hound/Walk/Walk_dir8.gif
  21. BIN
      assets/Zombie/Idle/Idle_dir1.gif
  22. BIN
      assets/Zombie/Idle/Idle_dir2.gif
  23. BIN
      assets/Zombie/Idle/Idle_dir3.gif
  24. BIN
      assets/Zombie/Idle/Idle_dir4.gif
  25. BIN
      assets/Zombie/Idle/Idle_dir5.gif
  26. BIN
      assets/Zombie/Idle/Idle_dir6.gif
  27. BIN
      assets/Zombie/Idle/Idle_dir7.gif
  28. BIN
      assets/Zombie/Idle/Idle_dir8.gif
  29. BIN
      assets/Zombie/Walk/Walk_dir1.gif
  30. BIN
      assets/Zombie/Walk/Walk_dir2.gif
  31. BIN
      assets/Zombie/Walk/Walk_dir3.gif
  32. BIN
      assets/Zombie/Walk/Walk_dir4.gif
  33. BIN
      assets/Zombie/Walk/Walk_dir5.gif
  34. BIN
      assets/Zombie/Walk/Walk_dir6.gif
  35. BIN
      assets/Zombie/Walk/Walk_dir7.gif
  36. BIN
      assets/Zombie/Walk/Walk_dir8.gif
  37. 64
      main.py
  38. 1
      solver.py

BIN
Units/__pycache__/knight.cpython-311.pyc

Binary file not shown.

62
Units/hound.py

@ -0,0 +1,62 @@
import tkinter as tk
import os
import glob
import time
import random
from Effects.animated_gif import AnimatedGif
from Effects.order_click import OrderClick
class Unit:
def __init__(self, engine, position=(0,0)):
self.engine = engine
self.position = position
self.target = position
self.destination = position
gifs = glob.glob("assets/Hound/**/*.gif", recursive=True)
self.animation = {os.path.basename(gif): AnimatedGif(gif) for gif in gifs}
self.state = "Walk"
self.speed = .025 # This is now the delay between updates in seconds
self.partial_move = 0
self.direction = 2
self.visited = [self.position]
self.starting_position = (position)
self.already_tried = []
def move(self, target):
pass
def ai(self):
screen_x, screen_y = self.engine.iso_transform(*self.position)
if self.position != self.target:
self.direction = self.engine.get_direction(self.position,self.destination)
if self.partial_move<1:
screen_x_dest, screen_y_dest = self.engine.iso_transform(*self.destination)
self.partial_move += self.speed
screen_x += (screen_x_dest - screen_x) * self.partial_move
screen_y += (screen_y_dest - screen_y) * self.partial_move
else:
screen_x, screen_y = self.engine.iso_transform(*self.destination)
neighbors_list = self.engine.find_neighbors(self.destination) # Get the neighbors of the destination cell
# Remove any neighbors that are not walkable
neighbors_list = [cell for cell in neighbors_list if self.engine.battlefield[cell[1]][cell[0]].walkable]
# Update position and visited list
self.position = self.destination
self.destination = None
self.visited.append(self.position)
self.destination = self.engine.get_closest_neighbor(neighbors_list,self.target)
self.partial_move = 0
else:
self.visited = [self.target]
self.position = self.target
self.starting_position = self.target
self.destination = self.target
self.state = "Idle"
self.partial_move = 1
gif = self.animation.get(f'{self.state}_dir{self.direction}.gif')
return gif, screen_x, screen_y
def move_to(self, target):
self.target = target
self.state = "Walk"

66
Units/knight.py

@ -7,7 +7,7 @@ import random
from Effects.animated_gif import AnimatedGif
from Effects.order_click import OrderClick
class Knight:
class Unit:
def __init__(self, engine, position=(0,0)):
self.engine = engine
self.position = position
@ -21,6 +21,11 @@ class Knight:
self.direction = 2
self.visited = [self.position]
self.starting_position = (position)
self.already_tried = []
def move(self, target):
pass
def ai(self):
screen_x, screen_y = self.engine.iso_transform(*self.position)
@ -33,66 +38,15 @@ class Knight:
screen_y += (screen_y_dest - screen_y) * self.partial_move
else:
screen_x, screen_y = self.engine.iso_transform(*self.destination)
neighbors_list = self.engine.find_neighbors(self.destination) # Get the neighbors of the destination cell
# Remove any neighbors that are not walkable
neighbors_list = [cell for cell in neighbors_list if self.engine.battlefield[cell[1]][cell[0]].walkable]
old_distance_from_target = self.engine.get_distance(self.destination,self.target)
old_distance_from_start = self.engine.get_distance(self.destination,self.starting_position)
for cell_visited in self.visited:
if cell_visited in neighbors_list:
neighbors_list.remove(cell_visited)
#self.visited = self.visited[-2:]
# Update position and visited list
self.position = self.destination
self.visited.append(self.position)
for cell in neighbors_list:
self.engine.effects.append(OrderClick(self.engine.iso_transform(*cell),color="lightblue"))
for cell in self.visited:
self.engine.effects.append(OrderClick(self.engine.iso_transform(*cell),color="red"))
if not neighbors_list:
print("No neighbors")
#neighbors_list.append(self.visited)
#self.target = self.position
self.destination = None
else:
self.visited.append(self.position)
self.destination = self.engine.get_closest_neighbor(neighbors_list,self.target)
new_distance_from_target = self.engine.get_distance(self.destination,self.target)
new_distance_from_start = self.engine.get_distance(self.destination,self.starting_position)
if new_distance_from_target >= old_distance_from_target or new_distance_from_start < old_distance_from_start:
print(f"{time.time()} - No progress")
new_neighbors_list = []
for neighbor in neighbors_list:
neighbor_neighbors_list = self.engine.find_neighbors(neighbor)
for neighbor_neighbor in neighbor_neighbors_list:
# consider only the neighbors of the neighbors that are walkable
if not self.engine.battlefield[neighbor_neighbor[1]][neighbor_neighbor[0]].walkable:
if neighbor_neighbor not in self.visited:
# if the neighbor of the neighbor is not walkable and has not been visited, add it to the list of visited cells
new_neighbors_list.append(neighbor)
break
# scegli uhn vicino casualmente come destinaziione
self.destination = new_neighbors_list[random.randint(0,len(new_neighbors_list)-1)]
if self.destination:
self.engine.effects.append(OrderClick(self.engine.iso_transform(*self.destination),color="purple"))
self.partial_move = 0
if self.position != self.target:
self.direction = self.engine.get_direction(self.position,self.destination)
else:
print("No destination")
self.visited = [self.position]
self.destination = self.position
#self.target = self.position
self.engine.effects.append(OrderClick(self.engine.iso_transform(*self.destination),color="brown"))
else:
self.visited = [self.target]
self.position = self.target
@ -102,3 +56,7 @@ class Knight:
self.partial_move = 1
gif = self.animation.get(f'Knight_{self.state}_dir{self.direction}.gif')
return gif, screen_x, screen_y
def move_to(self, target):
self.target = target
self.state = "Walk"

63
Units/zombie.py

@ -0,0 +1,63 @@
import tkinter as tk
import os
import glob
import time
import random
from Effects.animated_gif import AnimatedGif
from Effects.order_click import OrderClick
class Unit:
def __init__(self, engine, position=(0,0)):
self.engine = engine
self.position = position
self.target = position
self.script_name = os.path.basename(__file__).split('.')[0]
self.destination = position
gifs = glob.glob("assets/Zombie/**/*.gif", recursive=True)
self.animation = {os.path.basename(gif): AnimatedGif(gif) for gif in gifs}
self.state = "Walk"
self.speed = .05 # This is now the delay between updates in seconds
self.partial_move = 0
self.direction = 2
self.visited = [self.position]
self.starting_position = (position)
self.already_tried = []
def move(self, target):
pass
def ai(self):
screen_x, screen_y = self.engine.iso_transform(*self.position)
if self.position != self.target:
self.direction = self.engine.get_direction(self.position,self.destination)
if self.partial_move<1:
screen_x_dest, screen_y_dest = self.engine.iso_transform(*self.destination)
self.partial_move += self.speed
screen_x += (screen_x_dest - screen_x) * self.partial_move
screen_y += (screen_y_dest - screen_y) * self.partial_move
else:
screen_x, screen_y = self.engine.iso_transform(*self.destination)
neighbors_list = self.engine.find_neighbors(self.destination) # Get the neighbors of the destination cell
# Remove any neighbors that are not walkable
neighbors_list = [cell for cell in neighbors_list if self.engine.battlefield[cell[1]][cell[0]].walkable]
# Update position and visited list
self.position = self.destination
self.destination = None
self.visited.append(self.position)
self.destination = self.engine.get_closest_neighbor(neighbors_list,self.target)
self.partial_move = 0
else:
self.visited = [self.target]
self.position = self.target
self.starting_position = self.target
self.destination = self.target
self.state = "Idle"
self.partial_move = 1
gif = self.animation.get(f'{self.state}_dir{self.direction}.gif')
return gif, screen_x, screen_y
def move_to(self, target):
self.target = target
self.state = "Walk"

BIN
assets/Hound/Idle/Idle_dir1.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 150 KiB

BIN
assets/Hound/Idle/Idle_dir2.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

BIN
assets/Hound/Idle/Idle_dir3.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

BIN
assets/Hound/Idle/Idle_dir4.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 127 KiB

BIN
assets/Hound/Idle/Idle_dir5.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

BIN
assets/Hound/Idle/Idle_dir6.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 KiB

BIN
assets/Hound/Idle/Idle_dir7.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

BIN
assets/Hound/Idle/Idle_dir8.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 139 KiB

BIN
assets/Hound/Walk/Walk_dir1.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

BIN
assets/Hound/Walk/Walk_dir2.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

BIN
assets/Hound/Walk/Walk_dir3.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

BIN
assets/Hound/Walk/Walk_dir4.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

BIN
assets/Hound/Walk/Walk_dir5.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 69 KiB

BIN
assets/Hound/Walk/Walk_dir6.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

BIN
assets/Hound/Walk/Walk_dir7.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

BIN
assets/Hound/Walk/Walk_dir8.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

BIN
assets/Zombie/Idle/Idle_dir1.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

BIN
assets/Zombie/Idle/Idle_dir2.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

BIN
assets/Zombie/Idle/Idle_dir3.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
assets/Zombie/Idle/Idle_dir4.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 83 KiB

BIN
assets/Zombie/Idle/Idle_dir5.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
assets/Zombie/Idle/Idle_dir6.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

BIN
assets/Zombie/Idle/Idle_dir7.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

BIN
assets/Zombie/Idle/Idle_dir8.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

BIN
assets/Zombie/Walk/Walk_dir1.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 168 KiB

BIN
assets/Zombie/Walk/Walk_dir2.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB

BIN
assets/Zombie/Walk/Walk_dir3.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB

BIN
assets/Zombie/Walk/Walk_dir4.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

BIN
assets/Zombie/Walk/Walk_dir5.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

BIN
assets/Zombie/Walk/Walk_dir6.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

BIN
assets/Zombie/Walk/Walk_dir7.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 165 KiB

BIN
assets/Zombie/Walk/Walk_dir8.gif

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

64
main.py

@ -4,9 +4,14 @@ import glob
import os
import json
from Units.knight import Knight
import Units
from Effects.order_click import OrderClick
from tkinter import Menu
from collections import deque
import Units.hound
import Units.zombie
@ -17,8 +22,8 @@ class IsometricGame:
self.height = height
self.view_offset_x = 800
self.view_offset_y = 0
self.cell_width = 132
self.cell_height = 66
self.cell_width = 134
self.cell_height = 68
self.cell_selected = (0, 0)
self.last_trigger_time = 0
@ -48,7 +53,7 @@ class IsometricGame:
self.canvas.bind('<Motion>', self.calculate_coordinates)
self.knight = Knight(self, (1,1))
self.knight = Units.zombie.Unit(self, position=(1, 1))
self.draw_battlefield()
def calculate_coordinates(self, event):
@ -141,7 +146,40 @@ class IsometricGame:
x1, y1 = position1
x2, y2 = position2
return abs(((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5)
def get_distance_components(self, position1, position2):
x1, y1 = position1
x2, y2 = position2
return abs(x2 - x1), abs(y2 - y1)
#function to check if a cell is near a wall
def near_wall(self, position):
x, y = position
neighbors = self.find_neighbors(position)
for neighbor in neighbors:
if not self.battlefield[neighbor[1]][neighbor[0]].walkable:
return True
def min_steps_to_target(self, start, target):
grid = [[0 for _ in range(self.width)] for _ in range(self.height)]
rows, cols = len(grid), len(grid[0])
dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]
queue = deque([start])
visited = set([start])
steps = 0
while queue:
for _ in range(len(queue)):
x, y = queue.popleft()
if (x, y) == target:
return steps
for i in range(4):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < rows and 0 <= ny < cols and (nx, ny) not in visited:
queue.append((nx, ny))
visited.add((nx, ny))
steps += 1
return -1
def draw_battlefield(self):
self.canvas.delete("cell") # Pulisce le celle precedenti prima di ridisegnare
self.canvas.delete("cell-label")
@ -235,6 +273,7 @@ class IsometricGame:
# calculate the clicked cell's coordinates
cell_x, cell_y = self.inv_iso_transform(event.x, event.y)
self.battlefield[cell_y][cell_x].walkable ^= True
self.battlefield[cell_y][cell_x].tile = self.tiles["landscapeTiles_067"] if self.battlefield[cell_y][cell_x].walkable else self.tiles["landscapeTiles_066"]
self.draw_battlefield()
self.draw_units()
@ -260,9 +299,7 @@ class IsometricGame:
return
if 0 <= cell_x < self.width and 0 <= cell_y < self.height:
self.knight.starting_position = self.knight.position
self.knight.target = (cell_x,cell_y)
self.knight.state = "Walk"
self.knight.move_to((cell_x,cell_y))
def run(self):
self.draw_battlefield()
@ -288,6 +325,17 @@ class Cell:
if __name__ == "__main__":
with open('maze.json', 'r') as file:
data = json.load(file)
# Creare una matrice 10x10 piena di False
room = [[True for _ in range(10)] for _ in range(10)]
# Cambiare i valori interni della matrice in True
for i in range(1, 9):
for j in range(1, 8):
room[i][j] = False
# Creare un'apertura di un'unità verso l'esterno
room[0][4] = True # Cambia questo valore per spostare l'apertura
data =room
# data is a boolean matrix, find dmensions
width = len(data[0])
height = len(data)

1
solver.py

@ -70,5 +70,6 @@ class MazeSolver:
self.window.mainloop()
# Create and run the maze solver
#Breadth-First Search, BFS
solver = MazeSolver('maze.json')
solver.run()
Loading…
Cancel
Save