[code=python]
#Game by Todd Lunter
from random import choice as _choice
import random as r
c = 'a'
level = 1
stre = 2
spd = 2
exp = 0
maxexp = 60
hp = 60
maxhp = 60
gp = 10000
sword = 0
tempHP = 0
monsterName = ['Rat', 'Bat', 'Snake']
monsterHP = [2, 5, 10]
monsterExp = [15, 30, 50]
monsterStr = [2, 1, 4]
monsterSpd = [1, 3, 4]
monsterMin = [1, 2, 4]
monsterMax = [4, 3, 10]
saves = []
m_strength_tota l = 0
u_strength_tota l = 0
def clear():
import os
if os.name == 'nt':
os.system('CLS' ) #Pass CLS to cmd
if os.name == 'posix':
os.system('clea r') #Pass clear to terminal
def attack():
monName = _choice(monster Name)
print "You are attacking a %s" % monName
monIndex = monsterName.ind ex(monName)
monHP = monsterHP[monIndex]
monExp = monsterExp[monIndex]
monStr = monsterStr[monIndex]
monSpd = monsterSpd[monIndex]
monMin = monsterMin[monIndex]
monMax = monsterMax[monIndex]
if sword == 1:
stre += 4
spd += 3
if global hp > 0:
number = 1
u_speed_rand = r.randint(50, 150)
userSpd = spd * u_speed_rand
m_speed_rand = r.randint(50, 150)
monSpd = monSpd * m_speed_rand
while hp > 0 and monHP > 0:
u_roll = r.randint(1, userSpd)
m_roll = r.randint(1, monSpd)
if u_roll > m_roll:
u_strength = stre * r.randint(3, 7)
u_strength_tota l += u_strength
if u_strength > monHP: u_strength = monHP
monHP -= u_strength
if monHP < 0: monHP = 0
print "[%d] You attack for %d (%d HP Left)" % (number, u_strength, monHP)
elif m_roll > u_roll:
m_strength = monStr * r.randint(3, 7)
m_strength_tota l += m_strength
if m_strength > hp: m_strength = hp
hp -= m_strength
if hp < 0: hp = 0
print "[%d] %s attack for %d (%d HP Left)" % (number, monName, m_strength, hp)
number += 1
if hp == 0:
print "Oh dear, you lost. Better luck next time."
elif monHP == 0:
exp += monExp
gp += r.randint(monMi n, monMax)
if exp > maxexp:
exp -= maxexp
maxexp = maxexp * 2 * r.randint(1,2)
level += 1
print "You gained a level!"
print "Congrats you have won!"
else:
print "You are dead."
def store():
print "______________ _______________ __"
print "| [a] Sword: 500 GP |"
print "| [b] HP Restore: 10 GP |"
print "| [c] Max HP Upgrade: 1000 GP |"
print "| [d] +1 Strength: 4000 GP |"
print "| [e] +1 Speed: 4000 GP |"
print "|_____________ _______________ _|"
print ""
ca = raw_input("[a] [b] [c] [d] [e] ").lower()
clear()
if ca == 'a':
if sword != 1:
if gp > 499:
gp = gp - 500
sword = 1
print "Purchase successful."
else:
print "Insufficie nt Funds."
else:
print "Already own a sword."
elif ca == 'b':
if gp > 9:
tempHP = maxhp - hp
gp = gp - 10
if hp == maxhp:
print "Purchase not needed."
elif tempHP > 49:
hp = hp + 50
tempHP = 0
print "Purchase successful."
else:
hp = maxhp
tempHP = 0
print "Purchase successful."
else:
print "Insufficie nt Funds."
elif ca == 'c':
if gp > 999:
gp = gp - 1000
maxhp = maxhp + 100
hp = hp + 100
print "Purchase successful."
else:
print "Insufficie nt Funds."
elif ca == 'd':
if gp > 3999:
gp = gp - 4000
stre = stre + 1
else:
print "Insufficie nt Funds."
elif ca == 'e':
if gp > 3999:
gp = gp - 1000
spd = spd + 1
else:
print "Insufficie nt Funds."
else:
print "Invalid Choice."
def stats():
print "______________ ____"
print "| Level: %d" % level
print "| Strength: %d" % stre
print "| Speed: %d" % spd
print "| HP: %d/%d" % (hp, maxhp)
print "| Experience: %d/%d" % (exp, maxexp)
print "| GP: %d" % gp
if sword == 1:
print "| Sword: Yes"
else:
print "| Sword: No"
print "______________ ____"
print ""
def load():
try:
load = open('gamesave. txt', 'r+')
level = int(load.readli ne())
stre = int(load.readli ne())
spd = int(load.readli ne())
hp = int(load.readli ne())
maxhp = int(load.readli ne())
exp = int(load.readli ne())
maxexp = int(load.readli ne())
gp = int(load.readli ne())
sword = int(load.readli ne())
print "Load Successful."
load.close()
except ValueError: print "Load unsuccessful."
def save():
try:
saves.append(st r(level))
saves.append(st r(stre))
saves.append(st r(spd))
saves.append(st r(hp))
saves.append(st r(maxhp))
saves.append(st r(exp))
saves.append(st r(maxexp))
saves.append(st r(gp))
saves.append(st r(sword))
save = open('gamesave. txt', 'r+')
for s in saves[:]:
save.write(s)
save.write('\n' )
saves.remove(s)
print "Save successful."
print saves
save.close()
except ValueError: print "Save unsuccessful."
while c != 'q':
c = raw_input("[A]ttack [S]tore S[t]ats [L]oad Sa[v]e [Q]uit ").lower()
clear()
if c == 'a':
attack()
elif c == 's':
store()
elif c == 't':
stats()
elif c == 'l':
load()
elif c == 'v':
save()
else:
if c == 'q':
gb = raw_input("Good bye! Press [Enter]")
else:
print "Invalid Choice."
[/code]
Here is my code as it is to date. The problems I have at the moment, are it says the variables like HP, when they are requested in the attack() function are not defined before use, but they are defined, actually they are the first thing defined.
Any ideas as to why this might be happening?
#Game by Todd Lunter
from random import choice as _choice
import random as r
c = 'a'
level = 1
stre = 2
spd = 2
exp = 0
maxexp = 60
hp = 60
maxhp = 60
gp = 10000
sword = 0
tempHP = 0
monsterName = ['Rat', 'Bat', 'Snake']
monsterHP = [2, 5, 10]
monsterExp = [15, 30, 50]
monsterStr = [2, 1, 4]
monsterSpd = [1, 3, 4]
monsterMin = [1, 2, 4]
monsterMax = [4, 3, 10]
saves = []
m_strength_tota l = 0
u_strength_tota l = 0
def clear():
import os
if os.name == 'nt':
os.system('CLS' ) #Pass CLS to cmd
if os.name == 'posix':
os.system('clea r') #Pass clear to terminal
def attack():
monName = _choice(monster Name)
print "You are attacking a %s" % monName
monIndex = monsterName.ind ex(monName)
monHP = monsterHP[monIndex]
monExp = monsterExp[monIndex]
monStr = monsterStr[monIndex]
monSpd = monsterSpd[monIndex]
monMin = monsterMin[monIndex]
monMax = monsterMax[monIndex]
if sword == 1:
stre += 4
spd += 3
if global hp > 0:
number = 1
u_speed_rand = r.randint(50, 150)
userSpd = spd * u_speed_rand
m_speed_rand = r.randint(50, 150)
monSpd = monSpd * m_speed_rand
while hp > 0 and monHP > 0:
u_roll = r.randint(1, userSpd)
m_roll = r.randint(1, monSpd)
if u_roll > m_roll:
u_strength = stre * r.randint(3, 7)
u_strength_tota l += u_strength
if u_strength > monHP: u_strength = monHP
monHP -= u_strength
if monHP < 0: monHP = 0
print "[%d] You attack for %d (%d HP Left)" % (number, u_strength, monHP)
elif m_roll > u_roll:
m_strength = monStr * r.randint(3, 7)
m_strength_tota l += m_strength
if m_strength > hp: m_strength = hp
hp -= m_strength
if hp < 0: hp = 0
print "[%d] %s attack for %d (%d HP Left)" % (number, monName, m_strength, hp)
number += 1
if hp == 0:
print "Oh dear, you lost. Better luck next time."
elif monHP == 0:
exp += monExp
gp += r.randint(monMi n, monMax)
if exp > maxexp:
exp -= maxexp
maxexp = maxexp * 2 * r.randint(1,2)
level += 1
print "You gained a level!"
print "Congrats you have won!"
else:
print "You are dead."
def store():
print "______________ _______________ __"
print "| [a] Sword: 500 GP |"
print "| [b] HP Restore: 10 GP |"
print "| [c] Max HP Upgrade: 1000 GP |"
print "| [d] +1 Strength: 4000 GP |"
print "| [e] +1 Speed: 4000 GP |"
print "|_____________ _______________ _|"
print ""
ca = raw_input("[a] [b] [c] [d] [e] ").lower()
clear()
if ca == 'a':
if sword != 1:
if gp > 499:
gp = gp - 500
sword = 1
print "Purchase successful."
else:
print "Insufficie nt Funds."
else:
print "Already own a sword."
elif ca == 'b':
if gp > 9:
tempHP = maxhp - hp
gp = gp - 10
if hp == maxhp:
print "Purchase not needed."
elif tempHP > 49:
hp = hp + 50
tempHP = 0
print "Purchase successful."
else:
hp = maxhp
tempHP = 0
print "Purchase successful."
else:
print "Insufficie nt Funds."
elif ca == 'c':
if gp > 999:
gp = gp - 1000
maxhp = maxhp + 100
hp = hp + 100
print "Purchase successful."
else:
print "Insufficie nt Funds."
elif ca == 'd':
if gp > 3999:
gp = gp - 4000
stre = stre + 1
else:
print "Insufficie nt Funds."
elif ca == 'e':
if gp > 3999:
gp = gp - 1000
spd = spd + 1
else:
print "Insufficie nt Funds."
else:
print "Invalid Choice."
def stats():
print "______________ ____"
print "| Level: %d" % level
print "| Strength: %d" % stre
print "| Speed: %d" % spd
print "| HP: %d/%d" % (hp, maxhp)
print "| Experience: %d/%d" % (exp, maxexp)
print "| GP: %d" % gp
if sword == 1:
print "| Sword: Yes"
else:
print "| Sword: No"
print "______________ ____"
print ""
def load():
try:
load = open('gamesave. txt', 'r+')
level = int(load.readli ne())
stre = int(load.readli ne())
spd = int(load.readli ne())
hp = int(load.readli ne())
maxhp = int(load.readli ne())
exp = int(load.readli ne())
maxexp = int(load.readli ne())
gp = int(load.readli ne())
sword = int(load.readli ne())
print "Load Successful."
load.close()
except ValueError: print "Load unsuccessful."
def save():
try:
saves.append(st r(level))
saves.append(st r(stre))
saves.append(st r(spd))
saves.append(st r(hp))
saves.append(st r(maxhp))
saves.append(st r(exp))
saves.append(st r(maxexp))
saves.append(st r(gp))
saves.append(st r(sword))
save = open('gamesave. txt', 'r+')
for s in saves[:]:
save.write(s)
save.write('\n' )
saves.remove(s)
print "Save successful."
print saves
save.close()
except ValueError: print "Save unsuccessful."
while c != 'q':
c = raw_input("[A]ttack [S]tore S[t]ats [L]oad Sa[v]e [Q]uit ").lower()
clear()
if c == 'a':
attack()
elif c == 's':
store()
elif c == 't':
stats()
elif c == 'l':
load()
elif c == 'v':
save()
else:
if c == 'q':
gb = raw_input("Good bye! Press [Enter]")
else:
print "Invalid Choice."
[/code]
Here is my code as it is to date. The problems I have at the moment, are it says the variables like HP, when they are requested in the attack() function are not defined before use, but they are defined, actually they are the first thing defined.
Any ideas as to why this might be happening?
Comment