{"id":394,"date":"2017-06-18T22:00:49","date_gmt":"2017-06-18T22:00:49","guid":{"rendered":"https:\/\/www.triptera.com.au\/wordpress\/?p=394"},"modified":"2017-07-09T06:45:21","modified_gmt":"2017-07-09T06:45:21","slug":"python-in-minecraft-7-define-tunnel-route-in-function","status":"publish","type":"post","link":"https:\/\/www.triptera.com.au\/wordpress\/2017\/06\/18\/python-in-minecraft-7-define-tunnel-route-in-function\/","title":{"rendered":"Python in Minecraft 7 &#8211; define tunnel route in function"},"content":{"rendered":"<p>In the previous example the tunnel was horizontal so the value of y for the tunnel was always constant. To make the tunnel route more complicated without having duplicate code we define a function called tunnelvertical(eastposition). For any value of the east position (x value) this function will return the vertical position of the tunnel. <\/p>\n<pre class=\"brush: python; title: ; wrap-lines: false; notranslate\" title=\"\">def tunnelvertical(eastposition):\r\n    y = abs(100 - (eastposition - XMIN))\r\n    if y &lt; 10:\r\n        return 10\r\n    if y &gt; 72: \r\n        return 72\r\n    return y\r\n<\/pre>\n<p>XMIN is a constant we have previously defined in the code showing where the tunnel starts. The shape of this tunnel is horizontal at ground level, then slopes down until it reaches level 10, goes horizontal for a while, then slopes up until it reaches ground level again and finishes off horizontal.<\/p>\n<p>Here is the full code<\/p>\n<pre class=\"brush: python; title: ; wrap-lines: false; notranslate\" title=\"\">import mcpi.minecraft as minecraft\r\nimport mcpi.block as block\r\nmc=minecraft.Minecraft.create()\r\nXMIN = -400 # x value at one end of tunnel.\r\nXMAX = -200 # x value at other end of tunnel. Must be greater than XMIN\r\nGROUND = 72 # y position of tunnel at each end\r\nFLOOR = 10  # minimum value of y at bottom of tunnel\r\nZPOS = -222 # z position for full length of tunnel\r\nTAIL = 10   # length of horizontal at each end of tunnel\r\n\r\n# constants for tunnelvertical function\r\nkx = (XMAX + XMIN) \/ 2\r\nky = GROUND + TAIL - (XMAX - XMIN) \/ 2\r\n\r\n# tunnelvertical returns a vertical position of the tunnel\r\n# for each value of the x position (eastposition)\r\ndef tunnelvertical(eastposition):\r\n    y = abs(eastposition - kx) + ky\r\n    if y &lt; FLOOR:\r\n        return FLOOR\r\n    if y &gt; GROUND: \r\n        return GROUND\r\n    return y\r\n\r\n# set the z coordinate for the tunnel which doesn't change for the full length    \r\nz = ZPOS\r\n\r\n# Initial loop to create a route made of solid glass with a stone base \r\nfor x in range(XMIN,XMAX+1):\r\n    y = tunnelvertical(x)\r\n    mc.setBlocks(x,y,z-2,x,y+6,z+2,block.GLASS)\r\n    mc.setBlocks(x,y,z-1,x,y,z+1,block.STONE)\r\n\r\n# Second loop to convert solid glass into a tunnel\r\nfor x in range(XMIN+1,XMAX):\r\n    y = tunnelvertical(x)\r\n    # replace centre glass with air to make it a tunnel\r\n    mc.setBlocks(x,y+1,z-1,x,y+5,z+1,block.AIR)\r\n    # place a torch every 4 positions to light the tunnel\r\n    if x % 4 == 0:\r\n        mc.setBlock(x,y+1,z+1,block.TORCH.id,5)\r\n<\/pre>\n<p><a href=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/verticalroute.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.triptera.com.au\/wordpress\/wp-content\/uploads\/2017\/07\/verticalroute-300x169.png\" alt=\"\" width=\"300\" height=\"169\" class=\"alignnone size-medium wp-image-397\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous example the tunnel was horizontal so the value of y for the tunnel was always constant. To make the tunnel route more complicated without having duplicate code we define a function called tunnelvertical(eastposition). For any value of the east position (x value) this function will return the vertical position of the tunnel. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"categories":[13],"tags":[],"class_list":["post-394","post","type-post","status-publish","format-standard","hentry","category-coderdojo"],"_links":{"self":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/comments?post=394"}],"version-history":[{"count":6,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/394\/revisions"}],"predecessor-version":[{"id":406,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/posts\/394\/revisions\/406"}],"wp:attachment":[{"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/media?parent=394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/categories?post=394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.triptera.com.au\/wordpress\/wp-json\/wp\/v2\/tags?post=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}