Coders Packet

Like Battle Royal Game created using Python

By Ashish Kumar

A battle royal game using Python. We have used object-oriented concepts of Python to easily implement the complex logic.

We all know about the trend of Battle Royal games in today's world. Games like COD Warzone, BGMI, PUBG, Apex Legends, Fortnite, etc. are very famous among gamers, and the company owning such games are making millions. So, I have developed a Python script to develop a game like Battle Royal and that is why the title of our project is "Like Battle Royal".

The game is having one player and one enemy. The enemy attacks to reduce the Player's health point (HP). Similarly, the player attacks to reduce the enemy's health point (HP). The player can also use magic points (MP) to reduce the enemy's health or to increase the health of the player. The decision about the player should use an attack, healing power, or magic power depends on the player who is playing the game. On the other hand, the decision about the enemy should use fire or attack depends on program logic. The one who is reaching the 0 health point first will be considered defeated.

Programming Language used?

Python

How to run the project?

The proper order of files and folders to run this project is like this, the main.py file and classes folder should be inside the like_battle_royal file. Then, the classes file should contain __init. py__, game.py, inventory.py, and magic.py files. Once, all the things are done properly we can run the main.py file to observe the output. I have used the Visual Studio Code terminal to run the program. Or simply, we can download and unzip the attached file. After this, we should run the main.py file to observe the output.

The main.py file contents are given below: - 

from classes.game import Person, bcolors
from classes.magic import Spell
from classes.inventory import Item

#Create black magic
fire=Spell("Fire",10,100, "black")
thunder=Spell("Thunder",10,100, "black")
blizzard=Spell("Blizzard",10,100, "black")
meteror=Spell("Meteror",20,200, "black")
quake=Spell("Quake",14,140, "black")

#Create white magic
cure=Spell("Cure",12,120,"white")
cura=Spell("Cura",18,200,"white")

#Create some items
potion=Item("Potion","potion","Heals 50 HP",50)
hipotion=Item("Hi-Potion","potion","Heals 100 HP",100)
superpotion=Item("Super Potion","potion","Heals 500 HP", 500)
elixer=Item("Elixer","elixer","Fully restores HP/MP of one party member",9999)
hielixer=Item("MegaElixer","elixer","Fully restores party's HP/MP", 9999)

grenade=Item("Grenade", "attack", "Deals 500 damage", 500)


player_spells=[fire,thunder,blizzard,meteror,cure,cura]
player_items=[{"item":potion,"quantity":15},{"item":hipotion,"quantity":5},
             {"item":superpotion,"quantity":5},{"item":elixer, "quantity":5},
             {"item":hielixer,"quantity":5},{"item":grenade,"quantity":5}]
player=Person(460,65,60,34,player_spells,player_items)
enemy=Person(1200,65,45,25,player_spells,[])

running=True

print(bcolors.FAIL+bcolors.BOLD+"AN ENEMY ATTACKS!"+ bcolors.ENDC)

while running:
    print("==============================")
    player.choose_action()
    choice=input("Choose action:")
    index=int(choice)-1

    if index==0:
        dmg=player.generate_damage()
        enemy.take_damage(dmg)
        print("You attacked for", dmg)
    elif index==1:
        player.choose_magic()
        magic_choice=int(input("Choose magic:"))-1

        if magic_choice==-1:
            continue

        spell=player.magic[magic_choice]
        magic_dmg=spell.generate_damage()

        current_mp=player.get_mp()

        if spell.cost>current_mp:
            print(bcolors.FAIL+ "\nNot enough MP\n" + bcolors.ENDC)
            continue

        player.reduce_mp(spell.cost)

        if spell.type=="white":
            player.heal(magic_dmg)
            print(bcolors.OKBLUE+ "\n" + spell.name + " heal for", str(magic_dmg), "HP." + bcolors.ENDC)
        elif spell.type=="black":
            enemy.take_damage(magic_dmg)
            print(bcolors.OKBLUE + "\n" + spell.name + " deals", str(magic_dmg), "points of damage")
        
    elif index==2:
        player.choose_item()
        item_choice=int(input("Choose item: "))-1

        if item_choice==-1:
            continue

        item=player.items[item_choice]["item"]
        if player.items[item_choice]["quantity"]==0:
            print(bcolors.FAIL + "\n" + "None left..."+ bcolors.ENDC)
            continue

        player.items[item_choice]["quantity"]-=1



        if item.type=="potion":
            player.heal(item.prop)
            print(bcolors.OKGREEN + "\n" + item.name + " heals for", str(item.prop), "HP" + bcolors.ENDC)
        elif item.type=="elixer":
            player.hp=player.maxhp
            player.mp=player.maxmp
            print(bcolors.OKGREEN + "\n" + item.name + " fully restore HP/MP"+ bcolors.ENDC)
        elif item.type=="attack":
            enemy.take_damage(item.prop)
            print(bcolors.FAIL + "\n" + item.name + " deals", str(item.prop), "points of damage"+ bcolors.ENDC)

    enemy_choice=1
    if enemy.get_hp()<enemy.get_max_hp()/5:
        enemy_choice=1
    else:
        enemy_choice=0

    if enemy_choice==0 or enemy.get_mp()<10:
        enemy_dmg=enemy.generate_damage()
        player.take_damage(enemy_dmg)
        print("Enemy attacks for", enemy_dmg)
    else:
        magic_choice=0
        spell=enemy.magic[magic_choice]
        magic_dmg=spell.generate_damage()

        current_mp=enemy.get_mp()

        if spell.cost>current_mp:
            print(bcolors.FAIL+ "\nNot enough MP for enemy \n" + bcolors.ENDC)
            continue

        enemy.reduce_mp(spell.cost)

        player.take_damage(magic_dmg)
        print(bcolors.FAIL + "\n" + spell.name + " deals", str(magic_dmg), "points of damage to you ")
    print("=============================")
    print("Enemy HP:", bcolors.FAIL +str(enemy.get_hp()) + "/" + str(enemy.get_max_hp()) + bcolors.ENDC +"\n")
    print("Your HP:", bcolors.OKGREEN + str(player.get_hp()) + "/" + str(player.get_max_hp()) + bcolors.ENDC + "\n")
    print("Your MP:", bcolors.OKBLUE + str(player.get_mp()) + "/" + str(player.get_max_mp()) + bcolors.ENDC+ "\n")
    if enemy.get_hp()==0:
        print(bcolors.OKGREEN + "You win!" + bcolors.ENDC)
        running=False
    elif player.get_hp()==0:
        print(bcolors.FAIL + "Your enemy has defeated you!" + bcolors.ENDC)
        running=False

