Advertisement
Guest User

Untitled

a guest
Aug 7th, 2011
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # File: rpg.py
  4. # This is an RPG whereby you click a button
  5. # and that makes you fight monsters
  6. # which in turn gives you XP
  7. # which causes you to level up.
  8. #
  9. # Pretty much the premise of the game.
  10. # My first Python app.
  11. #
  12. # @author Benjamin Jones
  13. #
  14. # PS: I totally don't know how to document Python code
  15.  
  16. from Tkinter import *
  17. import random
  18.  
  19. class App:
  20.  
  21.     def __init__(self, master):
  22.  
  23.         # Creates frame
  24.         frame = Frame(master)
  25.         frame.pack()
  26.  
  27.         # Creates Fight button
  28.         self.button = Button(frame, text="Fight!", fg="red", command=self.fight)
  29.         self.button.pack(fill=Y, side=LEFT)
  30.  
  31.         # Creates lvl variable and label
  32.         global lvl
  33.         lvl = StringVar()
  34.         Label(master, textvariable=lvl).pack()
  35.         lvl.set("Level 1")
  36.  
  37.         # Creates XP variable and label
  38.         global xp
  39.         xp = StringVar()
  40.         Label(master, textvariable=xp).pack()
  41.         xp.set("XP: 0")
  42.  
  43.         # Creates event variable
  44.         global event
  45.         event = StringVar()
  46.         Label(master, textvariable=event).pack()
  47.         event.set("")
  48.  
  49.     def fight(self):
  50.  
  51.         # Generate kill message
  52.         event_msg = "Killed a "
  53.  
  54.         # Determines what monster you killed
  55.         rand = random.randrange(1,10)
  56.         if rand == 1:
  57.             event_msg += "spider!"
  58.         if rand == 2:
  59.             event_msg += "wasp!"
  60.         if rand == 3:
  61.             event_msg += "Barbarian!"
  62.         if rand == 4:
  63.             event_msg += "soldier!"
  64.         if rand == 5:
  65.             event_msg += "thief!"
  66.         if rand == 6:
  67.             event_msg += "wizard!"
  68.         if rand == 7:
  69.             event_msg += "cyclops!"
  70.         if rand == 8:
  71.             event_msg += "dragon!"
  72.         if rand == 9:
  73.             event_msg += "ghost!"
  74.  
  75.         # Parses xp string for actual value
  76.         xp_split = xp.get().split(":")
  77.         current_xp = xp_split[1]
  78.         del xp_split
  79.        
  80.         # Gain xp
  81.         xp_gained = random.randrange(1,5)
  82.         xp.set("XP: " + str(int(current_xp)+xp_gained))
  83.  
  84.         event_msg += " +%d XP" % xp_gained
  85.  
  86.         # Parses lvl string for actual value
  87.         lvl_split = lvl.get().split (" ")
  88.         current_lvl = lvl_split[1]
  89.         del lvl_split
  90.        
  91.         # Checks if you level up
  92.         if int(current_xp)+xp_gained >= int(current_lvl)**3:
  93.             lvl.set("Level " + str(int(current_lvl)+1))
  94.             event_msg += "... Level up!"
  95.  
  96.         # Displays event message
  97.         event.set(event_msg)
  98.  
  99.         # Change location
  100.  
  101. root = Tk()
  102.  
  103. app = App(root)
  104.  
  105. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement