{"id":551,"date":"2020-08-31T15:45:29","date_gmt":"2020-08-31T15:45:29","guid":{"rendered":"http:\/\/mipython.magwebdesigns.net\/WP\/?p=551"},"modified":"2020-08-31T15:45:41","modified_gmt":"2020-08-31T15:45:41","slug":"journey-with-the-python-text-adventure-empire-fable","status":"publish","type":"post","link":"http:\/\/mipython.magwebdesigns.net\/WP\/2020\/08\/31\/journey-with-the-python-text-adventure-empire-fable\/","title":{"rendered":"Journey With the Python Text Adventure Empire Fable"},"content":{"rendered":"\n<p>PLAY THE WORK IN PROGRESS GAME  <a href=\"https:\/\/PYTHONTEXTADVENTURE.mandrews85.repl.run\">EMPIRE FABLE<\/a> HERE:<\/p>\n\n\n\n<p><a href=\"https:\/\/PYTHONTEXTADVENTURE.mandrews85.repl.run\">https:\/\/PYTHONTEXTADVENTURE.mandrews85.repl.run<\/a><\/p>\n\n\n\n<p>Hey everybody, I&#8217;ve seen some questions lately.  I would like to share my methods (pun intended) that helped me better understand what I want to do with my programs .  <\/p>\n\n\n\n<p>I think Python is an excellent language for a text adventure.  If I want to scale it up later with graphics and menus.  Tkinter is an easy option or of course Pygame.  For this project though I wanted to stick with pure Python.  I didn&#8217;t want to have a bunch of dependencies. I want to make my own text adventure frame work eventually. <\/p>\n\n\n\n<p>A cool Ascii font for a text adventure in Python is the Efti Wall font here:<\/p>\n\n\n\n<p><a href=\"http:\/\/patorjk.com\/software\/taag\/#p=display&amp;h=2&amp;v=3&amp;f=Efti%20Wall&amp;t=H%0A\">http:\/\/patorjk.com\/software\/taag\/#p=display&amp;h=2&amp;v=3&amp;f=Efti%20Wall&amp;t=H%0A<\/a><\/p>\n\n\n\n<p>I wanted to do Ascii for the room, character and player &#8220;avatars&#8221;.  I&#8217;m calling them &#8220;portraits&#8221;. Eventually it would be cool to have ascii &#8220;images&#8221; for the items too.<\/p>\n\n\n\n<p>I was always into D&amp;D (Dungeons and Dragons)  and was fascinated with text adventures back in the day.   So the next logical step for me would be to write a simulation that encapsulates the basic algorithm from D&amp;D that Gary Gygax developed.  I&#8217;ve simplified the THAC0 system a little bit but a more faithful reproduction would be cool to do.   <\/p>\n\n\n\n<p>I&#8217;ve refactored this many many times now looking for the simplest, logical way to execute this.  For player movement.  I&#8217;ve come up with a &#8220;grid&#8221; method. The grid method passes the player in with a list of &#8220;rooms&#8221;.  The rooms are the areas of the game.  Rooms are stored as objects in  a 1 dimensional array. Each room has an x and y positional attribute.   The grid method loops through the list of rooms.  If the players position X and position Y attributes = the rooms position x and position y attributes.  The player see&#8217;s that rooms status.  I&#8217;m always looking for more efficient ways of doing things though. <\/p>\n\n\n\n<p>Let&#8217;s get to the code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>GAME CLASS<\/strong>:<\/h2>\n\n\n\n<p>Below, we initialize the game class.  The other objects, Player, Rooms, Characters and Items are imported into the &#8220;main.py&#8221; file.   With the grid method  we are passing the player object in with a list of rooms.  We then loop through the rooms.  If the players pos x and pos y = any room pos x and pos y,  that rooms status is printed.  If enemies are in the room.enemies list the game.battle_options() method is triggered.  Enemies position x and position y = the rooms position x and position y.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/>34<br \/>35<br \/>36<br \/>37<br \/>38<br \/>39<br \/>40<br \/>41<br \/>42<br \/>43<br \/>44<br \/>45<br \/>46<br \/>47<br \/>48<br \/>49<br \/>50<br \/>51<br \/>52<br \/>53<br \/>54<br \/>55<br \/>56<br \/>57<br \/>58<br \/>59<br \/>60<br \/>61<br \/>62<br \/>63<br \/>64<br \/>65<br \/>66<br \/><\/div><\/td><td><div class=\"text codecolorer\">from Player import *<br \/>\nfrom Room import *<br \/>\nfrom Character import *<br \/>\nfrom Item import *<br \/>\n<br \/>\nclass Game():<br \/>\n&nbsp; def __init__(self):<br \/>\n&nbsp; &nbsp; self.name = &quot;GAME&quot;<br \/>\n<br \/>\n&nbsp; def status(self):<br \/>\n&nbsp; &nbsp; print(self.name + &quot; IS INITIALIZED&quot;)<br \/>\n<br \/>\n<br \/>\n&nbsp; def grid(self,player):<br \/>\n<br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; print(&quot;INITIALIZE GRID&quot;)<br \/>\n&nbsp; &nbsp; rooms = &amp;#91;start_room]<br \/>\n<br \/>\n&nbsp; &nbsp; for room in rooms:<br \/>\n&nbsp; &nbsp; &nbsp; if player.pos_x == room.pos_x and player.pos_y == room.pos_y:<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; room.status()<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; for enemy in room.enemies:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enemy.pos_x = room.pos_x<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; enemy.pos_y = room.pos_y<br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if enemy.pos_x == player.pos_x and enemy.pos_y == player.pos_y:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while enemy.hp &gt;= 0:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.battle_options(player,enemy)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n<br \/>\n&nbsp; &nbsp; ### &nbsp; PLAYER OPTIONS ON TEH GRID<br \/>\n&nbsp; &nbsp; self.options(player)<br \/>\n&nbsp;<br \/>\n<br \/>\n&nbsp; def options(self,player):<br \/>\n&nbsp; &nbsp; print(&quot;MOVE: (N), (E), (S), (W)&quot;)<br \/>\n&nbsp; &nbsp; ### &nbsp; PLAYER USER INPUT <br \/>\n&nbsp; &nbsp; user_input = input(&quot; &gt;&gt;&gt; &quot;).upper()<br \/>\n<br \/>\n&nbsp; &nbsp; if user_input == &quot;N&quot;:<br \/>\n&nbsp; &nbsp; &nbsp; player.pos_y += 1<br \/>\n&nbsp; &nbsp; elif user_input == &quot;E&quot;:<br \/>\n&nbsp; &nbsp; &nbsp; player.pos_x += 1<br \/>\n&nbsp; &nbsp; elif user_input == &quot;S&quot;:<br \/>\n&nbsp; &nbsp; &nbsp; player.pos_y -= 1<br \/>\n&nbsp; &nbsp; elif user_input == &quot;W&quot;:<br \/>\n&nbsp; &nbsp; &nbsp; player.pos_x -= 1<br \/>\n&nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; print(&quot;INVALID CHOICE&quot;)<br \/>\n&nbsp; &nbsp; return<br \/>\n<br \/>\n&nbsp; def battle_options(self,attacker,defender):<br \/>\n<br \/>\n&nbsp; &nbsp; ### &nbsp;TO HIT ROLL PHASE<br \/>\n&nbsp; <br \/>\n&nbsp; &nbsp; attacker_roll = attacker.roll_die(100)<br \/>\n&nbsp; &nbsp; defender_roll = defender.roll_die(100)<br \/>\n<br \/>\n&nbsp; &nbsp; if attacker_roll &gt;= defender_roll:<br \/>\n&nbsp; &nbsp; &nbsp; print(attacker.name + &quot; HITS &quot; + defender.name)<br \/>\n&nbsp; &nbsp; &nbsp; defender.hp -= attacker.dmg<br \/>\n&nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; print(attacker.name + &quot; MISSES &quot; + defender.name)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>Now lets create our objects that will appear in the game.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text blackboard\" style=\"overflow:auto;white-space:nowrap;width:800px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"text codecolorer\">&nbsp;<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>[quote]<\/p>\n\n\n\n<p>[code]<\/p>\n\n\n\n<p>[\/code]<\/p>\n\n\n\n<p>[\/quote]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PLAY THE WORK IN PROGRESS GAME EMPIRE FABLE HERE: https:\/\/PYTHONTEXTADVENTURE.mandrews85.repl.run Hey everybody, I&#8217;ve seen some questions lately. I would like to share my methods (pun intended) that helped me better understand what I want to do with my programs . I think Python is an excellent language for a text adventure. If I want to scale it up later with graphics and menus. Tkinter is an easy option or of course Pygame. For this project though I wanted to stick with pure Python. I didn&#8217;t want to have a bunch&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-551","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":2,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":553,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/posts\/551\/revisions\/553"}],"wp:attachment":[{"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mipython.magwebdesigns.net\/WP\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}