Roblox 코딩

로블록스(Roblox) 코딩 실습 8: 다양한 게임 요소와 고급 코딩 기법

runner326 2025. 2. 24. 22:00

로블록스 코딩 실습: 다양한 게임 요소와 고급 코딩 기법 🎮🚀

이번에는 체력 시스템, 레벨 업 시스템, 게임 내 통화(골드) 시스템과 같은 다양한 게임 요소를 추가하는 방법과 고급 코딩 기법을 소개하겠습니다. 이러한 요소들은 게임의 완성도와 플레이어의 몰입도를 더욱 높일 수 있습니다! 😊


1️⃣ 체력 시스템 구현 ❤️

플레이어의 체력을 관리하고, 체력이 0이 되면 게임에서 패배하는 시스템을 구현해보겠습니다.

1. 체력 표시 UI 추가하기

  1. StarterGui에 "Insert Object" -> ScreenGui를 추가합니다.
  2. ScreenGui 안에 TextLabel을 추가하고 이름을 **"HealthLabel"**로 변경합니다.
  3. Text 속성을 "체력: 100"으로 설정합니다.

2. 체력 시스템 스크립트 작성

  1. StarterPlayer에 "Insert Object" -> LocalScript를 추가합니다.
  2. 다음 코드를 입력합니다:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local healthLabel = script.Parent:WaitForChild("HealthLabel")

local humanoid = character:WaitForChild("Humanoid")

humanoid.HealthChanged:Connect(function(newHealth)
    healthLabel.Text = "체력: " .. math.floor(newHealth)
    
    if newHealth <= 0 then
        print(player.Name .. "님이 패배했습니다.")
    end
end)

코드 설명

  • Humanoid.HealthChanged: 플레이어의 체력이 변경될 때 호출됩니다.
  • math.floor(): 체력을 정수 형태로 표시합니다.
  • newHealth <= 0: 체력이 0 이하가 되면 패배 메시지를 출력합니다.

2️⃣ 레벨 업 시스템 구현 🏆

플레이어가 경험치를 얻어 레벨을 올리는 시스템을 구현해보겠습니다.

1. 경험치와 레벨 표시

  1. ServerScriptService에 "Insert Object" -> Script를 추가합니다.
  2. 다음 코드를 입력합니다:
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local level = Instance.new("IntValue")
    level.Name = "Level"
    level.Value = 1
    level.Parent = leaderstats

    local xp = Instance.new("IntValue")
    xp.Name = "XP"
    xp.Value = 0
    xp.Parent = leaderstats
end)

경험치 획득과 레벨업 스크립트

local function gainXP(player, amount)
    local xp = player.leaderstats:FindFirstChild("XP")
    local level = player.leaderstats:FindFirstChild("Level")
    
    if xp and level then
        xp.Value = xp.Value + amount
        print(player.Name .. "님이 " .. amount .. "XP를 획득했습니다.")
        
        if xp.Value >= level.Value * 100 then  -- 레벨업 기준
            xp.Value = xp.Value - level.Value * 100
            level.Value = level.Value + 1
            print(player.Name .. "님이 레벨업했습니다! 현재 레벨: " .. level.Value)
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    wait(2)
    gainXP(player, 120)  -- 테스트로 120XP 추가
end)

코드 설명

  • xp.Value >= level.Value * 100: 현재 레벨에 따라 경험치 기준이 달라집니다.
  • 레벨업 시 경험치 초기화: 초과된 경험치를 유지한 채 다음 레벨로 이동합니다.

3️⃣ 게임 내 통화(골드) 시스템 구현 💰

플레이어가 골드를 획득하고 아이템 구매에 사용할 수 있는 시스템을 구현해보겠습니다.

1. 골드 시스템 추가

  1. ServerScriptService에 "Insert Object" -> Script를 추가합니다.
  2. 다음 코드를 입력합니다:
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local gold = Instance.new("IntValue")
    gold.Name = "Gold"
    gold.Value = 100
    gold.Parent = leaderstats
end)

2. 골드 획득 스크립트

local function gainGold(player, amount)
    local gold = player.leaderstats:FindFirstChild("Gold")
    if gold then
        gold.Value = gold.Value + amount
        print(player.Name .. "님이 " .. amount .. "골드를 획득했습니다.")
    end
end

game.Players.PlayerAdded:Connect(function(player)
    wait(2)
    gainGold(player, 50)  -- 테스트로 50 골드 추가
end)

4️⃣ 고급 코딩 기법 활용 💡

1. 객체 지향 방식으로 코딩하기

local PlayerClass = {}
PlayerClass.__index = PlayerClass

function PlayerClass.new(name, level)
    local self = setmetatable({}, PlayerClass)
    self.name = name
    self.level = level or 1
    return self
end

function PlayerClass:levelUp()
    self.level = self.level + 1
    print(self.name .. "님이 레벨업했습니다. 현재 레벨: " .. self.level)
end

local player1 = PlayerClass.new("John", 3)
player1:levelUp()
  • 객체 지향 프로그래밍: 클래스를 사용해 데이터와 동작을 하나로 묶어 관리합니다.

마무리 ✨

이번 실습에서는 체력 시스템, 레벨 업 시스템, 게임 내 통화 시스템과 같은 다양한 게임 요소를 구현해 보았습니다. 또한 객체 지향 방식과 같은 고급 코딩 기법을 활용해 코드를 더 구조적으로 관리하는 방법을 배웠습니다. 이를 기반으로 더욱 완성도 높은 게임을 개발해보세요! 😊

다음에는 더욱 흥미로운 게임 메커니즘과 AI 기능을 소개해드리겠습니다! 🚀

반응형