DEV Community

Medea
Medea

Posted on • Edited on

Text Battle Royale

Text Battle Royale


Introduction

In this post I'll show you the code for a text Battle Royale game in Python, which is completely based on luck.
You only need 1 file for this to work!


Code

import os
from time import sleep
from random import randint, choice
def clear():
    try:
        if os.name == 'posix':
            _ = os.system('clear')
        elif os.name == 'nt':
            _ = os.system('cls')
        return True
    except Exception as e:
        print(e)
        return False
print("WELCOME TO TEXT BATTLE ROYALE!!!")
start = input("Press enter to start.")
while start != "":
  start = input("Press enter to start.")
clear()
for i in range(5):
  print("Scanning island." + "." * i)
  sleep(1)
  clear()
areas_on_map = ["Cool Place", "Great Place", "Scary Place"]
landing_area = choice(areas_on_map)
input("Press enter to exit the battle bus.")
clear()
print("You are skydiving.")
input("Press enter to deploy your parachute.")
for i in range(5, 0, -1):
  clear()
  print(f"You have deployed your parachute. You are going to land in {i} seconds.")
  sleep(1)
clear()
print("You have landed on",landing_area)
sleep(3)
clear()
elimination = 0
players = 100
print("You have 1 sniper rifle with infinity ammo.")
while players>1:
  print("There are",players,"including you in the game.")
  risk = randint(1,50)
  if risk > 0 and risk < 16:
    print(f"Someone has shot you from behind. You were {players}th place.")
    exit()
  elif risk > 15 and risk < 24:
    print(f"Someone has shot you from the side. You were {players}th place.")
    exit()
  elif risk > 23 and risk < 51:
    shot = input("There is someone coming close to you. If you want to kill them, press enter.")
    if shot == "":
      print("You have killed someone.")
      players = players - 1
      elimination = elimination + 1
    else:
       print(f"Someone has shot you from behind. You were {players}th place.")
       exit()
  else:
    players = players - 1
    clear()
clear()
print("You have won!")
Enter fullscreen mode Exit fullscreen mode

Conclusion

After running the code, you will be able to play a text version of a Battle Royale game, fully based on luck!
Thanks to @dillonb07 for the clear function!
Thanks for reading! If you liked this, make sure you follow me on dev.to and GitHub

Top comments (2)

Collapse
 
dillonb07 profile image
Dillon Barnes

Cool. One thing I would like to point out is that os.system('clear') doesn't work on all devices. To solve this, you can import the clear function from my package like so:

from dill_tils.text import clear

clear() # This clears the screen!
Enter fullscreen mode Exit fullscreen mode

Or, just steal the function from my GitHub repo if you don't want to install packages.
github.com/DillonB07/Dill_tils/blo...

This just changes the command to clear or cls depending on the OS that's being used to run the program.

Collapse
 
vulcanwm profile image
Medea

Oh yeah thanks for that! I’ll implement it later!