473,473 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Creating an RPG

7 New Member
Expand|Select|Wrap|Line Numbers
  1. #Game by Todd Lunter
  2. from random import choice as _choice
  3. import random as r
  4.  
  5.  
  6. c = 'a'
  7. level = 1
  8. stre = 2
  9. spd = 2
  10. exp = 0
  11. maxexp = 60
  12. hp = 60
  13. maxhp = 60
  14. gp = 10000
  15. sword = 0
  16. tempHP = 0
  17. monsterName = ['Rat', 'Bat', 'Snake']
  18. monsterHP = [2, 5, 10]
  19. monsterExp = [15, 30, 50]
  20. monsterStr = [2, 1, 4]
  21. monsterSpd = [1, 3, 4]
  22. monsterMin = [1, 2, 4]
  23. monsterMax = [4, 3, 10]
  24. saves = []
  25. m_strength_total = 0
  26. u_strength_total = 0
  27.  
  28.  
  29. def clear():
  30.     import os
  31.     if os.name == 'nt':
  32.         os.system('CLS') #Pass CLS to cmd
  33.     if os.name == 'posix':
  34.         os.system('clear') #Pass clear to terminal
  35.  
  36. def attack():
  37.     monName = _choice(monsterName)
  38.     print "You are attacking a %s" % monName
  39.     monIndex = monsterName.index(monName)
  40.     monHP = monsterHP[monIndex]
  41.     monExp = monsterExp[monIndex]
  42.     monStr = monsterStr[monIndex]
  43.     monSpd = monsterSpd[monIndex]
  44.     monMin = monsterMin[monIndex]
  45.     monMax = monsterMax[monIndex]
  46.  
  47.     if sword == 1:
  48.         stre += 4
  49.         spd += 3
  50.     if global hp > 0:
  51.         number = 1
  52.         u_speed_rand = r.randint(50, 150)
  53.         userSpd = spd * u_speed_rand
  54.         m_speed_rand = r.randint(50, 150)
  55.         monSpd = monSpd * m_speed_rand        
  56.         while hp > 0 and monHP > 0:
  57.             u_roll = r.randint(1, userSpd)
  58.             m_roll = r.randint(1, monSpd)
  59.  
  60.             if u_roll > m_roll:
  61.                 u_strength = stre * r.randint(3, 7)
  62.                 u_strength_total += u_strength
  63.                 if u_strength > monHP: u_strength = monHP
  64.                 monHP -= u_strength
  65.                 if monHP < 0: monHP = 0
  66.                 print "[%d] You attack for %d (%d HP Left)" % (number, u_strength, monHP)
  67.             elif m_roll > u_roll:
  68.                 m_strength = monStr * r.randint(3, 7)
  69.                 m_strength_total += m_strength
  70.                 if m_strength > hp: m_strength = hp
  71.                 hp -= m_strength
  72.                 if hp < 0: hp = 0
  73.                 print "[%d] %s attack for %d (%d HP Left)" % (number, monName, m_strength, hp)
  74.  
  75.             number += 1
  76.  
  77.         if hp == 0:
  78.             print "Oh dear, you lost.  Better luck next time."
  79.         elif monHP == 0:
  80.             exp += monExp
  81.             gp += r.randint(monMin, monMax)
  82.             if exp > maxexp:
  83.                 exp -= maxexp
  84.                 maxexp = maxexp * 2 * r.randint(1,2)
  85.                 level += 1
  86.                 print "You gained a level!"
  87.             print "Congrats you have won!"
  88.     else:
  89.         print "You are dead."
  90.  
  91. def store():
  92.     print "_______________________________"
  93.     print "| [a] Sword: 500 GP           |"
  94.     print "| [b] HP Restore: 10 GP       |"
  95.     print "| [c] Max HP Upgrade: 1000 GP |"
  96.     print "| [d] +1 Strength: 4000 GP    |"
  97.     print "| [e] +1 Speed: 4000 GP       |"
  98.     print "|_____________________________|"
  99.     print ""
  100.     ca = raw_input("[a] [b] [c] [d] [e] ").lower()
  101.     clear()
  102.     if ca == 'a':
  103.         if sword != 1:
  104.             if gp > 499:
  105.                 gp = gp - 500
  106.                 sword = 1
  107.                 print "Purchase successful."
  108.             else:
  109.                 print "Insufficient Funds."
  110.         else:
  111.             print "Already own a sword."
  112.     elif ca == 'b':
  113.         if gp > 9:
  114.             tempHP = maxhp - hp
  115.             gp = gp - 10
  116.             if hp == maxhp:
  117.                 print "Purchase not needed."
  118.             elif tempHP > 49:
  119.                 hp = hp + 50
  120.                 tempHP = 0
  121.                 print "Purchase successful."
  122.             else:
  123.                 hp = maxhp
  124.                 tempHP = 0
  125.                 print "Purchase successful."
  126.         else:
  127.             print "Insufficient Funds."
  128.     elif ca == 'c':
  129.         if gp > 999:
  130.             gp = gp - 1000
  131.             maxhp = maxhp + 100
  132.             hp = hp + 100
  133.             print "Purchase successful."
  134.         else:
  135.             print "Insufficient Funds."
  136.     elif ca == 'd':
  137.         if gp > 3999:
  138.             gp = gp - 4000
  139.             stre = stre + 1
  140.         else:
  141.             print "Insufficient Funds."
  142.     elif ca == 'e':
  143.         if gp > 3999:
  144.             gp = gp - 1000
  145.             spd = spd + 1
  146.         else:
  147.             print "Insufficient Funds."
  148.     else:
  149.         print "Invalid Choice."
  150.  
  151. def stats():
  152.     print "__________________"
  153.     print "| Level: %d" % level
  154.     print "| Strength: %d" % stre
  155.     print "| Speed: %d" % spd
  156.     print "| HP: %d/%d" % (hp, maxhp)
  157.     print "| Experience: %d/%d" % (exp, maxexp)
  158.     print "| GP: %d" % gp
  159.     if sword == 1:
  160.         print "| Sword: Yes"
  161.     else:
  162.         print "| Sword: No"
  163.     print "__________________"
  164.     print ""
  165.  
  166. def load():
  167.     try:
  168.         load = open('gamesave.txt', 'r+')
  169.         level = int(load.readline())
  170.         stre = int(load.readline())
  171.         spd = int(load.readline())
  172.         hp = int(load.readline())
  173.         maxhp = int(load.readline())
  174.         exp = int(load.readline())
  175.         maxexp = int(load.readline())
  176.         gp = int(load.readline())
  177.         sword = int(load.readline())
  178.         print "Load Successful."
  179.         load.close()
  180.     except ValueError: print "Load unsuccessful."
  181.  
  182. def save():
  183.     try:
  184.         saves.append(str(level))
  185.         saves.append(str(stre))
  186.         saves.append(str(spd))
  187.         saves.append(str(hp))
  188.         saves.append(str(maxhp))
  189.         saves.append(str(exp))
  190.         saves.append(str(maxexp))
  191.         saves.append(str(gp))
  192.         saves.append(str(sword))
  193.         save = open('gamesave.txt', 'r+')
  194.         for s in saves[:]:
  195.             save.write(s)
  196.             save.write('\n')
  197.             saves.remove(s)
  198.         print "Save successful."
  199.         print saves
  200.         save.close()
  201.     except ValueError: print "Save unsuccessful."
  202.  
  203. while c != 'q':
  204.     c = raw_input("[A]ttack [S]tore S[t]ats [L]oad Sa[v]e [Q]uit ").lower()
  205.     clear()
  206.     if c == 'a':
  207.         attack()
  208.     elif c == 's':
  209.         store()
  210.     elif c == 't':
  211.         stats()
  212.     elif c == 'l':
  213.         load()
  214.     elif c == 'v':
  215.         save()
  216.     else:
  217.         if c == 'q':
  218.             gb = raw_input("Goodbye! Press [Enter]")
  219.         else:
  220.             print "Invalid Choice."
  221.  
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?
Jul 12 '07 #1
3 2083
bartonc
6,596 Recognized Expert Expert
Thanks for the new thread.

