Programming

Here are some of my programming projects, since I don’t have a github account quite yet. It’s a good spot to post some stuff, ’cause it’s all mine!

hawkmoon.py

This was my first ‘for fun’ program. Oh yeah, there’s a lot of spaghetti code lying around this one.

#Jake Shaw's simulation of hawkmoon:
#code simulates a buff being applied to three random
#stackable rounds in a 13-round magazine
#objective is for the code to demonstrate probability
#of two-round defeats and which bullets are buffed
#also demonstrating potential damage
#based on a turn-based fighting game

#import the random system and start the primary while loop
#and establishing values to be modified by the range and
#precision hit functions

from tkinter import *
from random import *
import random
import math
import sys
import time

print("Hawkmoon Rumble!")
print("Your objective is to gamble with your gun and crush your opponents!")
bot_op = input("Bot mode? (y/n): ")
if bot_op == 'y':
    bot_mode = True
else:
    bot_mode = False
testing_choice = input("Testing mode? (y/n): ")
if testing_choice == 'y':
    test_mode = True
else:
    test_mode = False
print("Starting in three seconds...")
time.sleep(3)
print("\n")

def game():
    
    loop = True
    while loop:
        base_dmg = 65
        base_buff = 81.25
        dmg_dealt = 0
        evade_count = 0
        turn_count = 1
        move_count = 0
        dmg_got = 0

        def turn_it():
            turn_count = turn_count + 1
        def move_it():
            move_count = move_count + 1

        time.sleep(2)

        if test_mode == True:
            range = str(input("Enter a range between 0 and 50, or say 'no' to roll randomly: "))
            if range == 'no':
                range = randint(0,45)
                test_mode == True
                pass

            range = int(range)

        if test_mode == False:
            range = randint(0,45)
        if range <= 5:
            close_range_lib = ("You round the corner and bump into an opponent.", "You startle from a close enemy.")
            print(random.choice(list(close_range_lib)))
        elif range >= 40:
            far_range_lib = ("You spot an opponent across the map.", "You see an opponent way out there.")
            print(random.choice(list(far_range_lib)))
        elif 6 <= range <= 15:
            near_range_lib = "There's an opponent across the room.","You stare down an enemy and they turn to face you."
            print(random.choice(list(near_range_lib)))
        elif 16 <= range <= 25:
            not_quite_close_range = ("You see a ping on your radar.","There's some movement in the next room you can hear.")
            print(random.choice(list(not_quite_close_range)))
        elif 26 <= range <= 39:
            print("You see an opponent duck into a lane.")

        if test_mode == True:
            print("Range: ", range)

        #random engagement distance set
        
        enemy_class = ('warlock', 'titan', 'hunter')
        opponent = random.choice(list(enemy_class))
        print("It's an enemy",opponent,".")
        if opponent == 'warlock':
            res_stat = randint(1,6)
            rec_stat = randint(3,9)
            agi_stat = randint(3,6)
        elif opponent == 'hunter':
            res_stat = randint(3,6)
            rec_stat = randint(3,6)
            agi_stat = randint(6,9)
        elif opponent == 'titan':
            res_stat = randint(6,9)
            rec_stat = randint(3,6)
            agi_stat = randint(1,3)

        #use hardcoded class statistics to generate an enemy
        #use random integers to generate random stats for player

        op_health = res_stat + 197
        op_recov = rec_stat + 20
        op_mob = agi_stat + rec_stat - 2
        op_accuracy = int(-(1/20 * range**2) + (range - 5) + ((9 * agi_stat) + 19))

        self_mob = 6 + random.randint(1, 3)
        self_rec = 24 + random.randint(-2, 4)
        self_health = 197 + random.randint(1, 4)

        self_health_base = self_health
        op_health_base = op_health

        start_range = range

        e_wep_range = range

        #damage values for enemy weapons from e_wep_range

        sa_dmg = (-1/30 * e_wep_range**2) + 30
        scout_dmg = (-1/80 * e_wep_range**2) + 65
        pulse_dmg = (-1/120 * e_wep_range**2) + 75
        sg_dmg = -(e_wep_range**2) + 200
        ar_dmg = (-1/100 * e_wep_range**2) + 70
        smg_dmg = (-1/20 * e_wep_range**2) + 25
        hc_dmg = (-1/50 * e_wep_range**2) + 60
        bow_dmg = (-1/150 * e_wep_range**2) + 90
        sniper_dmg = (-1/300 * e_wep_range**2) + 110
        fusion_dmg = (-1/10 * e_wep_range**2) + 180
        gl_dmg = (-1 * e_wep_range**2) + 50 + randint(5,50)

        #associating an enemy weapon library with damage values
        equipment = {'sidearm':sa_dmg, 'grenade launcher':gl_dmg, 'SMG':smg_dmg, 'scout rifle':scout_dmg, 'pulse rifle':pulse_dmg, 'shotgun':sg_dmg, 'auto rifle':ar_dmg, 'hand cannon':hc_dmg, 'sniper rifle':sniper_dmg, 'bow':bow_dmg, 'fusion rifle':fusion_dmg}
        opponent_equip = random.choice(list(equipment))

        #making sure that if a damage equation drops into negative values, the value of that attack is 0
        #print part of the assesement statement to give player engagement option
        op_atk = int(equipment[opponent_equip])
        if op_atk <= 0:
            op_atk = 0
        op_equip_lib = ("It looks like they have a","They appear to be carrying a","They're armed with a")
        print(random.choice(list(op_equip_lib)), opponent_equip,".")

        #pre-advantage-statement
        pre_adv_lib = ["Opponent is running away.","The opponent isn't looking your way.","The opponent is focused elsewhere."]
        if opponent_equip == 'sidearm' or 'shotgun':
            if range > 30:
                print(random.choice(list(pre_adv_lib)))

        if bot_mode == True:
            time.sleep(2)

        def engage_choice():
            no_engage_lib = ("You search for another target.", "You'd rather back off.", "You leave them alone.")
            if bot_mode == True:
                engage_lib = 'y', 'n'
                choice = (random.choice(list(engage_lib)))
            elif bot_mode == False:
                engage_decision = input("Do you want to engage? (y/n): ")
                if bot_mode == 'y':
                    engage_decision == 'y'
                if engage_decision == 'y':
                    loop = True
                else:
                    print(random.choice(list(no_engage_lib)))
                    print("\n")
                    sys.exit(game())
        engage_choice()

        print("\n")


        #set hawkmoon damage in proportion to range using this equation I whipped up on desmos.com using test points
        #range function
        def dmg_get(base_dmg):
            base_dmg = (-1/50 * range**2) + 65

        dmg_get(base_dmg)

        #precision hit out of a 75/25 split chance, favoring accuracy

        #if random.randint(0,100) < 75:
        #   crit_select = ('y')
        #   if crit_select == 'y':
        #       base_dmg = base_dmg * 1.31 + 9
        #       print("Precision bonus active.")
        #   else:
        #       print("No precision damage bonus applied.")
        
        buff_1 = int(base_dmg * 1.25)

        mag = {1:base_dmg , 2:base_dmg, 3:base_dmg, 4:base_dmg, 5:base_dmg, 6:base_dmg, 7:base_dmg, 8:base_dmg, 9:base_dmg, 10:base_dmg, 11:base_dmg, 12:base_dmg, 13:base_dmg}

        #selecting which rounds are buffed and displaying to user

        buffed = random.choice(list(mag))
        buffed2 = random.choice(list(mag))
        buffed3 = random.choice(list(mag))

        #buff damage stacks on top of range and precision hit
        #Brandon at the TLC helped me out with the next 3 lines here. It was really rough
        #trying to figure out where to go with making sure twice or thrice buffed rounds
        #display their damage properly. I was a couple of characters away!

        mag[buffed] = mag[buffed] * 1.25
        mag[buffed2] = mag[buffed2] * 1.25
        mag[buffed3] = mag[buffed3] * 1.25

        buffed_rounds = buffed, buffed2, buffed3

        inspect = print ("These are your buffed rounds...:",buffed_rounds)
        inspect
        
        #displaying base damage values after range and precision hit functions
        print ("Your bullets will deal", int(base_dmg), "damage.")
        print ("A buffed round will deal", buff_1, "damage.")

        if op_atk <= 0:
            op_atk = 0
            print("Your opponent is defenseless! Crush them!")

        #dialogue to show user if they have engagement advantage thanks to RNG
        if mag[13] + mag[12] + mag[11] >= op_health:
            print("You have advantage.")

        if bot_mode == True:
            time.sleep(2)

        #create a secondary loop for engagement options
        #setup engagement loop and arguements for exiting loop
        engaged = True
        while engaged:
            opponent_defeat = False
            if evade_count == 3:
                print("You were able to get away.")
                return(game())
            if dmg_got >= self_health:
                print("You died.")
                dead = True
                print("Respawning...")
                print("\n")
                time.sleep(3)
                break
            if op_health <= 0:
                opponent_defeat = True
                print("Opponent defeated!")
                print("\n")
                time.sleep(1)
                break
            if op_atk <= 0:
                op_atk = 0
            mag_count = len(list(mag))
        
            print("\n")
            print("Turn", turn_count)
            print (mag_count, "/ 13 rounds left in magazine.")
            #I want to code a call that sees if the next round fired
            #is a buffed round

            #bot mode options, prioritizing firing the weapon
            #and giving the bot options to leave the fight
            #or evade the enemy
            if bot_mode == True:
                choice_lib = 'fire', 'fire', 'fire', 'evade', 'approach'
                if test_mode == True:
                    print("Range: ", range)
                if len(mag) <= 5:
                    if test_mode == True:
                        print("try force reload")
                    if randint(1,5) < 4:
                        choice == 'reload'
                elif self_health <= op_health/2:
                    if test_mode == True:
                        print("try force evade")
                    if randint(1,5) <= 2:
                        choice == 'evade'
                elif self_health <= self_health_base/4:
                    if test_mode == True:
                        print("try force disengage")
                    de_engage_roll = randint(1,3)
                    if de_engage_roll < 3:
                        choice == 'disengage'
                elif op_health <= self_health:
                    choice == 'fire'
                    if test_mode == True:
                        print("force choice fire")
                else:
                    choice = (random.choice(list(choice_lib)))
                    if test_mode == True:
                        print("random choice selected: ",choice)
                if choice == 'approach':
                    if test_mode == True:
                        print("test approach")
                    approach_range = randint(1,9)
                    op_atk = op_atk + int(approach_range**1/2)
                    range = range - approach_range
                    if range <= 0:
                        range = 0
                    print("You got", approach_range, "meters closer.")
                    dmg_get(base_dmg)
                    move_count = move_count + 1
                    turn_count = turn_count + 1
                    engaged = True
                if choice == 'evade':
                    if test_mode == True:
                        print("test evade")
                    print("Evading...")
                    evade_engage = randint(1,9)
                    range = range + evade_engage
                    op_atk = op_atk - int(evade_engage**1/2)
                    self_health = self_health + self_rec
                    if self_health > self_health_base:
                        self_health = self_health_base
                    evade_count = evade_count + 1
                    print("You moved", evade_engage,"meters away and healed", self_rec, "points.")
                    print("You have", self_health,"health remaining.")
                    dmg_get(base_dmg)
                    turn_count = turn_count + 1
                    move_count = move_count + 1
                    engaged = True

                elif choice == 'fire':
                    if test_mode == True:
                        print("test fire")
                    if mag_count == 0:
                        print("You have to reload!")
                    if mag_count >= 1 and op_health > 0:
                        print("Firing.")
                        player_luck = randint(1,100)
                        if player_luck > 15:
                            if random.randint(0,100) < 75:
                                crit = True
                                print("Precision hit!")
                                dmg_dealt = dmg_dealt + (int(int(list(mag.values())[mag_count - 1]) * 1.31) + 9)
                                op_health = op_health - (int(int(list(mag.values())[mag_count - 1]) * 1.31) + 9)
                                print((int(int(list(mag.values())[mag_count - 1]) * 1.31) + 9), "damage dealt.")
                            else:
                                dmg_dealt = dmg_dealt + int(list(mag.values())[mag_count - 1])
                                op_health = op_health - int(list(mag.values())[mag_count - 1])
                                print(int(list(mag.values())[mag_count - 1]), "damage dealt.")
                            try:
                               mag.popitem()    
                            except KeyError:
                                print("You missed.")
                            if op_health <= 0:
                                op_health = 0
                            print("Your opponent has", op_health, "health remaining.")
                            turn_count = turn_count + 1
                        elif player_luck <= 15:
                            missed_lib = "Your shot missed.", "Your bullet ricochets off the wall behind your opponent.", "You hit the enemy's helmet, but it glances at an angle."
                            
                            mag.popitem()
                            print(random.choice(list(missed_lib)))
                            turn_count = turn_count + 1
                            engaged = True
                            
                    firing = True
                elif choice == 'reload':
                    if test_mode == True:
                        print("test reload")
                    if mag_count == 13:
                        print("Reload attempted; your magazine is already full!")
                    else:
                        print("Reloading...")
                        mag = {1:base_dmg , 2:base_dmg, 3:base_dmg, 4:base_dmg, 5:base_dmg, 6:base_dmg, 7:base_dmg, 8:base_dmg, 9:base_dmg, 10:base_dmg, 11:base_dmg, 12:base_dmg, 13:base_dmg}

                        buffed = random.choice(list(mag))
                        buffed2 = random.choice(list(mag))
                        buffed3 = random.choice(list(mag))

                        mag[buffed] = mag[buffed] * 1.25
                        mag[buffed2] = mag[buffed2] * 1.25
                        mag[buffed3] = mag[buffed3] * 1.25

                        buffed_rounds = buffed, buffed2, buffed3

                        inspect = print ("These are your buffed rounds...:",buffed_rounds)
                        inspect
                elif choice == 'disengage':
                    if test_mode == True:
                        print("test disengage")
                    luck = randint(-1,3)
                    if luck + self_mob > op_mob:
                        print("You escaped the fight.")
                        return game()
                    elif luck + self_mob <= op_mob:
                        print("Escape failed, they're on you. Fight on!")
                        turn_count = turn_count + 1
                        engaged = True
                time.sleep(2)
                
            elif bot_mode == False:    
                choice = (input("You can 'fire', 'evade', 'approach', 'reload', or 'disengage': "))
                if choice == 'approach':
                    approach_range = randint(1,9)
                    op_atk = op_atk + int(approach_range**1/2)
                    range = range - approach_range
                    if range <= 0:
                        range = 0
                    print("You got", approach_range, "meters closer.")
                    dmg_get(base_dmg)
                    move_count = move_count + 1
                    turn_count = turn_count + 1
                    engaged = True
                if choice == 'evade':
                    evade_engage = randint(1,9)
                    range = range + evade_engage
                    op_atk = op_atk - int(evade_engage**1/2)
                    self_health = self_health + self_rec
                    if self_health > self_health_base:
                        self_health = self_health_base
                    evade_count = evade_count + 1
                    print("You moved", evade_engage,"meters away and healed", self_rec, "points.")
                    print("You have", self_health,"health remaining.")
                    dmg_get(base_dmg)
                    turn_count = turn_count + 1
                    move_count = move_count + 1
                    engaged = True

                #this part was rough. I didn't even know what a dictionary was
                #when I started this, but the textbook and stackoverflow helped out.
                elif choice == 'fire':
                    if mag_count == 0:
                        print("You have to reload!")
                    if mag_count >= 1 and op_health > 0:
                        player_luck = randint(1,100)
                        if player_luck > 15:
                            print(int(list(mag.values())[mag_count - 1]), "damage dealt.")
                            dmg_dealt = dmg_dealt + int(list(mag.values())[mag_count - 1])
                            op_health = op_health - int(list(mag.values())[mag_count - 1])
                            try:
                               mag.popitem()    
                            except KeyError:
                                print("You missed.")
                            if op_health <= 0:
                                op_health = 0
                            print("Your opponent has", op_health, "health remaining.")
                            turn_count = turn_count + 1
                        elif player_luck <= 15:
                            missed_lib = "Your shot missed.", "Your bullet ricochets off the wall behind your opponent.", "You hit the enemy's helmet, but it glances at an angle."
                            
                            mag.popitem()
                            print(random.choice(list(missed_lib)))
                            turn_count = turn_count + 1
                            engaged = True
                            
                    firing = True
                elif choice == 'reload':
                    if mag_count == 13:
                        print("Your magazine is already full!")
                        turn_count = turn_count + 1
                    else:
                        print("Reloading...")
                        time.sleep(1)
                        mag = {1:base_dmg , 2:base_dmg, 3:base_dmg, 4:base_dmg, 5:base_dmg, 6:base_dmg, 7:base_dmg, 8:base_dmg, 9:base_dmg, 10:base_dmg, 11:base_dmg, 12:base_dmg, 13:base_dmg}

                        buffed = random.choice(list(mag))
                        buffed2 = random.choice(list(mag))
                        buffed3 = random.choice(list(mag))

                        mag[buffed] = mag[buffed] * 1.25
                        mag[buffed2] = mag[buffed2] * 1.25
                        mag[buffed3] = mag[buffed3] * 1.25

                        buffed_rounds = buffed, buffed2, buffed3

                        inspect = print ("These are your buffed rounds...:",buffed_rounds)
                        inspect
                elif choice == 'disengage':
                    if range >= 30:
                        print("You escaped the fight.")
                        print("\n")
                        return(game())
                    else:
                        luck = randint(-3,3)
                        if luck + self_mob > op_mob:
                            print("You escaped the fight.")
                            print("\n")
                            return(game())
                        elif luck + self_mob <= op_mob:
                            print("They're on you. Fight on!")
                            turn_count = turn_count + 1
                            engaged = True

            #opponent option selects
            
            if op_health > 0:
                print("\n")
                print("Turn", turn_count)
                if test_mode == True:
                    print("Range: ", range)
                op_choice = randint(1,6)
                if op_atk <= 10:
                    op_choice == 2
                if op_choice == 2:
                    print("The",opponent.capitalize(),"moves closer.")
                    op_approach = randint(1,9)
                    range = range - op_approach
                    dmg_get(base_dmg)
                    op_atk = op_atk + int(op_approach**1/2)
                    if range < 6:
                        if randint(1,3) < 3:
                            if opponent == 'titan':
                                melee_dmg = 125
                            elif opponent == 'hunter':
                                melee_dmg = 115
                            elif opponent == 'warlock':
                                melee_dmg = 105
                        print("The",opponent.capitalize(),"melees you for",melee_dmg)
                        dmg_got = dmg_got + melee_dmg
                    turn_count = turn_count + 1
                    move_count = move_count + 1
                if op_choice >2:
                    if op_accuracy > 85:
                        #rolling the dice on getting instakilled by a sniper
                        if opponent_equip == 'sniper rifle':
                            headshot = randint(1,10)
                            if headshot > 7:
                                print(opponent.capitalize(), "landed a headshot on you.")
                                print("You are dead.")
                                print("Respawning...")
                                turn_count = turn_count + 1
                            
                            break
                    print("The",opponent.capitalize(),"fires their",opponent_equip.rstrip("_"),".")
                    op_luck = randint(1, 15) + int(op_accuracy)
                    if op_luck >= 85:
                        op_atk = int(op_atk * 1.31)
                        print("Precision shot!") 

                    if choice == 'evade':
                        if randint(1,2) == 1:
                            op_luck = op_luck - 50
                    if op_luck <= 0:
                        op_luck = 0
                    base_ac = 100 - op_luck
                    if base_ac < 0:
                        base_ac = 0
                    op_hit_roll = randint(1,base_ac)
                    if test_mode == True:
                        print("luck = ",op_luck, "hit roll = ",op_hit_roll, "accuracy = ",base_ac)
                    if op_luck >= op_hit_roll:
                        dmg_got = dmg_got + op_atk
                        print("Opponent landed their shot for", op_atk, "damage.")
                        print("You have", self_health - dmg_got, "health remaining.")
                        turn_count = turn_count + 1
                    else:
                        print("Opponent missed their shot!")
                        turn_count = turn_count + 1
                elif op_choice <= 1:
                    op_evade = randint(1,9)
                    range = range + op_evade
                    print("Opponent is evading you.")
                    dmg_get(base_dmg)
                    op_atk = op_atk - int(op_evade**1/2)
                    op_health = op_health + op_recov
                    if op_health > op_health_base:
                        op_health = op_health_base
                    print("Opponent healed for", op_recov, "points!")
                    turn_count = turn_count + 1
                    move_count = move_count + 1

            if bot_mode == True:
                time.sleep(2)

        #append current round stats to end of txt file to save past progress
        #I want to use this to calculate chance of buffed rounds before revealed to player
        #or something, I have no clue yet really

        turn_stats = turn_count,'turns taken with', self_health - dmg_got,' health left.'
        buff_stats = 'Buffed rounds were:', str(buffed_rounds), 'Base damage:', int(base_dmg)
        enemy_stats = opponent.capitalize(), opponent_equip.capitalize(), 'Opponent attack:', op_atk
        range_stats = 'Start range:',start_range, 'End range:',range, 'Movements:', move_count

        win_count = 1
        loss_count = 1

        if bot_mode == True:
            botfile = open('hm_game_bot.txt', 'r')
            botfile.read
            for line in botfile:
                if opponent_defeat == True:
                    win_count = win_count + 1
                if opponent_defeat == False:
                    loss_count = loss_count + 1
            winloss = win_count / loss_count

            botfile.close()

            botfile = open('hm_game_bot.txt', 'a')
            botfile.write(str(winloss))
            botfile.write("\n")

            botfile.close()

            winloss_stat = winloss, "win rate."
            
        def save_data():
            hawkfile = open('hawkmoon_game.txt', 'a')
            hawkfile.write(str(buff_stats))
            hawkfile.write("\n")
            hawkfile.write(str(turn_stats))
            hawkfile.write("\n")
            hawkfile.write(str(enemy_stats))
            hawkfile.write("\n")
            hawkfile.write(str(range_stats))
            hawkfile.write("\n")
            if opponent_defeat == True:
                hawkfile.write("Round won.")
                hawkfile.write("\n")
            elif opponent_defeat == False:
                hawkfile.write("Round lost.")
                hawkfile.write("\n")
            hawkfile.write("---")
            hawkfile.write("\n")
            hawkfile.close()

        save_data()
            
        #close primary loop option select
        if bot_mode == True:
            loop == True
        else:
            reroll = input("Feeling lucky? (y/n): ")
            if reroll == 'y':
                loop = True
                print("Rerolling and reloading...")
                print("\n")
            elif reroll == 'n':
                print("Holstering...")
                sys.exit(0)
                loop = False

game()
    
#if I were to continue with this program, I would introduce a gui with
#sweet ASCII art of hawkmoon, and a gameboy-era pokemon dialog window
#where a more image-based battle takes place instead of zork


%d bloggers like this: