Upload files to "/"

sync 1
main
Filips Kalniņš 2026-02-11 08:03:17 +00:00
parent f4bc953538
commit 42a91a1bc8
3 changed files with 217 additions and 16 deletions

21
game.py
View File

@ -10,6 +10,10 @@ from enemy_manager import EnemyManager
class Game: class Game:
def __init__(self): def __init__(self):
pygame.init() # <-- IMPORTANT
self.show_inventory = False
# ---------------------------------- # ----------------------------------
# Display Setup # Display Setup
# ---------------------------------- # ----------------------------------
@ -32,7 +36,6 @@ class Game:
# ---------------------------------- # ----------------------------------
# Debug # Debug
# ----------------------------------
self.font = pygame.font.SysFont("consolas", 18) self.font = pygame.font.SysFont("consolas", 18)
# ========================================================== # ==========================================================
@ -64,12 +67,19 @@ class Game:
if event.key == pygame.K_ESCAPE: if event.key == pygame.K_ESCAPE:
self.paused = not self.paused 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 # Mouse input for breaking/placing
# -------------------------------
if event.type == pygame.MOUSEBUTTONDOWN: if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left click if event.button == 1: # Left click
self.world.break_block( self.world.break_block(
pygame.mouse.get_pos(), pygame.mouse.get_pos(),
self.camera self.camera,
self.inventory
) )
if event.button == 3: # Right click if event.button == 3: # Right click
@ -82,6 +92,10 @@ class Game:
# UPDATE # UPDATE
# ========================================================== # ==========================================================
def update(self, dt): def update(self, dt):
# If inventory is open, pause gameplay
if self.show_inventory:
return
self.player.update(dt, self.world) self.player.update(dt, self.world)
self.camera.update() self.camera.update()
self.enemy_manager.update(dt) self.enemy_manager.update(dt)
@ -100,9 +114,10 @@ class Game:
self.player.draw(self.screen, self.camera) self.player.draw(self.screen, self.camera)
# UI # UI
if self.show_inventory:
self.inventory.draw(self.screen) self.inventory.draw(self.screen)
self.draw_ui()
self.draw_ui()
pygame.display.flip() pygame.display.flip()
# ========================================================== # ==========================================================

173
settings.py 100644
View File

@ -0,0 +1,173 @@
# ==========================================
# WINDOW / DISPLAY SETTINGS
# ==========================================
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
FPS = 60
VSYNC = True
GAME_TITLE = "Terraria Clone"
BACKGROUND_COLOR = (135, 206, 235) # Sky blue
# ==========================================
# TILE SETTINGS
# ==========================================
TILE_SIZE = 32
CHUNK_SIZE = 16 # 16x16 tiles per chunk
RENDER_DISTANCE = 3 # how many chunks visible around player
# Tile IDs
# Tiles
AIR = 0
DIRT = 1
GRASS = 2
STONE = 3
WOOD = 4
LEAVES = 5
IRON_ORE = 6
GOLD_ORE = 7
COPPER_ORE = 8
COAL_ORE = 9
# Items
ITEM_WOOD = 100
ITEM_STONE = 101
ITEM_IRON = 102
ITEM_GOLD = 103
ITEM_COPPER = 104
ITEM_COAL = 105
# Tile properties
TILE_PROPERTIES = {
AIR: {"solid": False, "color": (0, 0, 0), "drop": None},
DIRT: {"solid": True, "color": (139, 69, 19), "drop": ITEM_STONE},
GRASS: {"solid": True, "color": (34, 177, 76), "drop": ITEM_STONE},
STONE: {"solid": True, "color": (100, 100, 100), "drop": ITEM_STONE},
WOOD: {"solid": True, "color": (160, 82, 45), "drop": ITEM_WOOD},
LEAVES: {"solid": False, "color": (34, 139, 34), "drop": None},
# ORES
IRON_ORE: {"solid": True, "color": (180, 180, 180), "drop": ITEM_IRON},
GOLD_ORE: {"solid": True, "color": (255, 215, 0), "drop": ITEM_GOLD},
COPPER_ORE: {"solid": True, "color": (210, 120, 60), "drop": ITEM_COPPER},
COAL_ORE: {"solid": True, "color": (40, 40, 40), "drop": ITEM_COAL},
}
# ==========================================
# WORLD GENERATION
# ==========================================
WORLD_WIDTH = 200 # in tiles
WORLD_HEIGHT = 100 # in tiles
SEED = 42
SURFACE_LEVEL = 40
CAVE_THRESHOLD = 0.4
ORE_THRESHOLD = 0.75
NOISE_SCALE = 0.05
OCTAVES = 4
# ==========================================
# PLAYER SETTINGS
# ==========================================
PLAYER_WIDTH = 28
PLAYER_HEIGHT = 48
PLAYER_SPEED = 250
PLAYER_ACCELERATION = 2000
PLAYER_FRICTION = -0.15
GRAVITY = 1500
MAX_FALL_SPEED = 1000
JUMP_FORCE = -500
DOUBLE_JUMP = False
MAX_HEALTH = 100
# ==========================================
# CAMERA SETTINGS
# ==========================================
CAMERA_SMOOTHING = 0.1
CAMERA_OFFSET_Y = -100
# ==========================================
# INVENTORY SETTINGS
# ==========================================
INVENTORY_SIZE = 40
HOTBAR_SIZE = 10
STACK_LIMIT = 999
INVENTORY_SLOT_SIZE = 40
INVENTORY_PADDING = 4
# ==========================================
# BLOCK BREAKING / PLACING
# ==========================================
BREAK_RANGE = 5 # tiles
BREAK_TIME = {
DIRT: 0.3,
GRASS: 0.3,
STONE: 0.8,
WOOD: 0.5,
IRON_ORE: 1.2,
GOLD_ORE: 1.5,
COPPER_ORE: 1.0,
COAL_ORE: 0.9,
}
PLACE_RANGE = 5
# ==========================================
# ENEMY SETTINGS
# ==========================================
MAX_ENEMIES = 10
ENEMY_SPAWN_RATE = 5 # seconds
ENEMY_SPEED = 100
ENEMY_DAMAGE = 10
ENEMY_HEALTH = 50
# ==========================================
# PHYSICS SETTINGS
# ==========================================
TERMINAL_VELOCITY = 1200
COLLISION_STEPS = 4
# ==========================================
# LIGHTING (for future use)
# ==========================================
ENABLE_LIGHTING = False
LIGHT_RADIUS = 5
# ==========================================
# DEBUG SETTINGS
# ==========================================
DEBUG_MODE = True
SHOW_FPS = True
SHOW_COLLIDERS = False
SHOW_CHUNK_BORDERS = False

