Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # File: rpg.py
- # This is an RPG whereby you click a button
- # and that makes you fight monsters
- # which in turn gives you XP
- # which causes you to level up.
- #
- # Pretty much the premise of the game.
- # My first Python app.
- #
- # @author Benjamin Jones
- #
- # PS: I totally don't know how to document Python code
- from Tkinter import *
- import random
- class App:
- def __init__(self, master):
- # Creates frame
- frame = Frame(master)
- frame.pack()
- # Creates Fight button
- self.button = Button(frame, text="Fight!", fg="red", command=self.fight)
- self.button.pack(fill=Y, side=LEFT)
- # Creates lvl variable and label
- global lvl
- lvl = StringVar()
- Label(master, textvariable=lvl).pack()
- lvl.set("Level 1")
- # Creates XP variable and label
- global xp
- xp = StringVar()
- Label(master, textvariable=xp).pack()
- xp.set("XP: 0")
- # Creates event variable
- global event
- event = StringVar()
- Label(master, textvariable=event).pack()
- event.set("")
- def fight(self):
- # Generate kill message
- event_msg = "Killed a "
- # Determines what monster you killed
- rand = random.randrange(1,10)
- if rand == 1:
- event_msg += "spider!"
- if rand == 2:
- event_msg += "wasp!"
- if rand == 3:
- event_msg += "Barbarian!"
- if rand == 4:
- event_msg += "soldier!"
- if rand == 5:
- event_msg += "thief!"
- if rand == 6:
- event_msg += "wizard!"
- if rand == 7:
- event_msg += "cyclops!"
- if rand == 8:
- event_msg += "dragon!"
- if rand == 9:
- event_msg += "ghost!"
- # Parses xp string for actual value
- xp_split = xp.get().split(":")
- current_xp = xp_split[1]
- del xp_split
- # Gain xp
- xp_gained = random.randrange(1,5)
- xp.set("XP: " + str(int(current_xp)+xp_gained))
- event_msg += " +%d XP" % xp_gained
- # Parses lvl string for actual value
- lvl_split = lvl.get().split (" ")
- current_lvl = lvl_split[1]
- del lvl_split
- # Checks if you level up
- if int(current_xp)+xp_gained >= int(current_lvl)**3:
- lvl.set("Level " + str(int(current_lvl)+1))
- event_msg += "... Level up!"
- # Displays event message
- event.set(event_msg)
- # Change location
- root = Tk()
- app = App(root)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement