C# · Chapter 3.5 · Intermediate level

Methods (Player Actions: Jump() & Attack()!)

मेथड्स (हीरो के एक्शन्स: Jump() और Attack()!)

In a real game, every time you tap the Attack or Jump button on your controller, Unity runs a C# Method like Attack() or Jump()! A Method is C#'s word for a reusable function:
• static void Attack() { ... } teaches the Hero how to attack.
• Writing Attack(); inside Main() triggers the sword slash on screen!

असली गेम में जब आप स्क्रीन पर Jump या Attack बटन दबाते हो, तो पीछे C# का Method (मेथड) चलता है! इसे दो आसान हिस्सों में समझो:
• static void Attack() { ... } लिखकर हम हीरो को सिखाते हैं कि हमला कैसे करना है।
• Main() के अंदर Attack(); लिखकर हम हीरो को हुक्म देते हैं कि अभी तलवार चलाओ!

In the classroom you can run and edit this code in your browser, get hints and mark the chapter as done. क्लास में यह code ब्राउज़र में चलाकर बदल सकते हो।

Words and symbols in this chapter

static void Attack()
Defines a reusable action method named Attack (void = it returns nothing, it just performs the action).
Attack नाम का एक्शन मेथड बनाता है (void का मतलब यह कुछ लौटाता नहीं, सिर्फ़ काम करता है)।
Attack();
Calls Attack — the hero swings his sword and the enemy loses 20 HP!
Attack मेथड चलाता है — हीरो तलवार घुमाएगा और दुश्मन की 20 HP घटेगी!
static void Jump() { ... }
A second method we write ourselves — C# has no built-in Jump(), so every action must be defined in the class.
दूसरा मेथड जो हम ख़ुद लिखते हैं — C# में Jump() पहले से नहीं होता, इसलिए हर एक्शन class में बनाना पड़ता है।
class Program { ... }
All methods, including Main(), live inside the braces of the class.
सारे मेथड, Main() समेत, class के braces के अंदर रहते हैं।

Ready example

using System;

class Program {
    static void Jump() {
        Console.WriteLine("Hero jumps over the pit!");
    }

    static void Attack() {
        Console.WriteLine("Sword Slash! -20 HP");
    }

    static void Main() {
        Jump();
        Attack();
        Attack();
    }
}

What you will see: Watch the Hero jump, swing a glowing sword slash (⚡⚔️💥), and drain the Enemy's HP Bar!

प्रिव्यू में हीरो उछलेगा, तलवार चलाएगा (⚡⚔️💥) और सामने दुश्मन की Health Bar घट जाएगी!

Line by line

static void Jump() { ... }
Our own Jump() method — every method you call must be defined inside the class.
हमारा अपना Jump() मेथड — जो भी मेथड बुलाओ, वह class के अंदर बना होना चाहिए।
static void Attack() { ... }
Creates the Attack() method that prints "Sword Slash! -20 HP".
Attack() मेथड बनाया जो "Sword Slash! -20 HP" छापता है।
static void Main() {
The game starts here!
गेम यहाँ से शुरू होता है!
Jump(); / Attack();
Calls the methods: the hero jumps, then slashes (twice in this demo).
मेथड बुलाता है: हीरो उछलता है, फिर तलवार चलाता है (इस डेमो में दो बार)।

Common mistake

✗ Wrong

static void Jump {
    ...
}

✓ Right

static void Jump() {
    ...
}

Method definitions and method calls ALWAYS require parentheses () even when taking zero inputs!

मेथड बनाते और चलाते समय नाम के आगे () लगाना ज़रूरी है — जैसे Jump();!

Your task

Inside class Program, define static void Attack() that prints "Sword Slash! -20 HP", then call Attack(); in Main() after Jump();.

class Program के अंदर static void Attack() बनाओ जो "Sword Slash! -20 HP" छापे, फिर Main() में Jump(); के बाद Attack(); बुलाओ।

  1. Below Jump() (still inside class Program) write static void Attack() { Console.WriteLine("Sword Slash! -20 HP"); }.
    Jump() के नीचे (class Program के अंदर ही) static void Attack() { Console.WriteLine("Sword Slash! -20 HP"); } लिखो।
  2. Inside static void Main(), after Jump();, call Attack();.
    static void Main() के अंदर Jump(); के बाद Attack(); लिखो।
  3. Click ▶ Run & Preview — the hero jumps and slashes!
    ▶ Run & Preview दबाओ — हीरो उछलेगा और तलवार चलाएगा!
Starter code for the task
using System;

class Program {
    static void Jump() {
        Console.WriteLine("Hero jumps over the pit!");
    }

    // TODO 1: yahan static void Attack() method banao
    //         jo Console.WriteLine("Sword Slash! -20 HP"); kare

    static void Main() {
        Jump();
        // TODO 2: yahan Attack(); call karo
    }
}

Do this task in the classroom: your code is checked automatically and you get hints if something is missing.

Recap

Quick quiz

What does void mean in static void Jump()?

static void Jump() में void शब्द का क्या मतलब है?

  1. Method runs forever
  2. Method performs an action without returning a value
  3. Method is deleted
  4. Method returns an integer
Show the answer

Answer: B (Method performs an action without returning a value)

void means the method performs its action and returns nothing.

void का मतलब है मेथड अपना काम करेगा लेकिन कोई संख्या वापस return नहीं करेगा।

Practice problems for this chapter

Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.