Browse Source

Refactor knight.py and main.py

master
Matteo Benedetto 1 year ago
parent
commit
9e3de5f896
  1. 62
      Units/hound.py
  2. 2
      Units/knight.py
  3. 63
      Units/zombie.py
  4. BIN
      assets/Hound/Idle/Idle_dir1.gif
  5. BIN
      assets/Hound/Idle/Idle_dir2.gif
  6. BIN
      assets/Hound/Idle/Idle_dir3.gif
  7. BIN
      assets/Hound/Idle/Idle_dir4.gif
  8. BIN
      assets/Hound/Idle/Idle_dir5.gif
  9. BIN
      assets/Hound/Idle/Idle_dir6.gif
  10. BIN
      assets/Hound/Idle/Idle_dir7.gif
  11. BIN
      assets/Hound/Idle/Idle_dir8.gif
  12. BIN
      assets/Hound/Walk/Walk_dir1.gif
  13. BIN
      assets/Hound/Walk/Walk_dir2.gif
  14. BIN
      assets/Hound/Walk/Walk_dir3.gif
  15. BIN
      assets/Hound/Walk/Walk_dir4.gif
  16. BIN
      assets/Hound/Walk/Walk_dir5.gif
  17. BIN
      assets/Hound/Walk/Walk_dir6.gif
  18. BIN
      assets/Hound/Walk/Walk_dir7.gif
  19. BIN
      assets/Hound/Walk/Walk_dir8.gif
  20. BIN
      assets/Zombie/Idle/Idle_dir1.gif
  21. BIN
      assets/Zombie/Idle/Idle_dir2.gif
  22. BIN
      assets/Zombie/Idle/Idle_dir3.gif
  23. BIN
      assets/Zombie/Idle/Idle_dir4.gif
  24. BIN
      assets/Zombie/Idle/Idle_dir5.gif
  25. BIN
      assets/Zombie/Idle/Idle_dir6.gif
  26. BIN
      assets/Zombie/Idle/Idle_dir7.gif
  27. BIN
      assets/Zombie/Idle/Idle_dir8.gif
  28. BIN
      assets/Zombie/Walk/Walk_dir1.gif
  29. BIN
      assets/Zombie/Walk/Walk_dir2.gif
  30. BIN
      assets/Zombie/Walk/Walk_dir3.gif
  31. BIN
      assets/Zombie/Walk/Walk_dir4.gif
  32. BIN
      assets/Zombie/Walk/Walk_dir5.gif
  33. BIN
      assets/Zombie/Walk/Walk_dir6.gif
  34. BIN
      assets/Zombie/Walk/Walk_dir7.gif
  35. BIN
      assets/Zombie/Walk/Walk_dir8.gif
  36. 12
      main.py

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"

2
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

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

12
main.py

@ -4,11 +4,15 @@ 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
@ -18,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
@ -49,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):

Loading…
Cancel
Save