View File

@ -1,25 +1,38 @@
import pygame
from settings import * from settings import *
class Tile: class Tile:
def __init__(self, id, name, solid, color, hardness=1.0, drop=None): def __init__(self, tile_id, name, collidable, color, hardness=1.0, drop=None):
self.id = id self.id = tile_id
self.name = name self.name = name
self.solid = solid self.collidable = collidable
self.color = color self.color = color
self.hardness = hardness self.hardness = hardness
self.drop = drop # item id dropped when broken self.drop = drop
# Register tiles here
TILE_TYPES = { TILE_TYPES = {
AIR: Tile(AIR, "Air", False, (0, 0, 0), hardness=0), AIR: Tile(AIR, "Air", False, (0,0,0), hardness=0, drop=None),
DIRT: Tile(DIRT, "Dirt", True, (139, 69, 19), hardness=0.3, drop=DIRT), DIRT: Tile(DIRT, "Dirt", True, (139,69,19), hardness=0.4, drop=ITEM_STONE),
GRASS: Tile(GRASS, "Grass", True, (34, 177, 76), hardness=0.3, drop=DIRT), GRASS: Tile(GRASS, "Grass", True, (34,177,76), hardness=0.4, drop=ITEM_STONE),
STONE: Tile(STONE, "Stone", True, (100, 100, 100), hardness=0.8, drop=STONE), STONE: Tile(STONE, "Stone", True, (100,100,100), hardness=0.8, drop=ITEM_STONE),
WOOD: Tile(WOOD, "Wood", True, (160,82,45), hardness=0.5, drop=ITEM_WOOD),
LEAVES: Tile(LEAVES, "Leaves", False, (34,139,34), hardness=0.2, drop=None),
IRON_ORE: Tile(IRON_ORE, "Iron Ore", True, (180,180,180), hardness=1.2, drop=ITEM_IRON),
GOLD_ORE: Tile(GOLD_ORE, "Gold Ore", True, (255,215,0), hardness=1.5, drop=ITEM_GOLD),
COPPER_ORE: Tile(COPPER_ORE, "Copper Ore", True, (210,120,60), hardness=1.0, drop=ITEM_COPPER),
COAL_ORE: Tile(COAL_ORE, "Coal Ore", True, (40,40,40), hardness=0.9, drop=ITEM_COAL),
} }
# ----------------------------
# Compatibility for world.py
# ----------------------------
TILE_PROPERTIES = {
tile_id: {
"solid": tile.collidable,
"color": tile.color,
"drop": tile.drop
}
for tile_id, tile in TILE_TYPES.items()
}
def get_tile(tile_id): def get_tile(tile_id):
return TILE_TYPES.get(tile_id, TILE_TYPES[AIR]) return TILE_TYPES.get(tile_id, TILE_TYPES[AIR])