zoria/src/game.py

109 lines
3.8 KiB
Python
Raw Normal View History

2020-02-10 13:18:27 -08:00
import pygame
import math, os, sys
2020-02-19 11:26:07 -08:00
2020-02-10 13:18:27 -08:00
# Init
pygame.init()
pygame.font.init()
winsize = [800, 600]
screen = pygame.display.set_mode(winsize, pygame.RESIZABLE)
from . import assets, controller, register, spritesheet
from .map import Map
from .player import Player
2020-02-24 16:33:22 -08:00
pygame.display.set_caption("Zoria")
pygame.display.set_icon(assets.load("icon.png"))
# Constants
SCALE = 2
METER = 32
FPS = 60
2020-02-10 13:18:27 -08:00
2020-02-24 16:57:59 -08:00
# Load all assets
2020-02-24 18:20:51 -08:00
for filename in os.listdir(os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), "assets")):
2020-02-12 13:40:25 -08:00
assets.load(filename)
2020-02-10 13:18:27 -08:00
# Map
2020-02-24 16:46:30 -08:00
map = Map(METER)
2020-03-02 07:53:52 -08:00
# map.load("map.json")
map.generate()
2020-02-12 13:40:25 -08:00
2020-02-24 18:20:51 -08:00
# Player
2020-02-20 21:36:17 -08:00
player = Player()
player.sprite.texture = spritesheet.SpriteSheet(assets.get("character.png"), 32, 48)
player.sprite.set_rect((8, 32, 16, 16))
# TODO: Scale character up x2 (1.5m)
2020-02-20 21:36:17 -08:00
# TODO: Use asset loader for spritesheets
player.sprite.texture.set_animation(0, 0, 0)
player.set_pos(1.1, 1.1)
2020-03-04 12:32:22 -08:00
player.set_pos(map.generator.rooms[0].x + 2, map.generator.rooms[0].y + 2)
2020-03-04 11:23:23 -08:00
2020-02-20 18:59:04 -08:00
CENTER = [winsize[0] / 2, winsize[1] / 2]
BGCOLOR = pygame.Color("#3e1202")
arial = pygame.font.SysFont("Arial", 10)
2020-02-10 13:18:27 -08:00
# Mainloop
2020-02-19 11:26:07 -08:00
clock = pygame.time.Clock()
2020-02-10 13:18:27 -08:00
while 1:
2020-02-19 11:26:07 -08:00
dtime = clock.tick(FPS) / 1000.0
2020-02-10 13:18:27 -08:00
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
elif event.type == pygame.VIDEORESIZE:
winsize = event.size
SCALE = 2 * (winsize[0] / 800)
CENTER = [winsize[0] / 2, winsize[1] / 2]
screen = pygame.display.set_mode(winsize, pygame.RESIZABLE)
2020-02-10 13:18:27 -08:00
screen.fill(BGCOLOR)
2020-02-10 13:18:27 -08:00
2020-02-20 18:59:04 -08:00
player.update(dtime, map)
2020-02-20 21:36:17 -08:00
spritesheet.update(dtime)
2020-02-19 11:10:56 -08:00
2020-02-19 13:19:07 -08:00
# Move the map based on player position
2020-02-20 21:36:17 -08:00
psize = player.sprite.rect.size
camera = [int(CENTER[0] - (psize[0] / 2 * SCALE)), int(CENTER[1] - (psize[1] / 2 * SCALE))]
player_rendered = False
2020-02-19 13:19:07 -08:00
for z in range(len(map.map)):
for y in range(len(map.map[z])):
for x in range(len(map.map[z][y])):
tile = map.get_tile(x, y, z)
if tile:
# NOTE: Rotations are clockwise due to Y+ down rendering
# Will this break if there is no texture for the given rotation? Yes.
# Do I care? No.
texture = assets.get(tile.textures[tile.get_rotation()])
tilesize = texture.get_rect().size
scaledsize = [tilesize[0] * SCALE, tilesize[1] * SCALE]
tilex = (x * METER) - ((tilesize[0] / 2) - (METER / 2))
tiley = (y * METER) - (tilesize[1] - METER)
# TODO: Fix scaling gaps
pos = [
camera[0] + math.floor((tilex - (player.pos.x * METER)) * SCALE),
camera[1] + math.ceil((tiley - (player.pos.y * METER)) * SCALE)
]
# Only render tile if on-screen
if pos[0] + scaledsize[0] >= 0 and pos[0] <= winsize[0] and \
pos[1] + scaledsize[1] >= 0 and pos[1] <= winsize[1]:
screen.blit(pygame.transform.scale(texture, [math.floor(scaledsize[0]), math.ceil(scaledsize[1])]), pos)
# DEBUG
# text = arial.render(str(int(x)) + ", " + str(int(y)), False, (255, 255, 255))
# screen.blit(text, [x * 64 - (player.pos.x * round(SCALE * METER)) + camera[0], y * 64 - (player.pos.y * round(SCALE * METER)) + camera[1]])
2020-02-10 13:18:27 -08:00
if not player_rendered and z == 1 and y == round(player.pos.y + player.sprite.get_rect()[3] / METER):
# Draw player
screen.blit(pygame.transform.scale(player.sprite.texture.frame, [round(SCALE * player.sprite.texture.width), round(SCALE * player.sprite.texture.height)]), camera)
player_rendered = True
pygame.display.update()
# pygame.display.flip()