Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- #import threading
- import random
- # Description -> type:(probability, value)
- APPLE_TYPES = {"GREEN":(0.40, 10), "RED":(0.40, 10), "GOLD":(0.15, 50), "FADED":(0.05, 100)}
- CHEST_TYPES = {"BROKEN":(0.05, 10), "NORMAL":(0.70, 1500), "RARE":(0.20, 5000), "LEGENDARY":(0.05, 50000)}
- # upgrade costs
- BASE_COST = {"basket":250, "machine":1500, "x_machine":15000}
- DELTA_COST = {"basket": 250, "machine":2500, "x_machine":25000}
- CHEST_COST = 1500
- def weighted_choice(iter_obj,
- values = lambda iter_obj: iter_obj.keys(),
- weight = lambda iter_obj, val: iter_obj[val][0]):
- total = sum(weight(iter_obj, val) for val in values(iter_obj))
- r = random.uniform(0, total)
- upto = 0
- for val in values(iter_obj):
- if upto + weight(iter_obj, val) >= r:
- return val
- upto += weight(iter_obj, val)
- class Apple(object):
- def __init__(self):
- self.color = weighted_choice(APPLE_TYPES)
- self.value = APPLE_TYPES[self.color][1]
- class Chest(object):
- def __init__(self):
- self.type = weighted_choice(CHEST_TYPES)
- self.value = int(max(0.1, random.random()) * CHEST_TYPES[self.type][1])
- class ApplePicker(object):
- def __init__(self):
- self.apples = []
- self.upgrades = {"basket":0, "machine":0, "x_machine":0}
- self.coins = 0
- self.chests = []
- def open_chest(self, x = 1):
- if len(self.chests) < x:
- print "Error, you don't own any chests."
- else:
- for i in xrange(x):
- # Get last chest
- chest = self.chests.pop()
- self.coins += chest.value
- print "Your %s chest contained: %d coins" % (chest.type, chest.value)
- def buy_chest(self):
- if self.coins < CHEST_COST:
- print "You don't have enough coins"
- else:
- self.coins -= CHEST_COST
- chest = Chest()
- self.chests.append(chest)
- print "You bought a %s chest!" % chest.type
- print "press c to open it!"
- def check_balance(self):
- print "You have %d apples." % len(self.apples)
- print "You have %d coins." % self.coins
- print "You have %d chests." % len(self.chests)
- def robber_check(self):
- delta = time.time() - self.robber_timer
- # For every second passed 30 second the risk of getting robbed increases
- if random.random()*delta > 30:
- amount = int(self.coins * max(0.3, random.random()))
- self.coins -= amount
- self.robber_timer = time.time()
- print ("\nA robber just stole %d coins from you!\n" % amount).upper()
- # Sometimes by random reset timer
- elif random.random() > 0.95:
- #print "timer war reset"
- self.robber_timer = time.time()
- def main(self):
- self.machine_timer = time.time()
- self.x_machine_timer = time.time()
- self.robber_timer = time.time()
- while True:
- user_in = raw_input()
- self.robber_check()
- if user_in == "c":
- self.open_chest()
- elif user_in == "u":
- self.upgrade()
- elif user_in == "p":
- self.pick()
- elif user_in == "m":
- self.machine_pick()
- elif user_in.startswith("s"):
- val = user_in.split()
- if len(val) > 1:
- self.sell_apples(x = val[-1])
- else:
- self.sell_apples()
- elif user_in == "b":
- self.check_balance()
- elif user_in == "q":
- break
- elif user_in == "help":
- apple_picker.print_welcome()
- elif user_in == "prices":
- apple_picker.print_prices()
- elif user_in == "g":
- self.buy_chest()
- def machine_pick(self):
- delta_time = int(round(time.time() - self.machine_timer))
- x_delta_time = int(round(time.time() - self.x_machine_timer))
- new_apples = delta_time * self.upgrades["machine"]
- new_x_apples = x_delta_time * self.upgrades["x_machine"]**2
- self.machine_timer += delta_time
- self.x_machine_timer += x_delta_time
- print "Machine picked %d apples" % new_apples
- if new_x_apples:
- print "X_machine picked %d apples" % new_x_apples
- for _ in xrange(new_apples):
- self.apples.append(Apple())
- for _ in xrange(new_x_apples):
- self.apples.append(Apple())
- def pick(self):
- for i in xrange(self.upgrades["basket"]+1):
- self.apples.append(Apple())
- print "You picked a %s apple!" % self.apples[-1].color
- def sell_apples(self, x = "1"):
- if x.isdigit():
- # Convert to integear is possible
- x = int(x)
- elif isinstance(x, str):
- # If x is a string check if equal to "all"
- if x == "all":
- x = len(self.apples)
- # If string not valid, remove one apple anyway
- else:
- x = 1
- # If more than 5 apples are sold, don't print for every apple
- printout = True
- if x > 5:
- printout = False
- if len(self.apples) < x:
- print "You don't have %d apples to sell" % x
- else:
- pre_sell_coins = self.coins
- for i in xrange(x):
- apple = self.apples[0]
- self.coins += apple.value
- self.apples.remove(apple)
- if printout:
- print "You sold a %s apple and got %d coins." % (apple.color, apple.value)
- print "You sold %d apples worth %d coins" % (x, self.coins-pre_sell_coins)
- def upgrade(self):
- machine_cost = BASE_COST["machine"] + DELTA_COST["machine"] * self.upgrades["machine"]
- basket_cost = BASE_COST["basket"] + DELTA_COST["basket"] * self.upgrades["basket"]
- x_machine_cost = BASE_COST["x_machine"] + DELTA_COST["x_machine"] * self.upgrades["x_machine"]
- print ("Current upgrades are:\n"
- "Basket: %d\n"
- "Machine: %d\n"
- "X_machine: %d\n" % (self.upgrades["basket"],
- self.upgrades["machine"],
- self.upgrades["x_machine"]))
- print ("Would you like to upgrade:\n"
- "[b] Basket: %d Coins\n"
- "[m] Machine: %d Coins\n"
- "[x] X_machine: %d Coins." % (basket_cost,
- machine_cost,
- x_machine_cost))
- while True:
- user_in = raw_input()
- if user_in == "m":
- if self.coins >= machine_cost:
- if self.upgrades["machine"] >= 5:
- print "You can't upgrade your machine anymore."
- break
- self.upgrades["machine"] += 1
- self.coins -= machine_cost
- # reset machine timer when upgraded
- self.machine_timer = time.time()
- print "You upgraded your machine [-%d coins]" % machine_cost
- else:
- print "You dont have enough coins [%d needed]" % machine_cost
- break
- elif user_in == "b":
- if self.upgrades["basket"] >= 5:
- print "You can't upgrade your basket anymore."
- break
- if self.coins >= basket_cost:
- self.upgrades["basket"] += 1
- self.coins-=basket_cost
- print "You upgraded your basket! [-%d Coins]" % basket_cost
- else:
- print "You dont have enough coins [%d needed]" % basket_cost
- break
- elif user_in == "x":
- if self.upgrades["machine"] < 5:
- print "You must upgrade your machine to level 5 before you can upgrade x_machine"
- elif self.coins < x_machine_cost:
- print "You dont have enough coins [%d needed]" % x_machine_cost
- else:
- self.upgrades["x_machine"] += 1
- self.coins -= x_machine_cost
- self.x_machine_timer = time.time()
- print "You upgraded your x_machine [-%d coins]" % x_machine_cost
- break
- else:
- print "Invalid input, try again!"
- def print_welcome(self):
- print "==================================================================================="
- print "The apolcolypse is near,\nWe got to do whatever we can to prepare for it!"
- print ""
- print "Hey YOU!\nGo to those trees and pick some apples you later can sell for other special stuff!"
- print "===================================================================================\n"
- print ("Controlls:\n[c] Opens your chests.\n"
- "[g] Buy a chest [%d coins]\n"
- "[p] Picks an apple for you.\n"
- "[b] Is your balance.\n"
- "[s] [amount] sells the amount apples that you wish!\n"
- "[u] to upgrade your tools\n"
- "[m] Collect apples from machines.\n"
- "And more controlls are comming!\n" % CHEST_COST)
- print ("Commands:\n"
- "[help] Brings this text back if you're lost in the forest.\n"
- "[prices] Will google for you how much an apple is worth.\n")
- print "Just remember, these fade apples are the most valuable! but at the same time they are the most rare."
- def print_prices(self):
- print "==================================================================================="
- print "Oh hello, this is the website of [BuyingApples.org]\nHere you can see what we are offering for YOUR apples."
- print "==================================================================================="
- print ""
- print "[Faded Apples] = 100 Coins\n[Golden Apples] = 50 Coins\n[Red Apples] = 10 Coins\n[Green Apples] = 10 Coins"
- if __name__ == "__main__":
- apple_picker = ApplePicker()
- apple_picker.print_welcome()
- apple_picker.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement