def wear_item(): if action.has_dialog == 1: return 0 if curr_item >= inv_width * inv_height: rm_equip() return 0 try: item_value = item.inv[curr_item] except IndexError: return 0 if item_value == -1: return #put the equip slot into item_loc item_loc = item.item[item_value].type #if item is equipment if item_loc < 6: #trade the item and whatever's in the equip slot temp = player.equip[item_loc] player.equip[item_loc] = item_value item.drop_inv_item(curr_item) item.take_inv_item(temp) main.print_message("You equip yourself with your " + item.item[player.equip[item_loc]].name + ".") player.reset_stats() if cur_button == 0: refresh_use() elif cur_button == 1: refresh_equip() refresh_stat_display()
def rm_equip(): if action.has_dialog == 1: return 0 #Curr_item is the current location in the equipment canvas. #The missing numbers in this sequence are the blank spots in the display. sel_equipment = -1 c_item = curr_item - inv_width * inv_height if c_item == 1: sel_equipment = 3 elif c_item == 2: sel_equipment = 4 elif c_item == 3: sel_equipment = 0 elif c_item == 4: sel_equipment = 1 elif c_item == 5: sel_equipment = 2 elif c_item == 7: sel_equipment = 5 if sel_equipment == -1: return 0 if player.equip[sel_equipment] != -1: if -1 != item.take_inv_item(player.equip[sel_equipment]): main.print_message("You take off your " + item.item[player.equip[sel_equipment]].name) player.equip[sel_equipment] = -1 player.reset_stats() refresh_equip() refresh_stat_display()
def monster_hurt(i, damage): monster_list[i].hp = monster_list[i].hp - damage if damage > 0: monster_slashed(i, damage) main.print_message("The " + monster_list[i].name + " is hit for " + str(damage) + " damage") #you kill it? if monster_list[i].hp <= 0: monster_dead(i) return 1 return 0
def attack_monster(i, attack_power): if i == -1: return global cur_mon_hp #find the damage done global num_dice damage = g.die_roll(num_dice, attack_power + 2) damage = damage - g.die_roll(1, monster_list[i].defense + 2) if damage > 0: if monster_hurt(i, damage) == 1: return 0 else: main.print_message("You miss the " + monster_list[i].name + ".")
def test_print_message(capsys): try: main.print_message() out, err = capsys.readouterr() print(f"The output: {type(out)} and the error: {type(err)}") if out == 'look at me I am coding\n': assert "look at me I am coding" == "a different message", 'You should change the message' print("\n") print(out) except SyntaxError: assert ('An invalid syntax' == 'Some words surrounded by quotes' ), "Check your message for quotation marks"
def monster_dead(i): global active_button #make the display look better monster_list[i].hp = 0 #add info to listbox_move; add gold and xp main.print_message("The " + monster_list[i].name + " dies.") if len(monster_list[i].on_death) == 0: gold = monster_list[i].gold player.give_stat("gold", gold) main.print_message("You find " + str(gold) + " " + g.gold_name.lower() + ",") exp = monster_list[i].exp main.print_message("and get " + str(exp) + " " + g.exp_name.lower() + ".") player.add_exp(exp) else: action.activate_lines(g.xgrid, g.ygrid, g.zgrid, monster_list[i].on_death) #finish up if can_leave() == 1: g.break_one_loop += 1 # main.canvas_map.delete("battle") # back_to_main.set("2") g.cur_window = "main" global did_run did_run = 0 else: for i in range(len(monster_list)): if monster_list[i].hp > 0: active_button = i break refresh()
def sell_item(): if curr_focus != 0: return 0 if curr_item > len(item.inv): return 0 if item.inv[curr_item] == -1: return 0 if item.item[item.inv[curr_item]].price == 0: main.print_message("You feel attached to your " + item.item[item.inv[curr_item]].name) return 0 #give the player money #note that all stores have a 5 year, money-back guarantee, #and accept returns from other stores as well. #This means there is no need to doublecheck intent. ;) #FIXME: this is no longer true since gems aren't paid for #full value anywhere but the gem shop #print g.shops[store_num].name if (item.item[item.inv[curr_item]].type == 14 and g.shops[store_num].name == "a Gem Shop"): main.print_message( "The Gem Shop owner is happy to pay the true value of your gems.") player.give_stat("gold", item.item[item.inv[curr_item]].price) else: player.give_stat("gold", item.item[item.inv[curr_item]].value) main.print_message("You sell your " + item.item[item.inv[curr_item]].name + ".") #remove the item item.drop_inv_item(curr_item) #refresh_inv() refresh_shop() show_details()
def runaway(force_success=False): global run_attempts global did_run clear_slashes() #leave/quit if can_leave() == 1: g.break_one_loop += 1 #Attempt to run. Testers get better chance. else: if (g.die_roll(1, 10 + run_attempts) > 7) or \ ((player.name == "testing123" and g.die_roll(1, 10) > 5)) or \ (force_success): main.print_message("You coward! You ran away from the " + monster.monster_groups[mon_index].name + ".") did_run = 1 g.break_one_loop += 1 else: run_attempts += 1 main.print_message("You fail to run away.") attack_player() refresh() return 0
def use_item(item_index=-1): # if action.has_dialog == 1: return 0 if item_index == -1: if curr_item >= len(item.inv) or item.inv[curr_item] == -1: return 0 #put the item[] index of the item into item_value try: item_value = item.inv[curr_item] except IndexError: return 0 #put the equip slot into item_loc item_loc = item.item[item_value].type #if equipment if item_loc < 6: wear_item() return if item_loc == 15: return main.print_message("You use your " + item.item[item_value].name) else: item_value = item_index item_loc = item.item[item_index].type #if item is healing if item_loc == 11: #heal the player, delete the item player.give_stat("hp", item.item[item_value].quality) if item_index == -1: item.drop_inv_item(curr_item) if item_loc == 16 or item_loc == 17: if action.activate_lines(g.xgrid, g.ygrid, g.zgrid, item.item[item_value].scripting) == 1: if item_index == -1: item.drop_inv_item(curr_item) if item_index == -1: refresh_use() refresh_stat_display()
def drop_item(): if action.has_dialog == 1: return 0 if curr_item >= len(item.inv) or item.inv[curr_item] == -1: return 0 try: item_to_delete = item.find_inv_item(item.inv[curr_item]) except IndexError: return 0 if item.item[item.inv[item_to_delete]].price == 0 and \ item.item[item.inv[item_to_delete]].value == 0: main.print_message("You feel attached to your " + item.item[item.inv[item_to_delete]].name) return 0 #the inv[] location of the item is now in item_to_delete. #Ask if the player really wants to drop it. # main.canvas_map.unbind("<ButtonRelease-1>") # main.canvas_map.unbind("<Motion>") tmp_surface = pygame.Surface((300, 200)) tmp_surface.blit(g.screen, (0, 0), (170, 140, 300, 200)) if main.show_yesno("Drop your " + item.item[item.inv[item_to_delete]].name + "?"): g.screen.blit(tmp_surface, (170, 140)) main.print_message("You drop your " + item.item[item.inv[curr_item]].name) # add dropped item to map g.maps[g.zgrid].additem(item.item[item.inv[item_to_delete]].name, g.xgrid, g.ygrid) #remove the item from inventory item.drop_inv_item(curr_item) else: g.screen.blit(tmp_surface, (170, 140)) main.refresh_tile(g.xgrid, g.ygrid, g.zgrid) g.cur_window = "inventory_drop" refresh_drop()
def buy_item(): if curr_focus != 1: return 0 if curr_item >= len(g.shops[store_num].itemlist): return 0 if (g.shops[store_num].itemlist[curr_item].buytype == "gold"): #if enough gold if int(g.shops[store_num].itemlist[curr_item].cost) <= int( player.gold): #if no actions were given if (len(g.shops[store_num].itemlist[curr_item].actions) == 0): #if given successfully. if (main.action.run_command( 0, 0, 0, "item(\"" + g.shops[store_num].itemlist[curr_item].item_name + "\")") == 1): main.print_message( "You buy a " + g.shops[store_num].itemlist[curr_item].item_name + ".") player.give_stat( "gold", -1 * int(g.shops[store_num].itemlist[curr_item].cost)) else: #not enough room main.print_message("Your inventory is full.") else: temp = main.action.activate_lines( 0, 0, 0, g.shops[store_num].itemlist[curr_item].actions) if temp == 1: player.give_stat( "gold", -1 * int(g.shops[store_num].itemlist[curr_item].cost)) else: #skillpoints #if enough skillpoints if int(g.shops[store_num].itemlist[curr_item].cost) <= \ int(player.skillpoints): #if no actions were given if (len(g.shops[store_num].itemlist[curr_item].actions) == 0): #if given successfully. if (main.action.run_command( 0, 0, 0, "item(\"" + g.shops[store_num].itemlist[curr_item].item_name + "\")") == 1): main.print_message( "You buy a " + g.shops[store_num].itemlist[curr_item].item_name + ".") player.give_stat( "skillpoints", -1 * int(g.shops[store_num].itemlist[curr_item].cost)) else: temp = main.action.activate_lines( 0, 0, 0, g.shops[store_num].itemlist[curr_item].actions) if temp == 1: player.give_stat( "skillpoints", -1 * int(g.shops[store_num].itemlist[curr_item].cost)) #refresh_inv() refresh_shop() show_details()
def attack_player_per_monster(i): damage = g.die_roll(1, monster_list[i].attack + 2) damage = damage - g.die_roll(1, player.adj_defense + 2) if damage > 0: main.print_message("The " + monster_list[i].name + " hits you for " + str(damage) + " damage.") player.give_stat("hp", -1 * damage) monster_slashed("h", damage) #you dead yet? if player.hp <= 0: player.hp = 0 main.print_message("The " + monster_list[i].name + " kills you.") cur_button = 1 refresh() refresh_buttons() elif monster_list[i].hp > 0: main.print_message("The " + monster_list[i].name + " misses you.")
def useskill(skill_index, free_skill=0): global active_button #don't do anything if monster is dead. if can_leave() == 1: return 0 clear_slashes() #sanity checks if skill_index >= len(player.skill): return 0 if free_skill == 0: if player.skill[skill_index][5] == 0: return 0 if player.skill[skill_index][2] > player.ep: return 0 if player.skill[skill_index][1] == 5: return 0 #actually use the skill. if player.skill[skill_index][1] == 0: #rage if free_skill == 0: #pay for the skill player.give_stat("ep", -1 * player.skill[skill_index][2]) main.print_message("You fly into a rage.") #increase attack ability player.give_stat("adj_attack", (1 + int(player.level) / 4)) main.refresh_bars() main.refresh_inv_icon() attack_player() elif player.skill[skill_index][1] == 1: #Sneak away if free_skill == 0: #pay for the skill player.give_stat("ep", -1 * player.skill[skill_index][2]) global run_attempts if g.die_roll(1, 10 + int(player.level) + run_attempts) > 4: runaway(True) global did_run did_run = 1 else: run_attempts += 3 main.print_message("You fail to sneak away.") attack_player() elif player.skill[skill_index][1] == 2: #Frenzy if free_skill == 0: #pay for the skill player.give_stat("ep", -1 * player.skill[skill_index][2]) mon_num = select_monster() for i in range(2 + int(player.level) / 4): if monster_list[mon_num].hp < 1: mon_num = select_monster() if mon_num == -1: return 0 attack_monster(mon_num, player.adj_attack) if can_leave() == 1: break active_button = -1 monster_mouse_move((0, 0)) refresh() if can_leave() == 0: attack_player() elif player.skill[skill_index][1] == 3: #Dismember if free_skill == 0: #pay for the skill player.give_stat("ep", -1 * player.skill[skill_index][2]) mon_num = select_monster() if mon_num == -1: return 0 damage = (player.adj_attack) monster_hurt(mon_num, damage) if can_leave() == 0: attack_player() elif player.skill[skill_index][1] == 4 or \ player.skill[skill_index][1] == 6: #Scripted #If the scripting ends with an "end" command, if action.activate_lines(g.xgrid, g.ygrid, g.zgrid, player.skill[skill_index][6]) == 1: if free_skill == 0: #pay for the skill player.give_stat("ep", -1 * player.skill[skill_index][2]) if can_leave() == 0: attack_player() main.refresh_bars() if can_leave() == 1: inv.leave_inner() return 0 active_button = -1 monster_mouse_move((0, 0)) refresh() #replace the cursor if free_skill == 0: inv.leave_inner() refresh_buttons()
def useitem(item_index, leave_item=0): global active_button #don't do anything if battle is over. if can_leave() == 1 or item_index == -1: return 0 clear_slashes() item_value = item_index item_type = g.item.item[item_value].type #if item is healing if item_type == 11: #heal the player, delete the item player.give_stat("hp", g.item.item[item_value].quality) main.print_message("You are healed for " + str(g.item.item[item_value].quality) + " " + g.hp_name + ".") if leave_item == 0: g.item.drop_inv_item(g.item.find_inv_item(item_value)) attack_player() #if item is a gem if item_type == 14: global old_attack old_attack = player.adj_attack gem_power = g.item.item[item_value].quality #gem power increases attack strength potential player.give_stat("adj_attack", gem_power * 0.75) #gem power increases num_dice (ie. chance to hit and total damage) global num_dice num_dice = num_dice + gem_power / 4 main.print_message("The " + g.item.item[item_value].name + " focuses the power of your " + g.attack_name.lower() + ".") main.refresh_bars() if leave_item == 0: g.item.drop_inv_item(g.item.find_inv_item(item_value)) global used_gem used_gem = 1 if leave_item == 0: attack() #if item is explosive if item_type == 12: sel_mon = select_monster() if sel_mon == -1: return 0 #if monster is still alive if monster_list[sel_mon].hp > 0: damage = int(g.item.item[item_value].quality) if leave_item == 0: g.item.drop_inv_item(g.item.find_inv_item(item_value)) monster_hurt(sel_mon, damage) if can_leave() == 0 and leave_item == 0: attack_player() active_button = -1 #If item is scripted: if item_type == 15 or item_type == 17: #If the scripting ends with an "end" command, if action.activate_lines(g.xgrid, g.ygrid, g.zgrid, g.item.item[item_value].scripting) == 1: if leave_item == 0: g.item.drop_inv_item(g.item.find_inv_item(item_value)) if can_leave() == 0 and leave_item == 0: attack_player() active_button = -1 #If item is equippable: if item_type < 6: #trade the item and whatever's in the equip slot temp = player.equip[item_type] player.equip[item_type] = item_index g.item.drop_inv_item(g.item.find_inv_item(item_index)) g.item.take_inv_item(temp) main.print_message("You equip yourself with your " + g.item.item[player.equip[item_type]].name + ".") player.reset_stats() if can_leave() == 0 and leave_item == 0: attack_player() if can_leave() == 1: inv.leave_inner() return 0 refresh() #back to battle if leave_item == 0: inv.leave_inner() refresh_buttons()
def begin(mon_index_input): # global window_battle global bgcolour bgcolour = "lightgrey" # window_battle = Frame(g.window_main, bd=4, relief=GROOVE, bg=bgcolour) # window_battle.grid(row=0, column=0) global mon_index mon_index = mon_index_input global monster_list monster_list = [] for line in monster.monster_groups[mon_index].monster_list: monster_list.append( monster.copy_monster( monster.monsters[monster.monster_name_to_index(line)])) for i in range(len(monster_list)): monster_list[i].reset() if g.difficulty == 0: monster_list[i].hp -= monster_list[i].hp / 5 monster_list[i].maxhp -= monster_list[i].maxhp / 5 monster_list[i].attack -= monster_list[i].attack / 5 monster_list[i].defense -= monster_list[i].defense / 5 if g.difficulty == 2: monster_list[i].hp += monster_list[i].hp / 5 monster_list[i].maxhp += monster_list[i].maxhp / 5 monster_list[i].attack += monster_list[i].attack / 5 monster_list[i].defense += monster_list[i].defense / 5 global did_run did_run = 0 global cur_button cur_button = 0 global run_attempts run_attempts = 0 global active_button active_button = -1 # Set the current window g.cur_window = "battle" global canvas_mon_pic global background_pic background_pic = g.maps[g.zgrid].battle_background global monster_pic monster_pic = [] global base_mon_hp_start base_mon_hp_start = [] global base_mon_hp_width base_mon_hp_width = [] global base_mon_hp_y_start base_mon_hp_y_start = [] global monster_slashes monster_slashes = [] global monster_start monster_start = (main.tilesize * main.half_mapx - background_pic.get_width() / 2, main.tilesize * main.half_mapy - background_pic.get_height() / 2) for i in range(len(monster_list)): monster_slashes.append([0, 0, 0]) try: monster_pic.append(g.tiles["monsters/" + monster_list[i].name + ".png"]) except KeyError: monster_pic.append(g.tiles["monsters/generic.png"]) #if x and y positions were given, use them; otherwise, start at the #middle, and go right. This works for 1 or two monsters, but xy #coords are recommended for more. if len(monster.monster_groups[mon_index].x_pos) > i: xstart = monster_start[0] + monster.monster_groups[ mon_index].x_pos[i] else: xstart = monster_start[0] + background_pic.get_width() / 2 + i * 40 ystart = y_start(i) base_mon_hp_start.append(xstart - monster_pic[i].get_width() / 2) base_mon_hp_width.append(monster_pic[i].get_width()) base_mon_hp_height.append(monster_pic[i].get_height()) base_mon_hp_y_start.append(ystart + 5 + monster_pic[i].get_height()) monster_slashes.append([0, 0, 0]) global hero_pic try: hero_pic = g.tiles["people/hero_n" + g.maps[g.zgrid].hero_suffix + ".png"] except KeyError: hero_pic = g.tiles["blank"] global hero_loc hero_loc = (monster_start[0] + (background_pic.get_width() - base_mon_hp_width[0]) / 2, background_pic.get_height() - hero_pic.get_height() * 5 / 2 + monster_start[1]) global attack_button_loc global item_button_loc global skill_button_loc global inspect_button_loc global run_button_loc global final_button_loc global button_y_start global button_height attack_button_loc = main.tilesize * main.half_mapx - \ g.buttons["attack.png"].get_width() - g.buttons["use.png"].get_width() - \ g.buttons["skill.png"].get_width() / 2 item_button_loc = attack_button_loc + g.buttons["attack.png"].get_width() skill_button_loc = item_button_loc + g.buttons["use.png"].get_width() inspect_button_loc = skill_button_loc + g.buttons["skill.png"].get_width() run_button_loc = inspect_button_loc + g.buttons["inspect.png"].get_width() final_button_loc = run_button_loc + g.buttons["quit.png"].get_width() button_y_start = main.tilesize * main.half_mapy + background_pic.get_height( ) / 2 button_height = g.buttons["attack.png"].get_height() g.create_norm_box((attack_button_loc, button_y_start), (final_button_loc - attack_button_loc, button_height)) #bindings bind_keys() main.print_message(monster.monster_groups[mon_index].attack_message) set_description_text(0) refresh() refresh_buttons() while 1: pygame.time.wait(30) g.clock.tick(30) if g.break_one_loop > 0: g.break_one_loop -= 1 break for event in pygame.event.get(): if event.type == pygame.QUIT: g.break_one_loop = 2 elif event.type == pygame.KEYDOWN: if key_handler(event.key) == 1: break elif event.type == pygame.MOUSEMOTION: if (event.pos[1] > button_y_start and event.pos[1] < button_height + button_y_start): mouse_handler_move(event.pos) elif event.type == pygame.MOUSEBUTTONDOWN: mouse_handler_move(event.pos) if key_handler(pygame.K_RETURN) == 1: break tmpjoy = g.run_joystick() if tmpjoy != 0: key_handler(tmpjoy) if g.unclean_screen: pygame.display.flip() if player.hp <= 0: did_run = "end" return did_run
def inv_savegame(): if action.has_dialog == 1: return 0 g.savegame(player.name) main.print_message("** Game Saved **") pygame.display.flip()