0
\$\begingroup\$

I almost made the isometric tile selection formula work but it seems to be slightly off.

It seems to be working correctly when the mouse is hovered over the first half of the tile but it jumps to the next tile as the mouse is moved to the second half.

Pointer over left half of tile, correct tile is highlighted

Pointer over right half of tile, neighbouring tile is highlighted

It also doesn't account for a camera and I don't know how to implement that. So some help on that as well would be appreciated.

Here's the code used:

tile_width_half = 84/2

tile_height_half = 51/2

class Game(pyglet.window.Window):
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.island = Island()
        self.camera = Camera()
        self.selection = pyglet.sprite.Sprite(pyglet.image.load("path"))
    def on_mouse_motion(self, x, y, dx, dy):
        global tile_height_half,tile_width_half
        j = int((x / tile_width_half + y / tile_height_half) / 2)
        i = int((y / tile_height_half - x / tile_width_half) / 2)
        self.selection.x = (j-i)*tile_width_half
        self.selection.y = (j+i)*tile_height_half
    def on_draw(self):
        self.clear()
        self.island.draw()
        self.selection.draw()
    def on_key_press(self, symbol, modifiers):
        if symbol == key.A:
            self.camera.left = True
        if symbol == key.W:
            self.camera.up = True
        if symbol == key.D:
            self.camera.right = True
        if symbol == key.S:
            self.camera.down = True
        if symbol == key.ESCAPE:
            pyglet.app.exit()
    def on_key_release(self, symbol, modifiers):
        if symbol == key.A:
            self.camera.left = False
        if symbol == key.W:
            self.camera.up = False
        if symbol == key.D:
            self.camera.right = False
        if symbol == key.S:
            self.camera.down = False
    def update(self,dt):
        for tile in self.island.all_sprites:
            tile.x += self.camera.position[0]
            tile.y += self.camera.position[1]
        self.camera.move()


if __name__ == '__main__':
    game = Game(800,600,"title")
    pyglet.clock.schedule_interval(game.update,1/60)

    pyglet.app.run()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.