105 lines
2.9 KiB
Python
105 lines
2.9 KiB
Python
import arcade
|
|
from random import randint
|
|
|
|
SCREEN_WIDTH = 1920
|
|
SCREEN_HEIGHT = 1280
|
|
SCREEN_FULL = True
|
|
TILE_WIDTH = 64
|
|
TILE_HEIGHT = 32
|
|
|
|
def to_iso(tile_list):
|
|
for tile in tile_list:
|
|
col = tile.center_x//TILE_WIDTH
|
|
row = tile.center_y//TILE_HEIGHT
|
|
tile.center_x = tile.center_x - TILE_WIDTH//2 * (col-row)
|
|
tile.center_y = tile.center_y - TILE_HEIGHT//2 * (row+col)
|
|
return tile_list
|
|
|
|
class Player(arcade.Sprite):
|
|
def __init__(self,sprite,lookLeft=True):
|
|
super().__init__()
|
|
self.texture = sprite.texture
|
|
self.lookLeft = lookLeft
|
|
self.center_x = sprite.center_x
|
|
self.center_y = sprite.center_y
|
|
self.move_timer = 0
|
|
def update(self,delta_time):
|
|
super().update()
|
|
self.move_timer += delta_time
|
|
if self.move_timer >= 1:
|
|
self.move_timer = 0
|
|
way = randint(0,3)
|
|
if way == 0:
|
|
self.move(1,1,delta_time)
|
|
elif way == 1:
|
|
self.move(1,-1,delta_time)
|
|
elif way == 2:
|
|
self.move(-1,-1,delta_time)
|
|
elif way == 3:
|
|
self.move(-1,1,delta_time)
|
|
|
|
def move(self,dx,dy,dt):
|
|
self.lookLeft = False if self.change_x > 0 else True
|
|
self.change_x = dx * TILE_WIDTH//2*dt
|
|
self.change_y = dy * TILE_HEIGHT//2*dt
|
|
if self.change_x > 0 and self.lookLeft:
|
|
self.texture = self.texture.flip_left_right()
|
|
self.lookLeft = False
|
|
elif self.change_x < 0 and not self.lookLeft:
|
|
self.texture = self.texture.flip_left_right()
|
|
self.lookLeft = True
|
|
|
|
|
|
class MainView(arcade.View):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.background_color = (100,100,200)
|
|
self.tilemap = arcade.load_tilemap('./assets/level1.tmx',scaling=1)
|
|
self.flor = self.tilemap.sprite_lists["flor"]
|
|
self.flor = to_iso(self.flor)
|
|
self.walls = self.tilemap.sprite_lists["walls"]
|
|
self.walls = to_iso(self.walls)
|
|
self.objects = self.tilemap.sprite_lists["objects"]
|
|
self.objects = to_iso(self.objects)
|
|
self.players = arcade.SpriteList()
|
|
for obj in self.objects:
|
|
self.players.append(Player(obj))
|
|
|
|
self.camera = arcade.Camera2D()
|
|
|
|
def on_draw(self):
|
|
self.clear()
|
|
self.camera.use()
|
|
self.flor.draw()
|
|
self.walls.draw()
|
|
self.players.draw()
|
|
|
|
def on_update(self,delta_time):
|
|
self.camera.position = 800,0
|
|
self.players.update(delta_time)
|
|
def on_mouse_press(self,x,y,but,mod):
|
|
world_point = self.camera.unproject((x,y))
|
|
tile_col = world_point.x//TILE_WIDTH
|
|
tile_row = world_point.y//TILE_HEIGHT
|
|
sheet = arcade.load_spritesheet('./assets/tiles/64x64.png')
|
|
textures = sheet.get_texture_grid((64,64),16,184)
|
|
self.walls.append(
|
|
arcade.Sprite(
|
|
textures[165],
|
|
center_x=tile_col*TILE_WIDTH + TILE_WIDTH//2,
|
|
center_y=tile_row*TILE_HEIGHT+TILE_HEIGHT
|
|
)
|
|
)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
window = arcade.Window(
|
|
SCREEN_WIDTH,
|
|
SCREEN_HEIGHT,
|
|
'Isometric game',
|
|
fullscreen=SCREEN_FULL)
|
|
view = MainView()
|
|
window.show_view(view)
|
|
arcade.run()
|