The above-mentioned Python code is used to import all the classes created in other files like the game.py file, the inventory.py file, and the magic.py file. It also implements the complex logic which is used to manipulate the HP and the MP of the player and the enemy using a while loop and many if-else statements. Initialization of the player and the enemy, to give the final results, etc. are other works done by this file.

The game.py file contents are given below: -

import random
import pprint
from .magic import Spell

class bcolors:
    HEADER='\033[95m'
    OKBLUE='\033[94m'
    OKGREEN='\033[92m'
    WARNING='\033[93m'
    FAIL='\033[91m'
    ENDC='\033[0m'
    BOLD='\033[1m'
    UNDEFINED='\033[4m'


class Person:
    def __init__(self,hp,mp,atk,df,magic, items):
        self.maxhp=hp
        self.hp=hp
        self.maxmp=mp
        self.mp=mp
        self.atkl=atk-10
        self.atkh=atk+10
        self.df=df
        self.magic=magic
        self.items=items
        self.actions=["Attack","Magic", "Items"]
    
    def generate_damage(self):
        return random.randrange(self.atkl,self.atkh)

    def reduce_mp(self,cost):
        self.mp-=cost

    def take_damage(self,dmg):
        self.hp-=dmg
        if self.hp<0:
            self.hp=0
        return self.hp

    def heal(self,dmg):
        self.hp+=dmg
        if self.hp>self.maxhp:
            self.hp=self.maxhp

    def get_hp(self):
        return self.hp
    
    def get_max_hp(self):
        return self.maxhp

    def get_mp(self):
        return self.mp

    def get_max_mp(self):
        return self.maxmp

    def get_spell_mp_cost(self,i):
        return self.magic[i]["cost"]

    def choose_action(self):
        i=1
        print("\n" + bcolors.OKBLUE + bcolors.BOLD + "ACTIONS:" + bcolors.ENDC)
        print("Actions")
        for item in self.actions:
            print("   "+str(i)+":",item)
            i+=1

    def choose_magic(self):
        i=1
        print("\n"+bcolors.OKBLUE + bcolors.BOLD + "Magic" + bcolors.ENDC)
        for spell in self.magic:
            print("    "+str(i)+":",spell.name, "(cost:", str(spell.cost) + ")")
            i+=1

    def choose_item(self):
        i=1
        print("\n"+bcolors.OKGREEN + bcolors.BOLD + "ITEMS" + bcolors.ENDC)
        for item in self.items:
            print("    "+str(i)+ ":", item["item"].name + ":", item["item"].description, "(x" + str(item["quantity"]) + ")")
            i+=1

The above Python code is used to provide blueprints of text-decoration and person (player and enemy). 

The inventory.py file contents are given below: -

class Item:
    def __init__(self,name,type,description,prop):
        self.name=name
        self.type=type
        self.description=description
        self.prop=prop

The above code is used to provide the blueprint of items used both by the player and the enemy.  

The magic.py file contents are given below: -

import random
class Spell:
    def __init__(self,name,cost,dmg,type):
        self.name=name
        self.cost=cost
        self.dmg=dmg
        self.type=type

    def generate_damage(self):
        low=self.dmg-15
        high=self.dmg+15
        return random.randrange(low,high)
        

The above Python code is used to create the blueprint of different magics used by the player and the enemy.

 The __inti. py__ file does not contain any code. It is an empty file.

That's all, Thank you!

 

 

 

 

 

Download project

Reviews Report

Submitted by Ashish Kumar (18BLC1119)

Download packets of source code on Coders Packet