OK: you've got all these globals:
Expand|Select|Wrap|Line Numbers
  1.  
  2. c = 'a'
  3. level = 1
  4. stre = 2
  5. spd = 2
  6. exp = 0
  7. maxexp = 60
  8. hp = 60
  9. maxhp = 60
  10. gp = 10000
  11. sword = 0
  12. tempHP = 0
  13. monsterName = ['Rat', 'Bat', 'Snake']
  14. monsterHP = [2, 5, 10]
  15. monsterExp = [15, 30, 50]
  16. monsterStr = [2, 1, 4]
  17. monsterSpd = [1, 3, 4]
  18. monsterMin = [1, 2, 4]
  19. monsterMax = [4, 3, 10]
  20. saves = []
  21. m_strength_total = 0
  22. u_strength_total = 0
Now, to read them in a function:
Expand|Select|Wrap|Line Numbers
  1. def attack():
  2.     global hp   #, any others
  3.     monName = _choice(monsterName)
  4.     print "You are attacking a %s" % monName
  5.     monIndex = monsterName.index(monName)
  6.     monHP = monsterHP[monIndex]
  7.     monExp = monsterExp[monIndex]
  8.     monStr = monsterStr[monIndex]
  9.     monSpd = monsterSpd[monIndex]
  10.     monMin = monsterMin[monIndex]
  11.     monMax = monsterMax[monIndex]
  12.  
  13.     if sword == 1:
  14.         stre += 4
  15.         spd += 3
  16.     if hp > 0:
  17.         number = 1
  18.         u_speed_rand = r.randint(50, 150)
  19.         userSpd = spd * u_speed_rand
  20.         m_speed_rand = r.randint(50, 150)
  21.         monSpd = monSpd * m_speed_rand        
  22.         while hp > 0 and monHP > 0:
  23.             u_roll = r.randint(1, userSpd)
  24.             m_roll = r.randint(1, monSpd)
  25.  
  26.             if u_roll > m_roll:
  27.                 u_strength = stre * r.randint(3, 7)
  28.                 u_strength_total += u_strength
  29.                 if u_strength > monHP: u_strength = monHP
  30.                 monHP -= u_strength
  31.                 if monHP < 0: monHP = 0
  32.                 print "[%d] You attack for %d (%d HP Left)" % (number, u_strength, monHP)
  33.             elif m_roll > u_roll:
  34.                 m_strength = monStr * r.randint(3, 7)
  35.                 m_strength_total += m_strength
  36.                 if m_strength > hp: m_strength = hp
  37.                 hp -= m_strength
  38.                 if hp < 0: hp = 0
  39.                 print "[%d] %s attack for %d (%d HP Left)" % (number, monName, m_strength, hp)
  40.  
  41.             number += 1
  42.  
  43.         if hp == 0:
  44.             print "Oh dear, you lost.  Better luck next time."
  45.         elif monHP == 0:
  46.             exp += monExp
  47.             gp += r.randint(monMin, monMax)
  48.             if exp > maxexp:
  49.                 exp -= maxexp
  50.                 maxexp = maxexp * 2 * r.randint(1,2)
  51.                 level += 1
  52.                 print "You gained a level!"
  53.             print "Congrats you have won!"
  54.     else:
  55.         print "You are dead."
Jul 12 '07 #2
xZaft
7 New Member
That worked wonders! Thank you so much.
Jul 12 '07 #3
bartonc
6,596 Recognized Expert Expert
That worked wonders! Thank you so much.
Any time, really. It's my pleasure.
Jul 12 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
6
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...
2
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...
15
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...
2
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 )....
2
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...
0
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...
12
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...
15
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. ...
9
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
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
agi2029
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,...
1
isladogs
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.