My first attempt at programming using Python was to make a calculator for an online game I play called AstroEmpires.
The user can input the number of bases they have, the shipyards on each base and the base's production value. They then tell the program which ship they wish to build and how many of that ship. The calculator tells them how much this will cost, how many of the ship to build at each base for maximum efficiency and how long this will take. It also saves the base, shipyard and production values.
It works, but I think that it is too messy; it takes 206 lines of code. I am wondering if there is anything I can do to condense the code.
Any help will be appreciated :)
loop = 1
ships = [' Fighters ' ,\
' Bombers ' ,\
' Heavy Bombers ' ,\
' Ion Bombers ' ,\
' Corvette ' ,\
' Recycler ' ,\
' Destroyer ' ,\
' Frigate ' ,\
' Ion Frigate ' ,\
' Scout Ship ' ,\
' Outpost Ship ' ,\
' Cruiser ' ,\
' Carrier ' ,\
' Heavy Cruiser ' ,\
' Battleship ','Fleet Carrier']
ships_cost = {' Fighters ' : 5 , \
' Bombers ' : 10 , \
' Heavy Bombers ' : 30 , \
' Ion Bombers ' : 60 , \
' Corvette ' : 20 , \
' Recycler ' : 30 , \
' Destroyer ' : 40 , \
' Frigate ' : 80 , \
' Ion Frigate ' : 120 , \
' Scout Ship ' : 40 , \
' Outpost Ship ' : 100 , \
' Cruiser ' : 200 , \
' Carrier ' : 400 , \
' Heavy Cruiser ' : 500 , \
' Battleship ' : 2000 , \
' Fleet Carrier ' : 2500 \
}
ships_shipyard = {' Fighters ' : 1 , \
' Bombers ' : 2 , \
' Heavy Bombers ' : 3 , \
' Ion Bombers ' : 3 , \
' Corvette ' : 4 , \
' Recycler ' : 5 , \
' Destroyer ' : 6 , \
' Frigate ' : 8 , \
' Ion Frigate ' : 8 , \
' Scout Ship ' : 4 , \
' Outpost Ship ' : 8 , \
' Cruiser ' : 10 , \
' Carrier ' : 12 , \
' Heavy Cruiser ' : 12 , \
' Battleship ' : 16 , \
'Fleet Carrier' : 16 }
print "Welcome to AE production calculator, written by Angus Goldsmith, a.k.a. Cowbacca!"
while loop == 1:
print"1) Yes"
print"2) No"
saved = input("Use saved data?")
if saved == 1:
import pickle
unpickleshipyards = open('shipyardlevels.txt', 'r')
# now load the list that we pickled into a new object
unpickledshipyards = pickle.load(unpickleshipyards)
# close the file, just for safety
unpickleshipyards.close()
base_shipyards = unpickledshipyards
import pickle
unpickleproduction = open('baseproduction.txt', 'r')
# now load the list that we pickled into a new object
unpickledproduction = pickle.load(unpickleproduction)
# close the file, just for safety
unpickleproduction.close()
base_production = unpickledproduction
import pickle
unpicklebases = open('bases.txt', 'r')
# now load the list that we pickled into a new object
unpickledbases = pickle.load(unpicklebases)
# close the file, just for safety
unpicklebases.close()
bases = unpickledbases
else:
bases = 0
base_production = []
base_shipyards = []
if saved == 2:
bases = input("Type your number of bases:")
import pickle
file = open('bases.txt', 'w')
pickle.dump(bases,file)
file.close()
if saved == 2:
temp_bases = bases
shipyards = 0
base_number1 = 1
while temp_bases > 0:
print "Type Base ",base_number1,"'s shipyard level:"
base_shipyards.append (input())
base_number1 = base_number1+1
temp_bases = temp_bases-1
print ""
temp_bases2 = bases
import pickle
file = open('shipyardlevels.txt', 'w')
pickle.dump(base_shipyards,file)
file.close()
base_number = 1
if saved == 2:
while temp_bases2 > 0:
print "Type Base ",base_number,"'s production value:"
base_production.append (input())
temp_bases2 = temp_bases2-1
base_number = base_number+1
print ""
import pickle
file = open('baseproduction.txt', 'w')
pickle.dump(base_production,file)
file.close()
choice = 0
print "Ship list:"
print " 1) Fighters"
print " 2) Bombers "
print " 3) Heavy Bombers "
print " 4) Ion Bombers "
print " 5) Corvette "
print " 6) Recycler "
print " 7) Destroyer "
print " 8) Frigate "
print " 9) Ion Frigate "
print " 10) Scout Ship "
print " 11) Outpost Ship "
print " 12) Cruiser "
print " 13) Carrier "
print " 14) Heavy Cruiser "
print " 15) Battleship "
print " 16) Fleet Carrier "
choice = input("What do you wish to produce?")-1
quantity = 0
print "How many",ships[choice],"do you want to build?"
quantity = input("Quantity:")
ship_name = ships[choice]
total_cost = ships_cost[ship_name]*quantity
print "Total cost is ",total_cost,"."
temp_bases3 = 0
temp_bases3 = bases
total_production = 0
while temp_bases3 >0:
base_prod = base_production[temp_bases3-1]
total_production = total_production + base_prod
temp_bases3 = temp_bases3-1
temp_bases4 = bases
while temp_bases4 >0:
if base_shipyards[temp_bases4-1]<ships_shipyard[ship_name]:
total_production = total_production-base_production[temp_bases4-1]
temp_bases4 = temp_bases4-1
temp_bases5 = bases
while temp_bases5 >0:
if base_shipyards[temp_bases5-1]>=ships_shipyard[ship_name]:
production_percentage = (base_production[temp_bases5-1]*1.0)/total_production
print "Build ",((total_cost*production_percentage)/ships_cost[ship_name]),ship_name,"at base ", temp_bases5
print "This will take",((total_cost*production_percentage)/(base_production[temp_bases5-1]*1.0))," hours, or ",((total_cost*production_percentage)/(base_production[temp_bases5-1]*1.0))*60," minutes."
print ""
else:
print "Base ",temp_bases5,"does not have suffecient shipyards."
temp_bases5 = temp_bases5-1
print ""
print ""
print ""
exit_choice = input("Press 1 to exit or 2 to do another calculation.")
if exit_choice == 1:
loop = 0