Ledger Nano X - The secure hardware wallet
SHARE
TWEET

game.py

a guest Mar 31st, 2020 115 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2.  
  3. os.system("cls")
  4.  
  5.  
  6. class Gamestate:
  7.     def __init__(self):
  8.         self.rooms = {}
  9.         self.import_rooms()
  10.         self.prototype = {}
  11.         self.import_prototypes()
  12.         self.commands = {}
  13.         self.make_commands()
  14.         self.items = {}
  15.         self.make_items()
  16.         # so im cheating the game by giving the player 99999 dollars... but its for me making the game...
  17.         self.player = Player(name="blank", health=100, damage=15, location=1, work_experiance=0, money=99999, items={})
  18.         self.get_player_room()
  19.         self.configure_rooms()
  20.         self.import_items()
  21.         self.entities = {}
  22.         self.make_entities()
  23.         self.import_entities()
  24.  
  25.     def configure_room_items(self):
  26.         # print(self.prototype["Ammo"])
  27.         for room in self.rooms.keys():
  28.             if self.rooms[room].general_location == "Gun Store":
  29.                 for item in self.rooms[room].items:
  30.                     if item.name == "Ammo":
  31.                         item.ammo = self.prototype["Ammo"].ammo
  32.  
  33.     # print(self.prototype["Ammo"].ammo)
  34.     # print(item)
  35.  
  36.     def import_prototypes(self):
  37.         ammo_prototype = Prototype(name="Ammo", ammo=40)
  38.         self.prototype[ammo_prototype.name] = ammo_prototype
  39.  
  40.     def load_weapon(self, user_input):
  41.         x = 0
  42.         for item in self.player.items:
  43.             item_name = self.items[item].name.lower().split()
  44.             print(item_name)
  45.  
  46.     # if a user wants to attack an entity
  47.     def attack(self, user_input):
  48.         # check if players location has an entity
  49.         if self.player.location.entity != None:
  50.             # if entity is in location, here i check if the user entered the entites name
  51.             entity_split = self.player.location.entity.name.lower().split()
  52.             x = 0
  53.             for word in user_input:
  54.                 for name in entity_split:
  55.                     if word == name:
  56.                         x += 1
  57.             # this if statement confirms if the user entered the entities name
  58.             if x == len(entity_split):
  59.                 # where the attack gets setup, printing the entity stats player stats etc
  60.                 # because the player entered attack entity it removes health from player and entity before the leave == False loop starts
  61.                 self.player.location.entity.health -= self.player.damage
  62.                 self.player.health -= self.player.location.entity.damage
  63.                 while True:
  64.                     while self.player.health >= 0:
  65.                         if self.player.location.entity.health <= 0:
  66.                             print("\nCongrats :) you have killed %s" % (self.player.location.entity.name))
  67.                             # self.player.location.entity = None
  68.                             break
  69.                         print("\nYour attacking %s!" % (self.player.location.entity.name))
  70.                         print("\n[Attack]")
  71.                         print("\n[Entity Stats]")
  72.                         print("Entity Name: %s" % (self.player.location.entity.name))
  73.                         print("Entity Health: %d" % (self.player.location.entity.health))
  74.                         print("Entity Damage: %d" % (self.player.location.entity.damage))
  75.                         print("\n[Player Stats]")
  76.                         print("Health: %d" % (self.player.health))
  77.                         print("Damage: %d" % (self.player.damage))
  78.                         # check first if the entities health is >= 0
  79.                         attack_input = input("\nAttack Command: ").lower().split()
  80.                         if "attack" in attack_input:
  81.                             # if the user attacked, it prints you attacked entity, and then removing health from both player and entity
  82.                             print("\nYou have attacked %s" % (self.player.location.entity.name))
  83.                             self.player.location.entity.health -= self.player.damage
  84.                             self.player.health -= self.player.location.entity.damage
  85.                         if "leave" in attack_input:
  86.                             print("\nYou have left the fight")
  87.                             break
  88.                     if self.player.health <= 0:
  89.                         break
  90.                     else:
  91.                         break
  92.  
  93.         # if no entity in location, print error
  94.         else:
  95.             print("\nThere are no entities in this room you can attack")
  96.  
  97.     def import_entities(self):
  98.         # importing entites into locations
  99.         for room in self.rooms.keys():
  100.             if self.rooms[room].rname == "Pizza Place Kitchen":
  101.                 pizza_cook = self.entities["Pizza Cook"]
  102.                 self.rooms[room].entity = pizza_cook
  103.             if self.rooms[room].rname == "Pizza Place Lobby":
  104.                 pizza_manager = self.entities["Pizza Manager"]
  105.                 self.rooms[room].entity = pizza_manager
  106.             if self.rooms[room].rname == "Gun Store Floor":
  107.                 gun_store_manager = self.entities["Gun Store Manager"]
  108.                 self.rooms[room].entity = gun_store_manager
  109.             if self.rooms[room].rname == "Grocery Store Produce Section":
  110.                 grocery_store_manager = self.entities["Grocery Store Manager"]
  111.                 self.rooms[room].entity = grocery_store_manager
  112.  
  113.     def make_entities(self):
  114.         self.entities = {
  115.             "Pizza Cook": Entity(name="Pizza Cook", health=100, damage=5, type="citizen", money=30, items=[]),
  116.             "Pizza Manager": Entity(name="Pizza Manager", health=100, damage=10, type="citizen", money=100, items=[]),
  117.             "Gun Store Manager": Entity(name="Gun Store Manager", health=100, damage=10, type="citizen", money=200,
  118.                                         items=[]),
  119.             "Grocery Store Manager": Entity(name="Grocery Store Manager", health=100, damage=12, type="citizen",
  120.                                             money=30, items=[])}
  121.  
  122.     def eat_food(self, user_input):
  123.         for item in self.player.items:
  124.             item_split = item.name.lower().split()
  125.             x = 0
  126.             for word in user_input:
  127.                 for name in item_split:
  128.                     if word == name:
  129.                         x += 1
  130.                 if x == len(item_split):
  131.                     if item.type == "Food":
  132.                         self.player.health += item.health
  133.                         print("\nYou ate %s your health has increased by %d" % (item.name, item.health))
  134.                         self.player.items.remove(item)
  135.  
  136.     def print_items(self, user_input):
  137.         if self.player.location.items != None:
  138.             print("\n[%s Items]" % (self.player.location.general_location))
  139.             print("-" * 50)
  140.             for item in self.player.location.items:
  141.                 # item = self.player.location.items[item]
  142.                 if item.type == "Gun":
  143.                     print("Name: %s\tDamage: %d\tPrice: %d" % (item.name, item.damage, item.price))
  144.                 if item.type == "Food":
  145.                     print("Name: %s\tHealth: %d\tPrice: %d" % (item.name, item.health, item.price))
  146.                 if item.type == "Ammo":
  147.                     print("Name: %s\tAmount: %d\tPrice: %d" % (item.name, item.ammo, item.price))
  148.             print("-" * 50)
  149.  
  150.     def import_items(self):
  151.         for room in self.rooms.keys():
  152.             if self.rooms[room].general_location == "Gun Store":
  153.                 self.rooms[room].items = {}
  154.                 ak47 = self.items["ak47"]
  155.                 ar15 = self.items["ar15"]
  156.                 ammo = self.items["ammo"]
  157.  
  158.                 # self.rooms[room].items.append(ak47)
  159.                 # self.rooms[room].items.append(ar15)
  160.                 # self.rooms[room].items.append(ammo)
  161.  
  162.                 # print(self.rooms[room].items)
  163.  
  164.                 self.rooms[room].items = {ak47: ak47, ar15: ar15, ammo: ammo}
  165.             # room_items = self.rooms.items
  166.             # self.rooms.items = []
  167.             # room_items = self.rooms.items
  168.             # room_items.append(ak47)
  169.             # room_items.append(ar15)
  170.             # room_items.append(ammo)
  171.  
  172.             if self.rooms[room].general_location == "Grocery Store":
  173.                 carrots = self.items["carrots"]
  174.                 fish = self.items["fish"]
  175.                 salad = self.items["salad"]
  176.                 self.rooms[room].items = {carrots: carrots, fish: fish, salad: salad}
  177.  
  178.             if self.rooms[room].general_location == "Pizza Place":
  179.                 cheese_pizza = self.items["cheese pizza"]
  180.                 hawaiian_pizza = self.items["hawaiian pizza"]
  181.                 self.rooms[room].items = {cheese_pizza: cheese_pizza, hawaiian_pizza: hawaiian_pizza}
  182.  
  183.     def make_items(self):
  184.         self.items = {"ak47": Item(name="AK-47", price=700, damage=55, type="Gun", ammo=0),
  185.                       "ar15": Item(name="AR-15", price=500, damage=45, type="Gun", ammo=0),
  186.                       "ammo": Item(name="Ammo", price=20, type="Ammo", ammo=40),
  187.                       "carrots": Item(name="Carrots", price=2, health=3, type="Food"),
  188.                       "fish": Item(name="Fish", price=10, health=15, type="Food"),
  189.                       "salad": Item(name="Salad", price=5, health=10, type="Food"),
  190.                       "cheese pizza": Item(name="Cheese Pizza", price=12, health=15, type="Food"),
  191.                       "hawaiian pizza": Item(name="Hawaiian Pizza", price=15, health=15, type="Food")}
  192.  
  193.     def work(self, user_input):
  194.         if self.player.location.general_location == "Pizza Place":
  195.             if self.player.work_experiance >= 0:
  196.                 work_shift = 60
  197.                 print("\n[Work]")
  198.                 print("you just made $%d today" % work_shift)
  199.                 self.player.money += work_shift
  200.                 work_experiance = 5
  201.                 print("you gained %d work experiance" % work_experiance)
  202.                 self.player.work_experiance += work_experiance
  203.         if self.player.location.general_location == "Gun Store":
  204.             if self.player.work_experiance >= 40:
  205.                 work_shift = 100
  206.                 print("\n[Work]")
  207.                 print("you just made $%d today" % work_shift)
  208.                 self.player.money += work_shift
  209.                 work_experiance = 20
  210.                 print("you gained %d work experiance" % work_experiance)
  211.                 self.player.work_experiance += work_experiance
  212.             else:
  213.                 print("\nsorry you need atleast 40 work experiance to work here")
  214.         if self.player.location.general_location == "Hospital":
  215.             if self.player.work_experiance >= 500:
  216.                 work_shift = 250
  217.                 print("\n[Work]")
  218.                 print("you just made $%d today" % work_shift)
  219.                 self.player.money += work_shift
  220.                 work_experiance = 50
  221.                 print("you gained %d work experiance" % work_experiance)
  222.                 self.player.work_experiance += work_experiance
  223.         if self.player.location.general_location == "Bank":
  224.             if self.player.work_experiance >= 120:
  225.                 work_shift = 150
  226.                 print("\n[Work]")
  227.                 print("you just made $%d today" % work_shift)
  228.                 self.player.money += work_shift
  229.                 work_experiance = 30
  230.                 print("you gained %d work experiance" % work_experiance)
  231.                 self.player.work_experiance += work_experiance
  232.         if self.player.location.general_location == "Grocery Store":
  233.             if self.player.work_experiance >= 15:
  234.                 work_shift = 80
  235.                 print("\n[Work]")
  236.                 print("you just made $%d today" % work_shift)
  237.                 self.player.money += work_shift
  238.                 work_experiance = 20
  239.                 print("you gained %d work experiance" % work_experiance)
  240.                 self.player.work_experiance += work_experiance
  241.  
  242.     def print_player_stats(self):
  243.         print("\n[Player Stats]")
  244.         print("Name: %s\nHealth: %d\nDamage: %d\nWork experiance: %d\nMoney: %d" % (
  245.             self.player.name, self.player.health, self.player.damage, self.player.work_experiance, self.player.money))
  246.  
  247.         if len(self.player.items) != 0:
  248.             print("\n[Player Items]")
  249.             print("-" * 50)
  250.             for item in self.player.items.keys():
  251.                 if self.player.items[item].type == "Gun":
  252.                     print("Name: %s\tDamage: %d" % (self.player.items[item].name, self.player.items[item].damage))
  253.                 if self.player.items[item].type == "Food":
  254.                     print("Name: %s\tHealth: %d" % (self.player.items[item].name, self.player.items[item].health))
  255.                 if self.player.items[item].type == "Ammo":
  256.                     print("Name: %s\tAmount: %d" % (self.player.items[item].name, self.player.items[item].ammo))
  257.             print("-" * 50)
  258.  
  259.     def print_stats(self):
  260.         print("\nin the print stats method")
  261.  
  262.     def buy_item(self, user_input):
  263.         item = None
  264.         if self.player.location.items != None:
  265.             for location_item in self.player.location.items:
  266.                 location_item_name = location_item.name.lower().split()
  267.                 for word in user_input:
  268.                     for name in location_item_name:
  269.                         if word == name:
  270.                             item = location_item
  271.         if self.player.money >= item.price:
  272.             if item in self.player.items:
  273.                 print("\nyou have this item already")
  274.                 for player_item in self.player.items:
  275.                     if player_item.name == item.name:
  276.                         if item.name == "Ammo":
  277.                             player_item.ammo += item.ammo
  278.             self.player.items[item.name] = item
  279.  
  280.     def make_commands(self):
  281.         self.commands = {
  282.             "go": Commands(name="go", function=self.going_places, type="movement", description="To go to places"),
  283.             "north": Commands(name="go", function=self.going_places, type="movement",
  284.                               description="To move to a north room"),
  285.             "stats": Commands(name="stats", function=self.print_stats, type="verbosity",
  286.                               description="the print the stats of something"),
  287.             "player": Commands(name="player", function=self.print_player_stats, type="verbosity",
  288.                                description="to print the players stats"),
  289.             "work": Commands(name="work", function=self.work, type="work", description="to try and work a job"),
  290.             "items": Commands(name="items", function=self.print_items, type="verbosity",
  291.                               description="To view the items of someone or someplace"),
  292.             "buy": Commands(name="buy", function=self.buy_item, type="purchase", description="To buy an item")}
  293.  
  294.     def configure_rooms(self):
  295.         # configure north south east and west
  296.         for room in self.rooms.keys():
  297.             exits = []
  298.             nsew = self.rooms[room].nsew
  299.             for number in nsew:
  300.                 exits.append(int(number))
  301.             self.rooms[room].nsew = exits
  302.  
  303.         for room in self.rooms.keys():
  304.             north = self.rooms[room].nsew[0]
  305.             south = self.rooms[room].nsew[1]
  306.             east = self.rooms[room].nsew[2]
  307.             west = self.rooms[room].nsew[3]
  308.             self.rooms[room].north = north
  309.             self.rooms[room].south = south
  310.             self.rooms[room].east = east
  311.             self.rooms[room].west = west
  312.  
  313.         # working on exits now
  314.         for room in self.rooms.keys():
  315.             exits = []
  316.             for exit in self.rooms[room].exits:
  317.                 exits.append(int(exit))
  318.             self.rooms[room].exits = exits
  319.  
  320.     def going_places(self, user_input):
  321.         if "north" in user_input:
  322.             if self.player.location.north != 0:
  323.                 for room in self.rooms.keys():
  324.                     if self.player.location.north == self.rooms[room].rnum:
  325.                         self.player.location = self.rooms[room]
  326.         if "south" in user_input:
  327.             if self.player.location.south != 0:
  328.                 for room in self.rooms.keys():
  329.                     if self.player.location.south == self.rooms[room].rnum:
  330.                         self.player.location = self.rooms[room]
  331.         if "east" in user_input:
  332.             if self.player.location.east != 0:
  333.                 for room in self.rooms.keys():
  334.                     if self.player.location.east == self.rooms[room].rnum:
  335.                         self.player.location = self.rooms[room]
  336.         if "west" in user_input:
  337.             if self.player.location.west != 0:
  338.                 for room in self.rooms.keys():
  339.                     if self.player.location.west == self.rooms[room].rnum:
  340.                         self.player.location = self.rooms[room]
  341.         # lets see if the user entered a room name instead of north south east or west
  342.         if self.player.location.north != 0:
  343.             for room in self.rooms.keys():
  344.                 if self.player.location.north == self.rooms[room].rnum:
  345.                     room_name = self.rooms[room].rname.lower().split()
  346.                     x = 0
  347.                     for word in user_input:
  348.                         for name in room_name:
  349.                             if word == name:
  350.                                 x += 1
  351.                     if x == len(room_name):
  352.                         self.player.location = self.rooms[room]
  353.         if self.player.location.south != 0:
  354.             for room in self.rooms.keys():
  355.                 if self.player.location.south == self.rooms[room].rnum:
  356.                     room_name = self.rooms[room].rname.lower().split()
  357.                     x = 0
  358.                     for word in user_input:
  359.                         for name in room_name:
  360.                             if word == name:
  361.                                 x += 1
  362.                     if x == len(room_name):
  363.                         self.player.location = self.rooms[room]
  364.         if self.player.location.east != 0:
  365.             for room in self.rooms.keys():
  366.                 if self.player.location.east == self.rooms[room].rnum:
  367.                     room_name = self.rooms[room].rname.lower().split()
  368.                     x = 0
  369.                     for word in user_input:
  370.                         for name in room_name:
  371.                             if word == name:
  372.                                 x += 1
  373.                     if x == len(room_name):
  374.                         self.player.location = self.rooms[room]
  375.         if self.player.location.west != 0:
  376.             for room in self.rooms.keys():
  377.                 if self.player.location.west == self.rooms[room].rnum:
  378.                     room_name = self.rooms[room].rname.lower().split()
  379.                     x = 0
  380.                     for word in user_input:
  381.                         for name in room_name:
  382.                             if word == name:
  383.                                 x += 1
  384.                     if x == len(room_name):
  385.                         self.player.location = self.rooms[room]
  386.         # lets assume the user wanted to enter an exit location
  387.         if self.player.location.exits[0] != 0:
  388.             for exit in self.player.location.exits:
  389.                 for room in self.rooms.keys():
  390.                     if exit == self.rooms[room].rnum:
  391.                         exit_name = self.rooms[room].rname.lower().split()
  392.                         x = 0
  393.                         for word in user_input:
  394.                             for name in exit_name:
  395.                                 if word == name:
  396.                                     x += 1
  397.                         if x == len(exit_name):
  398.                             self.player.location = self.rooms[room]
  399.                         general_name = self.rooms[room].general_location.lower().split()
  400.                         z = 0
  401.                         for word in user_input:
  402.                             for name in general_name:
  403.                                 if word == name:
  404.                                     z += 1
  405.                         if z == len(general_name):
  406.                             self.player.location = self.rooms[room]
  407.  
  408.     def get_player_room(self):
  409.         for room in self.rooms.keys():
  410.             if self.player.location == self.rooms[room].rnum:
  411.                 self.player.location = self.rooms[room]
  412.  
  413.     def take_input(self):
  414.         user_input = input("\nCommand: ").lower().split()
  415.         self.going_places(user_input)
  416.  
  417.         if "player" in user_input:
  418.             self.print_player_stats()
  419.         if "work" in user_input:
  420.             self.work(user_input)
  421.         if "items" in user_input:
  422.             self.print_items(user_input)
  423.         if "buy" in user_input:
  424.             self.buy_item(user_input)
  425.         if "eat" in user_input:
  426.             self.eat_food(user_input)
  427.         if "attack" in user_input:
  428.             self.attack(user_input)
  429.         if "load" in user_input:
  430.             self.load_weapon(user_input)
  431.  
  432.     def do_look(self):
  433.         # printing out the current room
  434.         print("\n[Location]")
  435.         print("Room name: %s" % (self.player.location.rname))
  436.         print("Room description: %s" % (self.player.location.rdesc))
  437.         if self.player.location.entity != None:
  438.             print("\n[Location Entities]")
  439.             print(self.player.location.entity.name)
  440.  
  441.         # print the exits of leaving general location
  442.         if self.player.location.exits[0] != 0:
  443.             print("\n[Exits]")
  444.             for room in self.rooms.keys():
  445.                 for exit in self.player.location.exits:
  446.                     if exit == self.rooms[room].rnum:
  447.                         print("%s: %s" % (self.rooms[room].general_location, self.rooms[room].rname))
  448.  
  449.         # print the room exits
  450.         print("\n[Room Exits]")
  451.         if self.player.location.north != 0:
  452.             for room in self.rooms.keys():
  453.                 if self.player.location.north == self.rooms[room].rnum:
  454.                     print("North: %s" % (self.rooms[room].rname))
  455.         if self.player.location.south != 0:
  456.             for room in self.rooms.keys():
  457.                 if self.player.location.south == self.rooms[room].rnum:
  458.                     print("South: %s" % (self.rooms[room].rname))
  459.         if self.player.location.east != 0:
  460.             for room in self.rooms.keys():
  461.                 if self.player.location.east == self.rooms[room].rnum:
  462.                     print("East: %s" % (self.rooms[room].rname))
  463.         if self.player.location.west != 0:
  464.             for room in self.rooms.keys():
  465.                 if self.player.location.west == self.rooms[room].rnum:
  466.                     print("West: %s" % (self.rooms[room].rname))
  467.  
  468.     def import_rooms(self):
  469.         f = open("rooms.txt", "r")
  470.         lines = f.readlines()
  471.  
  472.         loop = 0
  473.  
  474.         while loop <= len(lines) - 1:
  475.             self.rooms[lines[loop + 1]] = Room(rnum=int(lines[loop]), rname=lines[loop + 1].strip("\n"),
  476.                                                rdesc=lines[loop + 2].strip("\n"),
  477.                                                general_location=lines[loop + 3].strip("\n"),
  478.                                                nsew=lines[loop + 4].split(), exits=lines[loop + 5].split())
  479.             loop += 6
  480.  
  481.  
  482. class Item:
  483.     def __init__(self, name, health=None, price=None, damage=None, type=None, ammo=None):
  484.         self.name = name
  485.         self.health = health
  486.         self.price = price
  487.         self.damage = damage
  488.         self.type = type
  489.         self.ammo = ammo
  490.  
  491.  
  492. class Commands:
  493.     def __init__(self, name, function, type, description):
  494.         self.name = name
  495.         self.function = function
  496.         self.type = type
  497.         self.description = description
  498.  
  499.  
  500. class Player:
  501.     def __init__(self, name, health, damage, location, work_experiance, money, items):
  502.         self.name = name
  503.         self.health = health
  504.         self.damage = damage
  505.         self.location = location
  506.         self.work_experiance = work_experiance
  507.         self.money = money
  508.         self.items = items
  509.  
  510.  
  511. class Room:
  512.     def __init__(self, rnum=None, rname=None, rdesc=None, general_location=None, nsew=None, north=None, south=None,
  513.                  east=None, west=None, exits=None, items=None, type=None, entity=None):
  514.         self.rnum = rnum
  515.         self.rname = rname
  516.         self.rdesc = rdesc
  517.         self.general_location = general_location
  518.         self.nsew = nsew
  519.         self.north = north
  520.         self.south = south
  521.         self.east = east
  522.         self.west = west
  523.         self.exits = exits
  524.         self.items = items
  525.         self.type = type
  526.         self.entity = entity
  527.  
  528.  
  529. class Entity:
  530.     def __init__(self, name, health, damage, type, money, items):
  531.         self.name = name
  532.         self.health = health
  533.         self.damage = damage
  534.         self.type = type
  535.         self.money = money
  536.         self.items = items
  537.  
  538.  
  539. class Prototype:
  540.     def __init__(self, name, ammo=None, price=None):
  541.         self.name = name
  542.         self.ammo = ammo
  543.         self.price = price
  544.  
  545.  
  546. gamestate = Gamestate()
  547.  
  548.  
  549. def game_over():
  550.     print("\nGame Over! You have died :(")
  551.     os.system("exit")
  552.  
  553.  
  554. def main():
  555.     gamestate.player.name = input("\nWhats your name?\nName: ")
  556.     print("\nThank you for playing my game %s" % (gamestate.player.name))
  557.     while True:
  558.         if gamestate.player.health <= 0:
  559.             game_over()
  560.             break
  561.         gamestate.do_look()
  562.         # gamestate.initiate_values()
  563.         gamestate.take_input()
  564.         # what configure room items are, is so every item remains the same value or atleast the items i want to stay the same value
  565.         gamestate.configure_room_items()
  566.  
  567.  
  568. main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top