A little fun with Tkinter
Tags: Python, Tkinter, Rpg

Logic trumps all

Under the hood , the program is really not that complicated, there are simple python scripts, one that handles the interface behavior using the tkinter module and a second “backend” script that creates the enemy a player classes to be used by the program , let´s take a look at the backend script first.

The program is run by a simple Character() class, this class deals with the main attributes of any given character , such as their features (strength, agility, inteligence etc.) and the basic methods of attack and defense as outlined below

The character class is , in fact, a mother class that allows to create enemy and player classes objects with ease. In this way , we can have 3 different player classes (Combat Operative, Tech Operative and Covert Operative) just by adjusting the base parameters of our Character class. Note that Enemy is just another instance of character with different stats.

The methods attack() and defend() are a simple based on the stats for each class. The attack method will favor the player classes with higher agility and and strength (the base damage is a function of the player strength),while the defense favors the constitution stat . The idea behind these rules is to create a balance between the different player classes and the enemy so is not too easy or to hard for them to beat each other.

The second script controls all the interface aspects by making use of tkinter. Here , we will find mirror images of the attack()(attack_press() and defend() (defend() press) functions but this time dealing with the events that occur after a button press.

The whole screen is handled through the use of label frames, neatly allowing the arrangement of the different consoles , the canvas to show the player and enemy graphics and the action buttons

Now let´s take a closer look at attack_press()

def attack_press():

enemy_init_health=enemy.health

damage = player.attack()

enemy_health=enemy.defend(damage)

absorbed_damage=(damage-(enemy_init_health-enemy_health))

Action_button=ttk.Button(player_control,text="Action",command=command_action)

Action_button.grid(column=0,row=0)

Action_button.configure(state="disabled")

Flee_button=ttk.Button(player_control,text="Flee")

Flee_button.grid(column=0,row=1)

Flee_button.configure(state="disabled")

Guard_button=ttk.Button(player_control,text="Guard")

Guard_button.grid(column=1,row=0)

Guard_button.configure(state="disabled")

Pass_button=ttk.Button(player_control,text="Pass",command=enemy_attack)

Pass_button.grid(column=1,row=1,sticky=tk.W)

player_message=ttk.Label(action_console,text="Player Attacks! deals %s damage"%damage)

player_message.grid(row=0,column=0)

enemy_hp=ttk.Label(enemy_stats,text="Hp: %s"%enemy_health)

enemy_hp.grid(row=0,column=0)

enemy_message=ttk.Label(enemy_console,text="Enemy absorbs %s damage"%absorbed_damage)

enemy_message.grid(column=0,row=1)

if enemy.health==0:

mBox.showinfo("Python Message Info Box","You´ve won, restart the simulator!!!")

In the first few lines, the Character attack() method is invoked and with it we will calculate the base damage dealt by the player to the enemy. The enemy will , in turn , invoke the defend() method to calculate how much damage , if any, is absorbed.

When the player is done attacking , it must hit the ‘Pass’ button , giving the enemy a change to attack (this occurs with the enemy_attack() method), while the enemy_attack() method also invokes the Character attack() and defend() methods, it must also reset the state of the buttons used by the player so that a new run can be performed. The player will constantly hit attack and pass until either the enemy or the player runs out of HP, at which point , the program throws a little alert

And that’s it . What´s fun about this program is that its concept can be developed for bigger simulations and with a polished UI it could make a fairly decent RPG. I find that, most of the time, the difficulty lies in balancing the game’s rules so maybe one could pick up some tabletop RPG’s rulebook and bring them to life in the computer. By using the python´s libraries , the development possibilities are varied and quite extensive indeed. I have more ideas for game development but , as usual, this takes some time as I mostly focus in Analytics and Artificial Intelligence, which are quite extensive topics in and of themeselves, however sometimes I take a break from the more academic stuff and wind up by writing up some development ideas in my notebook, and eventually I try to find the time to put them together.

Until the next one