144 lines
4.4 KiB
Python
144 lines
4.4 KiB
Python
import pygame
|
|
from settings import *
|
|
|
|
from world import World
|
|
from player import Player
|
|
from camera import Camera
|
|
from inventory import Inventory
|
|
from enemy_manager import EnemyManager
|
|
|
|
|
|
class Game:
|
|
def __init__(self):
|
|
pygame.init() # <-- IMPORTANT
|
|
|
|
self.show_inventory = False
|
|
|
|
# ----------------------------------
|
|
# Display Setup
|
|
# ----------------------------------
|
|
flags = pygame.SCALED
|
|
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), flags)
|
|
pygame.display.set_caption(GAME_TITLE)
|
|
|
|
self.clock = pygame.time.Clock()
|
|
self.running = True
|
|
self.paused = False
|
|
|
|
# ----------------------------------
|
|
# Core Systems
|
|
# ----------------------------------
|
|
self.world = World()
|
|
self.player = Player(100, 100)
|
|
self.camera = Camera(self.player)
|
|
self.inventory = Inventory()
|
|
self.enemy_manager = EnemyManager(self.world, self.player)
|
|
|
|
# ----------------------------------
|
|
# Debug
|
|
self.font = pygame.font.SysFont("consolas", 18)
|
|
|
|
# ==========================================================
|
|
# MAIN LOOP
|
|
# ==========================================================
|
|
def run(self):
|
|
while self.running:
|
|
dt = self.clock.tick(FPS) / 1000
|
|
|
|
self.handle_events()
|
|
|
|
if not self.paused:
|
|
self.update(dt)
|
|
|
|
self.draw()
|
|
|
|
pygame.quit()
|
|
|
|
# ==========================================================
|
|
# EVENTS
|
|
# ==========================================================
|
|
def handle_events(self):
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
self.running = False
|
|
|
|
if event.type == pygame.KEYDOWN:
|
|
if event.key == pygame.K_ESCAPE:
|
|
self.paused = not self.paused
|
|
|
|
# Toggle inventory
|
|
if event.key == pygame.K_i:
|
|
self.show_inventory = not self.show_inventory
|
|
|
|
# -------------------------------
|
|
# Mouse input for breaking/placing
|
|
# -------------------------------
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
if event.button == 1: # Left click
|
|
self.world.break_block(
|
|
pygame.mouse.get_pos(),
|
|
self.camera,
|
|
self.inventory
|
|
)
|
|
|
|
if event.button == 3: # Right click
|
|
self.world.place_block(
|
|
pygame.mouse.get_pos(),
|
|
self.camera
|
|
)
|
|
|
|
# ==========================================================
|
|
# UPDATE
|
|
# ==========================================================
|
|
def update(self, dt):
|
|
# If inventory is open, pause gameplay
|
|
if self.show_inventory:
|
|
return
|
|
|
|
self.player.update(dt, self.world)
|
|
self.camera.update()
|
|
self.enemy_manager.update(dt)
|
|
|
|
# ==========================================================
|
|
# DRAW
|
|
# ==========================================================
|
|
def draw(self):
|
|
self.screen.fill(BACKGROUND_COLOR)
|
|
|
|
# Draw world relative to camera
|
|
self.world.draw(self.screen, self.camera)
|
|
|
|
# Draw entities
|
|
self.enemy_manager.draw(self.screen, self.camera)
|
|
self.player.draw(self.screen, self.camera)
|
|
|
|
# UI
|
|
if self.show_inventory:
|
|
self.inventory.draw(self.screen)
|
|
|
|
self.draw_ui()
|
|
pygame.display.flip()
|
|
|
|
# ==========================================================
|
|
# UI / DEBUG
|
|
# ==========================================================
|
|
def draw_ui(self):
|
|
if SHOW_FPS:
|
|
fps = self.clock.get_fps()
|
|
fps_text = self.font.render(f"FPS: {int(fps)}", True, (255, 255, 255))
|
|
self.screen.blit(fps_text, (10, 10))
|
|
|
|
if DEBUG_MODE:
|
|
debug_text = self.font.render(
|
|
f"Player: ({int(self.player.rect.x)}, {int(self.player.rect.y)})",
|
|
True,
|
|
(255, 255, 255)
|
|
)
|
|
self.screen.blit(debug_text, (10, 30))
|
|
|
|
if self.paused:
|
|
pause_text = self.font.render("PAUSED", True, (255, 0, 0))
|
|
rect = pause_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
|
|
self.screen.blit(pause_text, rect)
|