לדלג לתוכן

הקדמה

  • אתה הולך לבנות משחק RPG עם פייתון

דרישות הפרויקט

  • פתח משחק הרפתקה עם דמויות, אוייבים, מקומות שאפשר לחקור ועוד.
  • תעצב/תתכנן את הפרויקט שלך והמחלקות שלו לפני שאתה מתחיל אותו.
  • תוסיף כל פיצ'ר שאתה רוצה
  • כאשר אתה מעצב/מתכנן את הפרויקט זכור את עקרונות SOLID, תחשוב על איזה פיצ'רים אולי בעתיד תרצה להוסיף לפרויקט בעתיד והפוך את הפרויקט לקל ופשוט.

דוגמה

דוגמה לעיצוב של פרויקט כזה:
- במשחק יהיה לנו מחלקת שחקן (character) ויהיה לנו מחלקת אוייב (enemy), והם יכולים להילחם.
- שמחסלים אויב, הוא זורק loot, והשחקן יכול לקחת אותו.
- במשחק הזה יש כמה מפות. בכל מפה יש אוייבים שונים, פריטים שונים שאפשר לאסוף, ותיאור למקום.
הנה דוגמה לרשימת מחלוקת של משחק כזה
Pasted image 20240315175852.png
דוגמה לקוד חלקי (הקוד הזה לא עובד) שממש חלק מהדברים במשחק כזה:

import random  
import abc  


class Entity(abc.ABC):  
    def __init__(self, name, health, attack_power, defense):  
        self.name = name  
        self.health = health  
        self.attack_power = attack_power  
        self.defense = defense  

    @abc.abstractmethod  
    def attack(self, entity):  
        pass  


class Character(Entity):  
    def __init__(self, name, health=100, attack_power=10, defense=5):  
        super().__init__(name, health, attack_power, defense)  
        self.inventory = []  

    def attack(self, enemy):  
        damage = max(0, self.attack_power - enemy.defense)  
        enemy.health -= damage  
        print(f"{self.name} attacks {enemy.name} for {damage} damage.")  

    def use_item(self, item):  
        if item in self.inventory:  
            self.inventory.remove(item)  
            # Apply item effect  
            print(f"{self.name} uses {item.name}.")  
        else:  
            print("Item not found in inventory.")  

    # Other methods for inventory management, movement, etc.  


class Enemy(Entity):  
    def __init__(self, name, health=50, attack_power=8, defense=3):  
        super().__init__(name, health, attack_power, defense)  
        self.loot = []  

    def attack(self, character):  
        damage = max(0, self.attack_power - character.defense)  
        character.health -= damage  
        print(f"{self.name} attacks {character.name} for {damage} damage.")  

    def drop_loot(self):  
        return random.choice(self.loot)  


class Item:  
    def __init__(self, name, description):  
        self.name = name  
        self.description = description  
        # Add more attributes as needed  

    def use(self, character):  
        # Define item effects  
        pass  


class Location:  
    def __init__(self, name, description, enemies=[], items=[]):  
        self.name = name  
        self.description = description  
        self.enemies = enemies  
        self.items = items  

    def explore(self):  
        print(self.description)  
        for enemy in self.enemies:  
            print(f"You encounter a {enemy.name}!")  
            # Implement combat logic  
        for item in self.items:  
            print(f"You find a {item.name}.")  
            # Allow the player to pick up the item  


def main():  
    player = Character("Player")  

    enemy1 = Enemy("Goblin")  
    enemy2 = Enemy("Orc")  

    item1 = Item("Potion", "Restores 20 health")  
    item2 = Item("Sword", "Increases attack power")  

    location1 = Location("Forest", "You are in a dark forest.", [enemy1, enemy2], [item1, item2])  

    print("Welcome to the Text-Based RPG!")  
    print("=================================")  

    while True:  
        print("\n1. Explore")  
        print("2. View Inventory")  
        print("3. Exit")  

        choice = input("Enter your choice: ")  

        if choice == '1':  
            location1.explore()  

        elif choice == '2':  
            print("Inventory:")  
            for item in player.inventory:  
                print(item.name)  

        elif choice == '3':  
            print("Exiting game...")  
            break  

        else:  
            print("Invalid choice. Please enter a valid option.")  

if __name__ == "__main__":  
    main()

מה מצופה מכם

ליזום!
- יהיו יצירתיים, נסו לחשוב על משחק בעצמכם ופתחו אותו.
- אם אתם לא יצירתיים כמוני, אתם מוזמנים לקחת השראה מהמשחק החלקי שבניתי למעלה ולעצב אחד דומה.
- אתם יכולים גם לקחת את המשחק החלקי שבניתי למעלה ולפתח אותו, למשל: פתחו כמה סוגים של אוייבים שונים, פתחו סימולציית קרב מורכבת.
- אתם יכולים גם לממש את כל הפיצ'רים החסרים במשחק החלקי שכתבתי למעלה, איפה שהשארתי הערות.

בהצלחה.