-
#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_total = 0
-
u_strength_total = 0
-
-
-
def clear():
-
import os
-
if os.name == 'nt':
-
os.system('CLS') #Pass CLS to cmd
-
if os.name == 'posix':
-
os.system('clear') #Pass clear to terminal
-
-
def attack():
-
monName = _choice(monsterName)
-
print "You are attacking a %s" % monName
-
monIndex = monsterName.index(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_total += 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_total += 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(monMin, 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 "Insufficient 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 "Insufficient Funds."
-
elif ca == 'c':
-
if gp > 999:
-
gp = gp - 1000
-
maxhp = maxhp + 100
-
hp = hp + 100
-
print "Purchase successful."
-
else:
-
print "Insufficient Funds."
-
elif ca == 'd':
-
if gp > 3999:
-
gp = gp - 4000
-
stre = stre + 1
-
else:
-
print "Insufficient Funds."
-
elif ca == 'e':
-
if gp > 3999:
-
gp = gp - 1000
-
spd = spd + 1
-
else:
-
print "Insufficient 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.readline())
-
stre = int(load.readline())
-
spd = int(load.readline())
-
hp = int(load.readline())
-
maxhp = int(load.readline())
-
exp = int(load.readline())
-
maxexp = int(load.readline())
-
gp = int(load.readline())
-
sword = int(load.readline())
-
print "Load Successful."
-
load.close()
-
except ValueError: print "Load unsuccessful."
-
-
def save():
-
try:
-
saves.append(str(level))
-
saves.append(str(stre))
-
saves.append(str(spd))
-
saves.append(str(hp))
-
saves.append(str(maxhp))
-
saves.append(str(exp))
-
saves.append(str(maxexp))
-
saves.append(str(gp))
-
saves.append(str(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("Goodbye! Press [Enter]")
-
else:
-
print "Invalid Choice."
-
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?
3 2083 bartonc 6,596
Recognized Expert Expert
Thanks for the new thread.
OK: you've got all these globals: -
-
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_total = 0
-
u_strength_total = 0
Now, to read them in a function: - def attack():
-
global hp #, any others
-
monName = _choice(monsterName)
-
print "You are attacking a %s" % monName
-
monIndex = monsterName.index(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 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_total += 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_total += 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(monMin, 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."
That worked wonders! Thank you so much.
bartonc 6,596
Recognized Expert Expert
That worked wonders! Thank you so much.
Any time, really. It's my pleasure.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: rdsteph |
last post by:
Python411 is a series of podcasts about Python, aimed at hobbyists and
others who are learning Python. Each episode focuses on one aspect of
learning Python, or one kind of Python programming, and...
|
by: owen |
last post by:
Generally speaking, what does it mean when I see a "button" with red text
showing this message instead of the control I've dragged onto the web form
in Design View.? (But the page works fine at...
|
by: Pawan |
last post by:
Hi Guys,
I have this current assignment where I have to develop online forms for
local municipal authorities. I have to use adobe acrobat to create online
forms from PDFs (which I have never done...
|
by: Carlos Lozano |
last post by:
Hi,
What is the right way to create an OCX COM component. The component is
already registerred, but can't create an instance. I am using the reference
to the interop module created.
If I use...
|
by: LIN |
last post by:
Hello,
Greetings. I am creating a web site which will contain lot of articles. I
had been planning to create simple HTML page on the server everytime i
posted a article (eg. article12.html )....
| |
by: Patrick |
last post by:
I want to define a set of web-form templates in XML and render the equivalent
web-form with ASP.NET, then process any input server controls on the form.
Reading the XML file from Page_load is...
|
by: Ravi Ambros Wallau |
last post by:
Hi:
I've created a custom control - a grid that uses Infragistics to display
some filters, the grid itself, and some buttons.
Well, when using this control directly on WebForm, everything works...
|
by: Mats Lycken |
last post by:
Hi,
I'm creating a CMS that I would like to be plug-in based with different
plugins handling different kinds of content.
What I really want is to be able to load/unload plugins on the fly
without...
|
by: David Thielen |
last post by:
Hi;
My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a
subdirectory to display as part of the created page. However, the bitmap will
not display due to a security violation.
...
|
by: =?Utf-8?B?YmJn?= |
last post by:
Hi all,
I read somewhere "using kernel stuff in thread is not good.."
if ManualResetEvent object is created in thread but not actually used, will
it affect performance?
Bob
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |