def buy_upgrade(self, choice): print(choice) upgrade = PLAYER.available_upgrades[choice] if PLAYER.balance >= upgrade.current_price: PLAYER.balance -= upgrade.current_price PLAYER.owned_upgrades.append(upgrade) PLAYER.available_upgrades.remove(upgrade) for item in PLAYER.inventory: if item.name == upgrade.target: building = item else: print( "Upgrade creation error, target/name match not found") if upgrade.effect == 2: building.upgrade_mult *= 2 else: # Use logic for cursor upgrades here :) # Need to use building_ct in some way pass self.bal_show.config(text="Balance: " + display_num(round(PLAYER.balance))) PLAYER.cps_update() self.cps_show.config(text="Clicks per Second (cps): " + display_num(round(PLAYER.cps, 1))) self.create_upgrade_shop()
def __init__(self, master): PLAYER.stats = { 'balance': display_num(round(PLAYER.balance)), 'earned': display_num(round(PLAYER.earned)), 'lifetime': display_num(round(PLAYER.life_earned)), 'cps': display_num(round(PLAYER.cps * 100)), 'init_time': PLAYER.start_time, 'building count': PLAYER.building_ct, 'click strength': PLAYER.click_str, 'handmade': display_num(PLAYER.handmade) } self.label = tk.Label( master, text="################# Stats #################") self.label.grid(columnspan=2) for r, key in enumerate(PLAYER.stats, 1): if key == 'inventory' or key == 'pause_time': continue if key == 'init_time': self.key = tk.Label(master, text="Time Passed") self.key.grid(row=r) self.info = tk.Label( master, text=time_delta_display(time() - PLAYER.stats[key])) self.info.grid(row=r, column=1) r += 1 continue self.key = tk.Label(master, text=key.capitalize()) self.key.grid(row=r) self.info = tk.Label(master, text=PLAYER.stats[key]) self.info.grid(row=r, column=1)
def create_building_tooltip(self, name, building, index): """ Creates tooltips :param name: :param building: :param index: :return: """ label = name.capitalize() # Gets the cps created by the building type build_cps = building.count * building.cps * building.upgrade_mult PLAYER.cps_update() # Gets the % of the cps contributed by the building type try: build_cps_ratio = str(round(build_cps / PLAYER.cps * 100, 1)) except ZeroDivisionError: build_cps_ratio = str(0.0) # Creates the tooltip CreateToolTip( self.button_lst[index], "--Each " + label + " produces " + display_num(building.cps * building.upgrade_mult) + " cookies per second\n" + "--" + display_num(building.count) + " " + label + "s producing " + display_num(build_cps) + " cookies per second (" + build_cps_ratio + "%)")
def import_save(self): """ Reads and reloads the game with data from the save file :return: """ print("Loading save...") # Extracts the stats dict from the saved JSON with open("CookieClone Save", "r", encoding="utf-8") as file: self.stats = json.load(file) # Extracts the data from the stats dict to populate the game self.balance = self.stats['balance'] self.earned = self.stats['earned'] self.life_earned = self.stats['lifetime'] self.start_time = self.stats['init_time'] self.pause_time = self.stats['pause_time'] self.click_str = self.stats['click_str'] self.handmade = self.stats['handmade'] self.j_inv = self.stats['inventory'] self.j_upg = self.stats['upgrades'] self.inventory = [] for entry in self.j_inv: self.inventory.append(Building(**entry)) self.owned_upgrades = [] for entry in self.j_upg: self.owned_upgrades.append(Upgrade(**entry)) del self.j_inv del self.j_upg # For every building... for i, building in enumerate(self.inventory): # Update the count lists GAME.count_list[i].config(text=display_num(building.count)) # Update the price lists GAME.price_list[i].config( text='$' + display_num(round(building.current_price))) # Create a tooltip rollover GAME.create_building_tooltip(building.name, building, i) # Recalculate the cps and update the balances self.cps_update() self.balance += self.cps * (time() - self.pause_time) self.earned += self.cps * (time() - self.pause_time) self.life_earned += self.cps * (time() - self.pause_time) # Updates the cps label GAME.cps_show.config(text="Clicks per Second (cps): " + display_num(round(self.cps, 1))) self.check_upg_cond() GAME.create_upgrade_shop() print("Finished!")
def update_shop(self): buy_ct = self.var.get() i = 0 for key, entry in PLAYER.inventory.items(): self.count_list[i].config(text=str(PLAYER.inventory[key][0])) self.price_list[i].config(text='$' + display_num(round(entry[2] * (1.15**(buy_ct-1))))) self.create_tooltip(key, entry, i) # Update their balance self.bal_show.config(text="Balance: " + display_num(round(PLAYER.balance))) # Recalculate and update the cps PLAYER.cps_update() self.cps_show.config(text="Clicks per Second (cps): " + display_num(PLAYER.cps))
def create_shop(self): """ Creates the entire shop: Prices, Buy buttons, & Quantity numbers based on PLAYER.inventory :return: """ self.price_list = [] self.button_lst = [] self.count_list = [] index = 1 r = 3 for key, entry in PLAYER.inventory.items(): label = key.lower().capitalize() # makes price labels (for the displayed price: is the price x [1.15^(building mult number-1)] # The '...-1' is because the correct price is stored in the PLAYER.inv entry price_name = tk.Label(self.frame_shop, width=21, text='$' + display_num(round(entry[2] * (1.15**(self.var.get()-1))))) price_name.grid(row=r) self.price_list.append(price_name) # makes purchase buttons but_name = tk.Button(self.frame_shop, text=label, width=20, command=lambda j=index: self.buy(j)) but_name.grid(row=r, column=1) self.button_lst.append(but_name) # makes the rollover tooltips self.create_tooltip(key, entry, index - 1) # makes count labels key = tk.Label(self.frame_shop, width=21, text=str(PLAYER.inventory[key][0])) key.grid(row=r, column=2) self.count_list.append(key) r += 1 index += 1
def ck_click(self): """ If the player clicks the cookie, add a cookie and update the balance :return: """ PLAYER.balance += 1 PLAYER.earned += 1 PLAYER.handmade += 1 self.bal_show.config(text="Balance: " + str(display_num(round(PLAYER.balance))))
def buy_building(self, choice): """ Runs the backend for purchasing buildings :param choice: :return: """ # Sets the variables for the function buy_ct = self.var.get() building = PLAYER.inventory[choice - 1] # If the player can afford the building at it's current price based on quantity purchased if PLAYER.balance >= round( self.current_price_calculator(building.base_price, building.count)): # For every copy of the building required... for i in range(0, buy_ct): # Take the cookies from the player PLAYER.balance -= building.current_price # Give the player one building building.count += 1 # Raise the price of the next building building.current_price = self.current_price_calculator( building.base_price, building.count, True) # Update their balance self.bal_show.config(text="Balance: " + display_num(round(PLAYER.balance))) # Update the building's count list self.count_list[choice - 1].config(text=display_num(building.count)) # Update the building's price list self.price_list[choice - 1].config(text='$' + display_num( round( self.current_price_calculator(building.base_price, building.count)))) # Recalculate and update the cps PLAYER.cps_update() self.cps_show.config(text="Clicks per Second (cps): " + display_num(round(PLAYER.cps, 1))) PLAYER.check_upg_cond() # Update all the tooltips (for cps%) for i, entry in enumerate(PLAYER.inventory): self.create_building_tooltip(entry.name, entry, i)
def buy(self, choice): """ Runs the backend for purchasing buildings :param choice: :return: """ index = 1 buy_ct = self.var.get() # For every building possible... for key, building in PLAYER.inventory.items(): # If the player bought one of these buildings... if choice == index: # And if the player can afford it at it's current price... if PLAYER.balance >= building[2] * buy_ct: for i in range(0, buy_ct): # Take the cookies from the player PLAYER.balance -= building[2] # Give the player one building building[0] += 1 # Raise the price of the next building building[2] *= 1.15 # TODO: Ensure the balance updates when a purchase is made for more than 1 building # Update their balance bal = display_num(round(PLAYER.balance)) self.bal_show.config(text="Balance: " + bal) # Update the building's count list ct = display_num(building[0]) self.count_list[choice - 1].config(text=ct) # Update the building's price list price = '$' + display_num(round(building[2] * (1.15**(buy_ct-1)))) self.price_list[choice - 1].config(text=price) # Recalculate and update the cps PLAYER.cps_update() self.cps_show.config(text="Clicks per Second (cps): " + display_num(PLAYER.cps)) # Update all the tooltips (for cps%) i = 0 for name, entry in PLAYER.inventory.items(): self.create_tooltip(name, entry, i) i += 1 break index += 1
def import_save(self): """ Reads and reloads the game with data from the save file :return: """ print("Loading save...") # Extracts the stats dict from the saved JSON with open("CookieClone Save", "r", encoding="utf-8") as file: self.stats = json.load(file) # Extracts the data from the stats dict to populate the game self.balance = self.stats['balance'] self.earned = self.stats['earned'] self.life_earned = self.stats['lifetime'] self.start_time = self.stats['init_time'] self.pause_time = self.stats['pause_time'] self.click_str = self.stats['click_str'] self.handmade = self.stats['handmade'] self.inventory = self.stats['inventory'] i = 0 # For every building... for key, building in self.inventory.items(): # Update the count lists GAME.count_list[i].config(text=display_num(building[0])) # Update the price lists GAME.price_list[i].config(text='$' + display_num(round(building[2]))) # Create a tooltip rollover GAME.create_tooltip(key, building, i) i += 1 # Recalculate the cps and update the balances self.cps_update() self.balance += self.cps * (time() - self.pause_time) self.earned += self.cps * (time() - self.pause_time) self.life_earned += self.cps * (time() - self.pause_time) # Updates the cps label GAME.cps_show.config(text="Clicks per Second (cps): " + display_num(round(self.cps, 1))) print("Finished!")
def update_shop(self): """ Light weight function to update the prices and count of the buildings in the shop without totally recreating everything :return: """ for i in range(len(self.price_list)): building = PLAYER.inventory[i] self.price_list[i].configure(text='$' + display_num( round( self.current_price_calculator(building.base_price, building.count)))) self.count_list[i].configure(text=str(building.count))
def create_tooltip(self, key, entry, index): """ Creates tooltips :param key: :param entry: :param index: :return: """ label = key.capitalize() # Gets the cps created by the building type build_cps = entry[0] * entry[1] PLAYER.cps_update() # Gets the % of the cps contributed by the building type try: build_cps_ratio = str(round(build_cps / PLAYER.cps * 100, 1)) except ZeroDivisionError: build_cps_ratio = str(0.0) # Creates the tooltip CreateToolTip(self.button_lst[index], "--Each " + label + " produces " + display_num(entry[1]) + " cookies per second\n" + "--" + display_num(entry[0]) + " " + label + "s producing " + display_num(build_cps) + " cookies per second (" + build_cps_ratio + "%)")
def game_tick(self): """ Adds the amount of cookies the player should receive every second :return: """ # Ensure the cps is correct, uses the cps / 100 to make the bal update live, while using 1sec as the baseline PLAYER.cps_update(game_tick=1) # Add the cps to the player's balance PLAYER.balance += PLAYER.cps PLAYER.earned += PLAYER.cps # Update the balance self.bal_show.config(text="Balance: " + display_num(round(PLAYER.balance))) self.save_counter += 1 if self.save_counter % 30000 == 0: self.save_counter = 0 PLAYER.export_save() # Repeat after 10ms self.bal_show.after(10, self.game_tick)
def create_building_shop(self): """ Creates the entire shop: Prices, Buy buttons, & Quantity numbers based on PLAYER.inventory :return: """ self.price_list = [] self.button_lst = [] self.count_list = [] for i, building in enumerate(PLAYER.inventory, 1): label = building.name.lower().capitalize() r = i + 2 # makes price labels p = self.current_price_calculator(building.base_price, building.count) price_name = tk.Label(self.frame_shop, width=20, text='$' + display_num(round(p))) price_name.grid(row=r) self.price_list.append(price_name) # makes purchase buttons but_name = tk.Button(self.frame_shop, text=label, width=20, command=lambda j=i: self.buy_building(j)) but_name.grid(row=r, column=1) self.button_lst.append(but_name) # makes the rollover tooltips self.create_building_tooltip(building.name, building, i - 1) # makes count labels key = tk.Label(self.frame_shop, width=20, text=str(building.count)) key.grid(row=r, column=2) self.count_list.append(key)