Batalla: Por Terra
// Inicializar resetGame(); </script> </body> </html> import random import os class Terrain: PLAIN = "name": "Plain", "dmg_atk": 1.0, "dmg_def": 1.0, "def_bonus": 0 FOREST = "name": "Forest", "dmg_atk": 0.9, "dmg_def": 1.1, "def_bonus": 2 HILL = "name": "Hill", "dmg_atk": 1.15, "dmg_def": 0.95, "def_bonus": 1
Puedes copiar el código HTML en un archivo .html y abrirlo en cualquier navegador para jugar. El script de Python se ejecuta en terminal.
// Tipos de unidad const UNITS = INFANTRY: name: "⚔️", baseAtk: 8, baseDef: 5, range: 1, hp: 20, icon: "⚔️" , ARCHER: name: "🏹", baseAtk: 6, baseDef: 3, range: 3, hp: 15, icon: "🏹" , CAVALRY: name: "🐎", baseAtk: 10, baseDef: 4, range: 1, hp: 18, icon: "🐎" ; batalla por terra
// Lógica de selección y ataque function handleCellClick(x, y) if (checkVictory()) return; const cell = grid[x][y]; // Si tenemos unidad seleccionada if (selectedUnit) const sel = selectedUnit; if (cell.side && cell.side !== currentTurn) // Atacar attack(sel.x, sel.y, x, y); selectedUnit = null; updateUI(); const winner = checkVictory(); if (!winner) // Después de atacar no se cambia turno automáticamente (opcional, puedes cambiarlo) // Por diseño: ataque consume turno? En muchos juegos sí. Aquí decidimos que después de atacar termina turno // Descomentar si quieres que atacar termine turno: endTurn(); updateUI(); else // Selección inválida addLog("❌ No puedes atacar ahí"); selectedUnit = null; updateUI(); // Si no hay selección, seleccionar unidad propia si es el turno correcto else if (cell.side === currentTurn && cell.unit !== null) selectedUnit = x, y ; addLog(`✅ Unidad $cell.unit.icon seleccionada en ($x,$y)`); updateUI(); else addLog("⚠️ Selecciona una unidad de tu ejército.");
def __str__(self): return f"self.iconself.type[0]" class LandBattle: def (self, size=8): self.size = size self.grid = [[None for _ in range(size)] for _ in range(size)] self.terrain = [[self.random_terrain() for _ in range(size)] for _ in range(size)] self.current_turn = "attacker" self.setup_armies() En muchos juegos sí
def attack(self, ax, ay, dx, dy): attacker = self.grid[ax][ay] defender = self.grid[dx][dy] if not attacker or not defender: return False, "No hay unidad" if attacker.side == defender.side: return False, "No puedes atacar aliados" if attacker.side != self.current_turn: return False, "No es tu turno" if not self.in_range(ax, ay, dx, dy, attacker.range): return False, "Fuera de rango" damage = self.calculate_damage(attacker, defender, self.terrain[ax][ay], self.terrain[dx][dy]) defender.hp -= damage msg = f"attacker.icon ataca a defender.icon y causa damage de daño!" if defender.hp <= 0: msg += f" defender.icon ha muerto!" self.grid[dx][dy] = None return True, msg
class Unit: def (self, unit_type, side): self.type = unit_type self.side = side if unit_type == "Infantry": self.atk, self.defense, self.range, self.max_hp = 8, 5, 1, 20 self.icon = "⚔️" elif unit_type == "Archer": self.atk, self.defense, self.range, self.max_hp = 6, 3, 3, 15 self.icon = "🏹" else: # Cavalry self.atk, self.defense, self.range, self.max_hp = 10, 4, 1, 18 self.icon = "🐎" self.hp = self.max_hp Terminar turno") print("3
def play(self): while True: self.display() if self.check_victory(): break print("\nOpciones: ") print("1. Atacar (coordenadas atacante x y | defensor x y)") print("2. Terminar turno") print("3. Rendirse") opt = input("Elige: ") if opt == "3": print(f"self.current_turn se rinde. Batalla terminada.") break elif opt == "2": self.change_turn() continue elif opt == "1": try: ax, ay, dx, dy = map(int, input("Atacante (x y) y Defensor (x y): ").split()) if 0<=ax<self.size and 0<=ay<self.size and 0<=dx<self.size and 0<=dy<self.size: success, msg = self.attack(ax, ay, dx, dy) print(msg) if success: if self.check_victory(): break self.change_turn() else: print("Coordenadas inválidas") except: print("Formato incorrecto. Ejemplo: 1 0 6 7") input("\nPresiona Enter para continuar...") if == " main ": game = LandBattle(8) game.play() Características del Feature "Batalla por Tierra" ✅ Campo de batalla con terreno variable ✅ Dos facciones (Atacante/Defensor) ✅ Unidades con atributos distintos (daño, defensa, rango, HP) ✅ El terreno modifica el daño y defensa ✅ Sistema por turnos ✅ Interfaz visual para web o consola