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. 45
      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.

45
main.py

@ -25,13 +25,11 @@ class IsometricGame:
self.effects = [] self.effects = []
# Create a window with a specific size and title # 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 = pyglet.window.Window(width=1920, height=1080, caption='Campo di Battaglia RTS Isometrico')
self.batch = pyglet.graphics.Batch()
@self.window.event @self.window.event
def on_draw(): def on_draw():
pyglet.gl.glClear(pyglet.gl.GL_COLOR_BUFFER_BIT) self.window.clear()
self.draw_battlefield() self.draw_battlefield()
self.draw_units()
# self.draw_effects()
@self.window.event @self.window.event
def on_mouse_scroll(x, y, scroll_x, scroll_y): def on_mouse_scroll(x, y, scroll_x, scroll_y):
@ -43,13 +41,21 @@ class IsometricGame:
self.input_lock = False self.input_lock = False
files = sorted(glob.glob("Tiles/**/*.png", recursive=True)) 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)] # 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 # use data to create the battlefield
self.battlefield = [[Cell(walkable = not data[y][x], tile=self.tiles["landscapeTiles_067"] def create_sprite(tile, x, y):
if not data[y][x] else self.tiles["landscapeTiles_066"] print(x, y)
) for x in range(self.width)] for y in range(self.height)] 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.canvas.pack()
# self.menu = Menu(self.canvas, tearoff=0) # self.menu = Menu(self.canvas, tearoff=0)
# self.window.bind('<KeyRelease>', self.on_key_press) # self.window.bind('<KeyRelease>', self.on_key_press)
@ -59,8 +65,8 @@ class IsometricGame:
# self.canvas.bind('<Motion>', self.calculate_coordinates) # self.canvas.bind('<Motion>', self.calculate_coordinates)
self.knight = Knight(self, (1,1)) #self.knight = Knight(self, (1,1))
# self.draw_battlefield() self.draw_battlefield()
def calculate_coordinates(self, event): def calculate_coordinates(self, event):
if self.input_lock: if self.input_lock:
@ -142,7 +148,6 @@ class IsometricGame:
screen_x = (x-y)*self.cell_width/2 + self.view_offset_x 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_y = (x+y)*self.cell_height/2 + self.view_offset_y
return screen_x, screen_y return screen_x, screen_y
min_distance_index = distances.index(min(distances))
def inv_iso_transform(self, screen_x, screen_y): 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 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) return abs(((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5)
def draw_battlefield(self): def draw_battlefield(self):
for y, row in reversed(list(enumerate(self.battlefield))): for y, row in list(enumerate(self.battlefield)):
for x, cell in reversed(list(enumerate(row))): for x, cell in list(enumerate(row)):
screen_x, screen_y = self.iso_transform(x, y) cell.sprite.draw()
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") #self.canvas.create_image(screen_x, screen_y+45+offset, image=cell.tile, tags="cell")
@ -274,16 +276,13 @@ class IsometricGame:
def run(self): def run(self):
self.draw_battlefield()
self.update() self.update()
self.window.mainloop() self.window.mainloop()
class Cell: class Cell:
def __init__(self, walkable=False, tile=None, elevation=0, offset=0): def __init__(self, walkable=False, sprite=None, elevation=0, offset=0):
self.tile = tile self.sprite = sprite
self.walkable = walkable self.walkable = walkable
self.elevation = elevation 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