Browse Source

chore: Refactor code to improve knight movement and update maze configuration

pyglet
Matteo Benedetto 1 year ago
parent
commit
2f05711825
  1. BIN
      Effects/__pycache__/animated_gif.cpython-311.pyc
  2. BIN
      Effects/__pycache__/order_click.cpython-311.pyc
  3. BIN
      Units/__pycache__/knight.cpython-311.pyc
  4. 49
      main.py
  5. 8
      maze.json

BIN
Effects/__pycache__/animated_gif.cpython-311.pyc

Binary file not shown.

BIN
Effects/__pycache__/order_click.cpython-311.pyc

Binary file not shown.

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

Binary file not shown.

49
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('<KeyRelease>', self.on_key_press)
@ -59,8 +65,8 @@ class IsometricGame:
# self.canvas.bind('<Motion>', 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

8
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]]
[
[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]
]

Loading…
Cancel
Save