I'm looking for tips on cleaning this up. I think it's legible, but it sure ain't pretty. In particular, I'd love to clean up the hitCheck
function. I've dealt with switches before in other languages, so I was kind of stuck on trying simulating that. getReady
looks messy to me, but I'm not sure how I'd go about tidying that up either.
import random
import time
def hitCheck(rng, acc):
hitTotal = acc + rng
if hitTotal > 190:
print("CRIT! HEADSHOT!")
return 5
if 160 < hitTotal < 191:
print("Lethal!")
return 4
if 120 < hitTotal < 161:
print("Severe!")
return 2
if 80 < hitTotal < 121:
print("Ouch!")
return 1
if 40 < hitTotal < 81:
print("Grazed!")
return 0.5
if 0 < hitTotal < 41:
print("Miss!")
return 0
def firing(begin):
print("BANG!")
#end & start variables are used to tell how fast the user is pressing the key.
#The faster they go, the more inaccurate their shots.
end = time.time()
accuracy = int((end - begin) * 60)
if accuracy > 100:
accuracy = 100
#print("Accuracy: ", accuracy)
#Not sure if this 'luck' generator is mathematically different
#from just using 'int((random.random() * 100))'.
luck1 = int((random.random() * 50))
luck2 = int((random.random() * 50))
luck = luck1 + luck2
#print("Luck: ", luck)
return hitCheck(luck, accuracy)
#Here's the main part of the app.
def getReady():
running = False
while not running:
i = 0
downed = 0
total = 0
while True:
print("\n")
total = input('Max attempts: ')
print("\n")
if total == '' or int(total) < 0:
print("Value must be higher than 0!")
else:
total = int(total)
break
print("Fire away!")
while i < total:
#Other half to figuring out the accuracy.
start = time.time()
input()
downed += firing(start)
i += 1
print(total - i, " shots left.")
time.sleep(0.1)
#Print the results!
print("\n")
print("Injuries:", int(downed))
print("You felled", round(downed / 5), "foes!")
print("\n")
#Shall we continue?
while True:
running = input("More? yes / no: ")
if running in ('no', 'n'):
running = True
break
elif running in ('yes', 'y'):
running = False
break
else:
continue
getReady()