http://roguebasin.com/api.php?action=feedcontributions&user=Bix&feedformat=atomRogueBasin - User contributions [en]2024-03-28T13:35:20ZUser contributionsMediaWiki 1.36.0http://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47822Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-11-15T14:59:47Z<p>Bix: /* Installing an Editor */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [https://code.visualstudio.com/ VS Code (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.7.0" (See: [https://www.python.org/dev/peps/pep-0569/ Release Schedule]).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.7.0". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Use pip<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) $ pip install tcod</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) $ python -c "import tcod"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47821Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-11-15T14:59:28Z<p>Bix: /* Installing an Editor */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [https://code.visualstudio.com/ VS Code]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.7.0" (See: [https://www.python.org/dev/peps/pep-0569/ Release Schedule]).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.7.0". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Use pip<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) $ pip install tcod</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) $ python -c "import tcod"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47820Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-11-15T14:58:24Z<p>Bix: /* Setup Mac */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.7.0" (See: [https://www.python.org/dev/peps/pep-0569/ Release Schedule]).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.7.0". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Use pip<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) $ pip install tcod</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) $ python -c "import tcod"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47819Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-11-15T14:57:39Z<p>Bix: /* Setup Mac */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.7.0" (See: [https://www.python.org/dev/peps/pep-0569/ Release Schedule]).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.7.0". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Use pip<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) $ pip install tcod</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) $ python -c "import tcod"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47818Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-11-15T14:56:23Z<p>Bix: /* Creating a virtual environment using vsh */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.7.0" (See: [https://www.python.org/dev/peps/pep-0569/ Release Schedule]).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.7.0". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Use pip<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ pip install tcod</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ python -c "import tcod"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47817Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-11-15T14:48:04Z<p>Bix: /* Setup Mac */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.7.0" (See: [https://www.python.org/dev/peps/pep-0569/ Release Schedule]).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Use pip<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ pip install tcod</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ python -c "import tcod"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47231Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-17T01:07:07Z<p>Bix: /* Setup Mac */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5" (After 15 June 2018, this will be 3.7).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Use pip<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ pip install tdl</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ python -c "import tcod"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47230Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-17T00:52:26Z<p>Bix: /* Installing libtcod */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. While there are quite a few ways to do this, the most straightforward way is to simply use the .dylib files already built by the libtcod team.<br />
<br />
==== Download libtcod ====<br />
<br />
libtcod now has builds ready for macos on its [https://bitbucket.org/libtcod/libtcod/downloads/ bitbucket source code website]. Simply download the latest macos file, unzip that file and copy the `SDL2.Framework` and `libtcod*.dylib` files into your project's folder.<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5" (After 15 June 2018, this will be 3.7).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47229Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-17T00:51:05Z<p>Bix: /* Build libtcod */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with autoconf tools<br />
* move the resulting library files ($HOME/repos/other/libtcod/build/autoconf/.libs/*) into a proper folder (/usr/local/lib)<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
==== Download libtcod ====<br />
<br />
libtcod now has builds ready for macos on its [https://bitbucket.org/libtcod/libtcod/downloads/ bitbucket source code website]. Simply download the latest macos file, unzip that file and copy the `SDL2.Framework` and `libtcod*.dylib` files into your project's folder.<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5" (After 15 June 2018, this will be 3.7).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod&diff=47223Complete Roguelike Tutorial, using python3+libtcod2018-06-10T19:36:21Z<p>Bix: /* Why Python? */</p>
<hr />
<div>__TOC__<br />
<br />
== '''Short introduction''' ==<br />
<br />
=== Welcome! ===<br />
<br />
Welcome to this tutorial! As you probably guessed, the goal is to have a one-stop-shop for all the info you need on how to build a good Roguelike from scratch. We hope you find it useful! But first, some quick Q&A.<br />
<br />
=== Why Python? ===<br />
<br />
Most people familiar with this language will tell you it's fun! Python aims to be simple but powerful, and very accessible to beginners. This tutorial would probably be much harder without it. We insist that you install/use Python 3.6 and go through at least the first parts of the [http://docs.python.org/tutorial/ Python 3 Tutorial]. This tutorial will be much easier if you've experimented with the language first. Remember that the [https://docs.python.org/3/library/index.html Python Library Reference] is your friend -- the standard library has everything you might need and when programming you should be ready to search it for help on any unknown function you might encounter.<br />
<br />
<center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C" width="60%"><tr><td><center><br />
This tutorial is for <b>Python 3 only</b>, and it is strongly recommended you use the latest Python 3.6 or higher release.<br />
<br />
If you choose to use earlier versions of Python, you may encounter problems you need to overcome.<br/><br />
</center></td></tr></table></center><br />
<br />
=== Why libtcod? ===<br />
<br />
If you haven't seen it in action yet, check out the [https://bitbucket.org/libtcod/libtcod/wiki/Features features] and [http://roguecentral.org/doryen/projects-2/ some projects] where it was used successfully. It's extremely easy to use and has tons of useful functions specific to RogueLikes (RLs).<br />
<br />
=== Alternative Tutorials and Libraries ===<br />
<br />
There are no known versions of this tutorial for other programming languages than Python, for libtcod 1.6.0.<br />
<br />
There is a [[Complete Roguelike Tutorial, using python+libtcod|Python 2]] version of this exact tutorial.<br />
<br />
[http://rogueliketutorials.com/ Here] you'll find completed ports, one for Python 3 and libtcod (revising this tutorial "with good coding practices kept in mind from the beginning") and another for Python 3 and TDL, created by [https://www.reddit.com/user/TStand90 /u/TStand90] for r/roguelikedev [https://www.reddit.com/r/roguelikedev/wiki/python_tutorial_series Tutorial Tuesday 2017].<br />
<br />
A separate work-in-progress port of this tutorial for Python 3 and tdl (a pythonic cffi port of libtcod) can be found [http://www.roguebasin.com/index.php?title=Roguelike_Tutorial,_using_python3%2Btdl here].<br />
<br />
==The complete guide to making your first Roguelike ==<br />
<br />
Follow the first link to get started!<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 0|Part 0: Setup your system]]'''<br />
*: Make sure your system is setup to begin writing code<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 1|Part 1: Graphics]]'''<br />
*: Start your game right away by setting up the screen, printing the stereotypical @ character and moving it around with the arrow keys.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 2|Part 2: The object and the map]]'''<br />
*: This introduces two new concepts: the generic object system that will be the basis for the whole game, and a general map object that you'll use to hold your dungeon.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 3|Part 3: The dungeon]]'''<br />
*: Learn how to code up a neat little dungeon generator.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 4|Part 4: Field-of-view and exploration]]'''<br />
*: Display the player's field-of-view (FOV) and explore the dungeon gradually (also known as fog-of-war).<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 5|Part 5: Preparing for combat]]'''<br />
*: Place some orcs and trolls around the dungeon (they won't stay there for long!). Also, deal with blocking objects and game states, which are important before coding the next part.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 6|Part 6: Going Berserk!]]'''<br />
*: Stalking monsters, fights, splatter -- need we say more?<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 7|Part 7: The GUI]]'''<br />
*: A juicy Graphical User Interface with status bars and a colored message log for maximum eye-candy. Also, the infamous "look" command, with a twist: you can use the mouse.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 8|Part 8: Items and Inventory]]'''<br />
*: The player gets to collect ("borrow") items from the dungeon and use them, with a neat inventory screen. More items added in the next part.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 9|Part 9: Spells and ranged combat]]'''<br />
*: The player's strategic choices increase exponentially as we add a few magic scrolls to the mix. Covers damage and mind spells, as well as ranged combat.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 10|Part 10: Main menu and saving]]'''<br />
*: A main menu complete with a background image and the ability to save and load the game.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 11|Part 11: Dungeon levels and character progression]]'''<br />
*: Let the player venture deeper into the dungeon and grow stronger, including experience gain, levels and raising stats!<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 12|Part 12: Monster and item progression]]'''<br />
*: Deeper dungeon levels become increasingly more difficult! Here we create tools for dealing with chances and making them vary with level.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, part 13|Part 13: Adventure gear]]'''<br />
*: Swords, shields and other equipment can now help the player by granting hefty bonuses. The bonus system can also be used for all kinds of magics and buffs!<br />
<br />
==Extras==<br />
<br />
Some stuff that is entirely optional and didn't make it in; check this out if you finished the tutorial and are looking for some modifications and improvements to your game -- some are easy, others are more advanced.<br />
<br />
<br />
* '''[[Complete Roguelike Tutorial, using Python3+libtcod, extras#A neat Python shortcut for Notepad++|A neat Python shortcut for Notepad++]]'''<br />
*: For Notepad++ users, how to set up a shortcut to help you debugging.<br />
<br />
* '''[[Complete Roguelike Tutorial, using Python3+libtcod, extras#Old-school wall and floor tiles|Old-school wall and floor tiles]]'''<br />
*: Using characters in tiles, without getting weird graphical glitches. This is actually very simple.<br />
<br />
* '''[[Complete Roguelike Tutorial, using Python3+libtcod, extras#Real-time combat|Real-time combat]]'''<br />
*: A speed system to change the tutorial's turn-based combat to real-time!<br />
<br />
* '''[[Complete Roguelike Tutorial, using Python3+libtcod, extras#Mouse-driven Menus|Mouse-driven menus]]'''<br />
*: Add basic mouse support to your menus!<br />
<br />
* '''[[Complete Roguelike Tutorial, using python3+libtcod, extras scrolling code|Scrolling maps]]'''<br />
*: Placeholder page for the scrolling map code. Tutorial text will be written soon.<br />
<br />
* '''[[Complete Roguelike Tutorial, using Python3+libtcod, extras#Creating a Binary|Creating a Binary]]'''<br />
*: Package and deliver your game the nice way!<br />
<br />
* '''[[Complete Roguelike Tutorial, using Python3+libtcod, extras#A* Pathfinding|A* Pathfinding]]'''<br />
*: A good pathfinding system<br />
<br />
* '''[[Complete Roguelike Tutorial, using Python3+libtcod, extras#Using Graphical Tiles|Using Graphical Tiles]]'''<br />
*: An alternative to solid colors or ASCII graphics<br />
<br />
* '''[[Complete Roguelike Tutorial, using Python3+libtcod, extras#BSP Dungeon Generator|BSP Dungeon Generator]]'''<br />
*: Binary Space Partitioning Dungeon Generator<br />
<br />
==Credits==<br />
<br />
Code and tutorial written by Jo&atilde;o F. Henriques (a.k.a. Jotaf). Thanks go out to George Oliver for helping with the layout, sections rearrangement, and syntax highlighting; Teddy Leach for his text reviews; and all the folks in the libtcod forums for their valuable feedback!<br />
<br />
The most active place to discuss this tutorial, or libtcod in general, is the [https://www.reddit.com/r/roguelikedev/ roguelikedev subreddit]. Post if you're stuck, to show your own project, or just to say hi. It's always cool to get some feedback on the tutorial, and hear about other roguelikes in development. Also, past discussions can either be found in the old [http://roguecentral.org/doryen/forum/index.php?board=20.0 libtcod/Python forum] or the old [http://roguecentral.org/doryen/forum/index.php?topic=328.0 forum on this tutorial].<br />
<br />
[[Category: Developing]]</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Windows&diff=47222Complete Roguelike Tutorial, using python3+libtcod, setup Windows2018-06-10T19:31:56Z<p>Bix: /* Validating the setup */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Windows =<br />
<br />
== Installing an Editor ==<br />
<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
* [https://code.visualstudio.com/downloads/ Visual Code (Editor)]<br />
* [https://notepad-plus-plus.org/download/v7.5.1.html Notepad ++ (Editor)]<br />
* [https://www.visualstudio.com/downloads/ Visual Studio (IDE)]<br />
<br />
== Installing libtcod ==<br />
<br />
Download the latest release of [https://bitbucket.org/libtcod/libtcod/downloads libtcod 1.6] and extract it somewhere. Be warned that both Python and libtcod must either be <b>both 32 bit</b>, or <b>both 64 bit</b>. If you get dll loading errors, getting this wrong is the most likely cause. libtcod will now (as of 1.6.2 and above) check they are compatible, and tell you if they are not.<br />
<br />
== Installing Python ==<br />
<br />
=== Python Executable ===<br />
If you haven't already done so, [http://www.python.org/download/ download and install Python 3.6]. Any version of Python 3, from 3.6 and above should be fine, but the latest update (of <i>Python 3</i>, not Python 2) is of course always preferable.<br />
<br />
There are reports that for older versions of libtcod on 64-bit Windows (Windows 7) that the 32 bit version is preferable, since the 64 bit version of Python seems to cause problems with libtcod. This was been tested on Windows 10 with libtcod 1.6, and was not reproducible, so it's up to you.<br />
<br />
It might be advisable to go with 32 bit just to keep things simple, and when you're gone through the tutorial look at switching to 64 bit if it matters to you to do so, and if you do encounter errors specifically related to this, report them on the [https://bitbucket.org/libtcod/libtcod/issues/ libtcod bug tracker].<br />
<br />
<br />
=== Validating Python with libtcod ===<br />
<br />
Open a command prompt (windows key + R: cmd)<br />
<br />
Run the following command:<br />
<br />
c:\> python -c "import libtcod"<br />
<br />
== Setting up your Project ==<br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
<br />
* \project\<br />
*: \libtcodpy\<br />
*: \tests\<br />
*: \firstrl.py<br />
<br />
The images below should help further qualify.<br />
<br />
=== Creating your project folder ===<br />
<br />
Now create your project's folder. Inside it, create an empty file with a name of your choice. We're using ''firstrl.py''. It'll make the tutorial easier to just use the same name, and you can always rename it later.<br />
<br />
[[File:Windows-01-162-project-folder-empty.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the relevant DLLs.<br />
<br />
[[File:Windows-02-162-dlls.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''libtcodpy'' folder.<br />
<br />
[[File:Windows-03-162-libtcodpy-folder.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''arial10x10.png'' font. If you choose a different one, you'll need to adjust how you do the tutorial accordingly. It's simpler to just use the same one as we do, and change it later, if it suits you to do so.<br />
<br />
[[File:Windows-04-162-font.png|center]]<br />
<br />
At this point, you should have a folder that looks like this:<br />
<br />
[[File:Windows-05-162-project-folder-ready.png|center]]<br />
<br />
== Validating the setup ==<br />
<br />
libtcod is compiled with the latest official release of Visual Studio. In an ideal world, it should just run, and if you have recently updated games installed from Steam or perhaps even some modern applications, it should. However, some people lack the support for that latest release of Visual Studio, and these steps help update your computer to allow libtcod to be run.<br />
<br />
<ol><br />
<li>Within the libtcod release you just extracted, you will see a file called <cite>samples.exe</cite>.</li><br />
<li>Run <cite>samples.exe</cite> and verify that it appears to work. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it just doesn't start and instead you see a message about <cite>vcruntime140.dll</cite> being required, then you need to install the correct Visual Studio 2017 runtime.</li><br />
<li>Go to the latest Microsoft Visual Studio [https://www.visualstudio.com/downloads/ runtime download page], and download and install the runtime that matches the Python and libtcod you are using. If you are using 32 bit Python and libtcod, this will be the x86 runtime. If you are using the 64 bit Python and libtcod, this will be the x64 runtime. (Tip: If you think you might switch from 32 bit to 64 bit in the future, hedge your bets and install both runtimes to prevent future problems.)</li><br />
<li>Once you've installed the correct runtime, run <cite>samples.exe</cite> and verify that it now appears to work. If it still gives the same message, you didn't install the correct runtime. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it is not verified as working for you, you can open a libtcod bug report [https://bitbucket.org/libtcod/libtcod/issues?status=new&status=open on BitBucket].<br />
</ol><br />
<br />
You've verified that libtcod is working? Great! You can move onto the next section.<br />
<br />
== Finishing touches ==<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Windows&diff=47221Complete Roguelike Tutorial, using python3+libtcod, setup Windows2018-06-10T19:30:01Z<p>Bix: </p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Windows =<br />
<br />
== Installing an Editor ==<br />
<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
* [https://code.visualstudio.com/downloads/ Visual Code (Editor)]<br />
* [https://notepad-plus-plus.org/download/v7.5.1.html Notepad ++ (Editor)]<br />
* [https://www.visualstudio.com/downloads/ Visual Studio (IDE)]<br />
<br />
== Installing libtcod ==<br />
<br />
Download the latest release of [https://bitbucket.org/libtcod/libtcod/downloads libtcod 1.6] and extract it somewhere. Be warned that both Python and libtcod must either be <b>both 32 bit</b>, or <b>both 64 bit</b>. If you get dll loading errors, getting this wrong is the most likely cause. libtcod will now (as of 1.6.2 and above) check they are compatible, and tell you if they are not.<br />
<br />
== Installing Python ==<br />
<br />
=== Python Executable ===<br />
If you haven't already done so, [http://www.python.org/download/ download and install Python 3.6]. Any version of Python 3, from 3.6 and above should be fine, but the latest update (of <i>Python 3</i>, not Python 2) is of course always preferable.<br />
<br />
There are reports that for older versions of libtcod on 64-bit Windows (Windows 7) that the 32 bit version is preferable, since the 64 bit version of Python seems to cause problems with libtcod. This was been tested on Windows 10 with libtcod 1.6, and was not reproducible, so it's up to you.<br />
<br />
It might be advisable to go with 32 bit just to keep things simple, and when you're gone through the tutorial look at switching to 64 bit if it matters to you to do so, and if you do encounter errors specifically related to this, report them on the [https://bitbucket.org/libtcod/libtcod/issues/ libtcod bug tracker].<br />
<br />
<br />
=== Validating Python with libtcod ===<br />
<br />
Open a command prompt (windows key + R: cmd)<br />
<br />
Run the following command:<br />
<br />
c:\> python -c "import libtcod"<br />
<br />
== Setting up your Project ==<br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
<br />
* \project\<br />
*: \libtcodpy\<br />
*: \tests\<br />
*: \firstrl.py<br />
<br />
The images below should help further qualify.<br />
<br />
=== Creating your project folder ===<br />
<br />
Now create your project's folder. Inside it, create an empty file with a name of your choice. We're using ''firstrl.py''. It'll make the tutorial easier to just use the same name, and you can always rename it later.<br />
<br />
[[File:Windows-01-162-project-folder-empty.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the relevant DLLs.<br />
<br />
[[File:Windows-02-162-dlls.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''libtcodpy'' folder.<br />
<br />
[[File:Windows-03-162-libtcodpy-folder.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''arial10x10.png'' font. If you choose a different one, you'll need to adjust how you do the tutorial accordingly. It's simpler to just use the same one as we do, and change it later, if it suits you to do so.<br />
<br />
[[File:Windows-04-162-font.png|center]]<br />
<br />
At this point, you should have a folder that looks like this:<br />
<br />
[[File:Windows-05-162-project-folder-ready.png|center]]<br />
<br />
== Validating the setup ==<br />
<br />
libtcod is compiled with the latest official release of Visual Studio. In an ideal world, it should just run, and if you have recently updated games installed from Steam or perhaps even some modern applications, it should. However, some people lack the support for that latest release of Visual Studio, and these steps help update your computer to allow libtcod to be run.<br />
<br />
<ol><br />
<li>Within the libtcod release you just extracted, you will see a file called <cite>samples.exe</cite>.</li><br />
<li>Run <cite>samples.exe</cite> and verify that it appears to work. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it just doesn't start and instead you see a message about <cite>vcruntime140.dll</cite> being required, then you need to install the correct Visual Studio 2017 runtime.</li><br />
<li>Go to the latest Microsoft Visual Studio 2017 [https://www.microsoft.com/en-us/download/details.aspx?id=53587 runtime download page], and download and install the runtime that matches the Python and libtcod you are using. If you are using 32 bit Python and libtcod, this will be the x86 runtime. If you are using the 64 bit Python and libtcod, this will be the x64 runtime. (Tip: If you think you might switch from 32 bit to 64 bit in the future, hedge your bets and install both runtimes to prevent future problems.)</li><br />
<li>Once you've installed the correct runtime, run <cite>samples.exe</cite> and verify that it now appears to work. If it still gives the same message, you didn't install the correct runtime. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it is not verified as working for you, you can open a libtcod bug report [https://bitbucket.org/libtcod/libtcod/issues?status=new&status=open on BitBucket].<br />
</ol><br />
<br />
You've verified that libtcod is working? Great! You can move onto the next section.<br />
<br />
== Finishing touches ==<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Windows&diff=47220Complete Roguelike Tutorial, using python3+libtcod, setup Windows2018-06-10T19:29:30Z<p>Bix: /* Validating the setup */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Windows =<br />
<br />
== Installing an Editor ==<br />
<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
* [https://code.visualstudio.com/downloads/ Visual Code (Editor)]<br />
* [https://notepad-plus-plus.org/download/v7.5.1.html Notepad ++ (Editor)]<br />
* [https://www.visualstudio.com/downloads/ Visual Studio (IDE)]<br />
<br />
== Installing libtcod ==<br />
<br />
Download the latest release of [https://bitbucket.org/libtcod/libtcod/downloads libtcod 1.6] and extract it somewhere. Be warned that both Python and libtcod must either be <b>both 32 bit</b>, or <b>both 64 bit</b>. If you get dll loading errors, getting this wrong is the most likely cause. libtcod will now (as of 1.6.2 and above) check they are compatible, and tell you if they are not.<br />
<br />
== Installing Python ==<br />
<br />
=== Python Executable ===<br />
If you haven't already done so, [http://www.python.org/download/ download and install Python 3.6]. Any version of Python 3, from 3.6 and above should be fine, but the latest update (of <i>Python 3</i>, not Python 2) is of course always preferable.<br />
<br />
There are reports that for older versions of libtcod on 64-bit Windows (Windows 7) that the 32 bit version is preferable, since the 64 bit version of Python seems to cause problems with libtcod. This was been tested on Windows 10 with libtcod 1.6, and was not reproducible, so it's up to you.<br />
<br />
It might be advisable to go with 32 bit just to keep things simple, and when you're gone through the tutorial look at switching to 64 bit if it matters to you to do so, and if you do encounter errors specifically related to this, report them on the [https://bitbucket.org/libtcod/libtcod/issues/ libtcod bug tracker].<br />
<br />
<br />
=== Validating Python with libtcod ===<br />
<br />
Open a command prompt (windows key + R: cmd)<br />
<br />
Run the following command:<br />
<br />
c:\> python -c "import libtcod"<br />
<br />
== Setting up your Project ==<br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
<br />
* \project\<br />
*: \libtcodpy\<br />
*: \tests\<br />
*: \firstrl.py<br />
<br />
The images below should help further qualify.<br />
<br />
=== Creating your project folder ===<br />
<br />
Now create your project's folder. Inside it, create an empty file with a name of your choice. We're using ''firstrl.py''. It'll make the tutorial easier to just use the same name, and you can always rename it later.<br />
<br />
[[File:Windows-01-162-project-folder-empty.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the relevant DLLs.<br />
<br />
[[File:Windows-02-162-dlls.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''libtcodpy'' folder.<br />
<br />
[[File:Windows-03-162-libtcodpy-folder.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''arial10x10.png'' font. If you choose a different one, you'll need to adjust how you do the tutorial accordingly. It's simpler to just use the same one as we do, and change it later, if it suits you to do so.<br />
<br />
[[File:Windows-04-162-font.png|center]]<br />
<br />
At this point, you should have a folder that looks like this:<br />
<br />
[[File:Windows-05-162-project-folder-ready.png|center]]<br />
<br />
== Validating the setup ==<br />
<br />
libtcod is compiled with the latest official release of Visual Studio. In an ideal world, it should just run, and if you have recently updated games installed from Steam or perhaps even some modern applications, it should. However, some people lack the support for that latest release of Visual Studio, and these steps help update your computer to allow libtcod to be run.<br />
<br />
<ol><br />
<li>Within the libtcod release you just extracted, you will see a file called <cite>samples.exe</cite>.</li><br />
<li>Run <cite>samples.exe</cite> and verify that it appears to work. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it just doesn't start and instead you see a message about <cite>vcruntime140.dll</cite> being required, then you need to install the correct Visual Studio 2017 runtime.</li><br />
<li>Go to the latest Microsoft Visual Studio 2015 [https://www.microsoft.com/en-us/download/details.aspx?id=53587 runtime download page], and download and install the runtime that matches the Python and libtcod you are using. If you are using 32 bit Python and libtcod, this will be the x86 runtime. If you are using the 64 bit Python and libtcod, this will be the x64 runtime. (Tip: If you think you might switch from 32 bit to 64 bit in the future, hedge your bets and install both runtimes to prevent future problems.)</li><br />
<li>Once you've installed the correct runtime, run <cite>samples.exe</cite> and verify that it now appears to work. If it still gives the same message, you didn't install the correct runtime. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it is not verified as working for you, you can open a libtcod bug report [https://bitbucket.org/libtcod/libtcod/issues?status=new&status=open on BitBucket].<br />
</ol><br />
<br />
You've verified that libtcod is working? Great! You can move onto the next section.<br />
<br />
== Finishing touches ==<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Windows&diff=47219Complete Roguelike Tutorial, using python3+libtcod, setup Windows2018-06-10T19:28:50Z<p>Bix: /* Installing an Editor */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Windows =<br />
<br />
== Installing an Editor ==<br />
<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
* [https://code.visualstudio.com/downloads/ Visual Code (Editor)]<br />
* [https://notepad-plus-plus.org/download/v7.5.1.html Notepad ++ (Editor)]<br />
* [https://www.visualstudio.com/downloads/ Visual Studio (IDE)]<br />
<br />
== Installing libtcod ==<br />
<br />
Download the latest release of [https://bitbucket.org/libtcod/libtcod/downloads libtcod 1.6] and extract it somewhere. Be warned that both Python and libtcod must either be <b>both 32 bit</b>, or <b>both 64 bit</b>. If you get dll loading errors, getting this wrong is the most likely cause. libtcod will now (as of 1.6.2 and above) check they are compatible, and tell you if they are not.<br />
<br />
== Installing Python ==<br />
<br />
=== Python Executable ===<br />
If you haven't already done so, [http://www.python.org/download/ download and install Python 3.6]. Any version of Python 3, from 3.6 and above should be fine, but the latest update (of <i>Python 3</i>, not Python 2) is of course always preferable.<br />
<br />
There are reports that for older versions of libtcod on 64-bit Windows (Windows 7) that the 32 bit version is preferable, since the 64 bit version of Python seems to cause problems with libtcod. This was been tested on Windows 10 with libtcod 1.6, and was not reproducible, so it's up to you.<br />
<br />
It might be advisable to go with 32 bit just to keep things simple, and when you're gone through the tutorial look at switching to 64 bit if it matters to you to do so, and if you do encounter errors specifically related to this, report them on the [https://bitbucket.org/libtcod/libtcod/issues/ libtcod bug tracker].<br />
<br />
<br />
=== Validating Python with libtcod ===<br />
<br />
Open a command prompt (windows key + R: cmd)<br />
<br />
Run the following command:<br />
<br />
c:\> python -c "import libtcod"<br />
<br />
== Setting up your Project ==<br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
<br />
* \project\<br />
*: \libtcodpy\<br />
*: \tests\<br />
*: \firstrl.py<br />
<br />
The images below should help further qualify.<br />
<br />
=== Creating your project folder ===<br />
<br />
Now create your project's folder. Inside it, create an empty file with a name of your choice. We're using ''firstrl.py''. It'll make the tutorial easier to just use the same name, and you can always rename it later.<br />
<br />
[[File:Windows-01-162-project-folder-empty.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the relevant DLLs.<br />
<br />
[[File:Windows-02-162-dlls.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''libtcodpy'' folder.<br />
<br />
[[File:Windows-03-162-libtcodpy-folder.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''arial10x10.png'' font. If you choose a different one, you'll need to adjust how you do the tutorial accordingly. It's simpler to just use the same one as we do, and change it later, if it suits you to do so.<br />
<br />
[[File:Windows-04-162-font.png|center]]<br />
<br />
At this point, you should have a folder that looks like this:<br />
<br />
[[File:Windows-05-162-project-folder-ready.png|center]]<br />
<br />
== Validating the setup ==<br />
<br />
libtcod is compiled with the latest official release of Visual Studio. In an ideal world, it should just run, and if you have recently updated games installed from Steam or perhaps even some modern applications, it should. However, some people lack the support for that latest release of Visual Studio, and these steps help update your computer to allow libtcod to be run.<br />
<br />
<ol><br />
<li>Within the libtcod release you just extracted, you will see a file called <cite>samples.exe</cite>.</li><br />
<li>Run <cite>samples.exe</cite> and verify that it appears to work. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it just doesn't start and instead you see a message about <cite>vcruntime140.dll</cite> being required, then you need to install the correct Visual Studio 2015 runtime.</li><br />
<li>Go to the latest Microsoft Visual Studio 2015 [https://www.microsoft.com/en-us/download/details.aspx?id=53587 runtime download page], and download and install the runtime that matches the Python and libtcod you are using. If you are using 32 bit Python and libtcod, this will be the x86 runtime. If you are using the 64 bit Python and libtcod, this will be the x64 runtime. (Tip: If you think you might switch from 32 bit to 64 bit in the future, hedge your bets and install both runtimes to prevent future problems.)</li><br />
<li>Once you've installed the correct runtime, run <cite>samples.exe</cite> and verify that it now appears to work. If it still gives the same message, you didn't install the correct runtime. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it is not verified as working for you, you can open a libtcod bug report [https://bitbucket.org/libtcod/libtcod/issues?status=new&status=open on BitBucket].<br />
</ol><br />
<br />
You've verified that libtcod is working? Great! You can move onto the next section.<br />
<br />
== Finishing touches ==<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Windows&diff=47218Complete Roguelike Tutorial, using python3+libtcod, setup Windows2018-06-10T19:27:14Z<p>Bix: /* Installing an Editor */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Windows =<br />
<br />
== Installing an Editor ==<br />
<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
* [https://notepad-plus-plus.org/download/v7.5.1.html Notepad ++ (Editor)]<br />
* [https://www.visualstudio.com/downloads/ Visual Studio (IDE)]<br />
* [https://code.visualstudio.com/downloads/ Visual Code (Editor)]<br />
<br />
== Installing libtcod ==<br />
<br />
Download the latest release of [https://bitbucket.org/libtcod/libtcod/downloads libtcod 1.6] and extract it somewhere. Be warned that both Python and libtcod must either be <b>both 32 bit</b>, or <b>both 64 bit</b>. If you get dll loading errors, getting this wrong is the most likely cause. libtcod will now (as of 1.6.2 and above) check they are compatible, and tell you if they are not.<br />
<br />
== Installing Python ==<br />
<br />
=== Python Executable ===<br />
If you haven't already done so, [http://www.python.org/download/ download and install Python 3.6]. Any version of Python 3, from 3.6 and above should be fine, but the latest update (of <i>Python 3</i>, not Python 2) is of course always preferable.<br />
<br />
There are reports that for older versions of libtcod on 64-bit Windows (Windows 7) that the 32 bit version is preferable, since the 64 bit version of Python seems to cause problems with libtcod. This was been tested on Windows 10 with libtcod 1.6, and was not reproducible, so it's up to you.<br />
<br />
It might be advisable to go with 32 bit just to keep things simple, and when you're gone through the tutorial look at switching to 64 bit if it matters to you to do so, and if you do encounter errors specifically related to this, report them on the [https://bitbucket.org/libtcod/libtcod/issues/ libtcod bug tracker].<br />
<br />
<br />
=== Validating Python with libtcod ===<br />
<br />
Open a command prompt (windows key + R: cmd)<br />
<br />
Run the following command:<br />
<br />
c:\> python -c "import libtcod"<br />
<br />
== Setting up your Project ==<br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
<br />
* \project\<br />
*: \libtcodpy\<br />
*: \tests\<br />
*: \firstrl.py<br />
<br />
The images below should help further qualify.<br />
<br />
=== Creating your project folder ===<br />
<br />
Now create your project's folder. Inside it, create an empty file with a name of your choice. We're using ''firstrl.py''. It'll make the tutorial easier to just use the same name, and you can always rename it later.<br />
<br />
[[File:Windows-01-162-project-folder-empty.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the relevant DLLs.<br />
<br />
[[File:Windows-02-162-dlls.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''libtcodpy'' folder.<br />
<br />
[[File:Windows-03-162-libtcodpy-folder.png|center]]<br />
<br />
From within the libtcod build you downloaded above, select and copy the ''arial10x10.png'' font. If you choose a different one, you'll need to adjust how you do the tutorial accordingly. It's simpler to just use the same one as we do, and change it later, if it suits you to do so.<br />
<br />
[[File:Windows-04-162-font.png|center]]<br />
<br />
At this point, you should have a folder that looks like this:<br />
<br />
[[File:Windows-05-162-project-folder-ready.png|center]]<br />
<br />
== Validating the setup ==<br />
<br />
libtcod is compiled with the latest official release of Visual Studio. In an ideal world, it should just run, and if you have recently updated games installed from Steam or perhaps even some modern applications, it should. However, some people lack the support for that latest release of Visual Studio, and these steps help update your computer to allow libtcod to be run.<br />
<br />
<ol><br />
<li>Within the libtcod release you just extracted, you will see a file called <cite>samples.exe</cite>.</li><br />
<li>Run <cite>samples.exe</cite> and verify that it appears to work. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it just doesn't start and instead you see a message about <cite>vcruntime140.dll</cite> being required, then you need to install the correct Visual Studio 2015 runtime.</li><br />
<li>Go to the latest Microsoft Visual Studio 2015 [https://www.microsoft.com/en-us/download/details.aspx?id=53587 runtime download page], and download and install the runtime that matches the Python and libtcod you are using. If you are using 32 bit Python and libtcod, this will be the x86 runtime. If you are using the 64 bit Python and libtcod, this will be the x64 runtime. (Tip: If you think you might switch from 32 bit to 64 bit in the future, hedge your bets and install both runtimes to prevent future problems.)</li><br />
<li>Once you've installed the correct runtime, run <cite>samples.exe</cite> and verify that it now appears to work. If it still gives the same message, you didn't install the correct runtime. If it opens a colourful window, then it is now verified as working for you.</li><br />
<li>If it is not verified as working for you, you can open a libtcod bug report [https://bitbucket.org/libtcod/libtcod/issues?status=new&status=open on BitBucket].<br />
</ol><br />
<br />
You've verified that libtcod is working? Great! You can move onto the next section.<br />
<br />
== Finishing touches ==<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47217Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T17:05:56Z<p>Bix: /* Environmental Variable: Path */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with autoconf tools<br />
* move the resulting library files ($HOME/repos/other/libtcod/build/autoconf/.libs/*) into a proper folder (/usr/local/lib)<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5" (After 15 June 2018, this will be 3.7).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal. Do not add this line to your .bashrc file as this will invariably cause issues with subprocesses.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47216Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T17:05:08Z<p>Bix: /* Environmental Variable: Path */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with autoconf tools<br />
* move the resulting library files ($HOME/repos/other/libtcod/build/autoconf/.libs/*) into a proper folder (/usr/local/lib)<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5" (After 15 June 2018, this will be 3.7).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal: <br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin:$PATH</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47215Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T17:04:44Z<p>Bix: /* Environmental Variable: Path */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with autoconf tools<br />
* move the resulting library files ($HOME/repos/other/libtcod/build/autoconf/.libs/*) into a proper folder (/usr/local/lib)<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5" (After 15 June 2018, this will be 3.7).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following line to your $HOME/.bash_profile file and then restart your terminal: <br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47214Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T17:04:01Z<p>Bix: /* Setting up Python */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with autoconf tools<br />
* move the resulting library files ($HOME/repos/other/libtcod/build/autoconf/.libs/*) into a proper folder (/usr/local/lib)<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5" (After 15 June 2018, this will be 3.7).<br />
<br />
=== Troubleshooting ===<br />
<br />
==== Environmental Variable: Path ====<br />
Brew installs python into /usr/local/opt/python. You may need to add the following: <br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin</syntaxhighlight></div><br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47213Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:58:18Z<p>Bix: /* Setup Mac */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with autoconf tools<br />
* move the resulting library files ($HOME/repos/other/libtcod/build/autoconf/.libs/*) into a proper folder (/usr/local/lib)<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47212Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:57:23Z<p>Bix: /* Pip */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all project related python dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with autoconf tools<br />
* move the resulting library files ($HOME/repos/other/libtcod/build/autoconf/.libs/*) into a proper folder (/usr/local/lib)<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47211Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:56:58Z<p>Bix: /* Installing libtcod */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all python related dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with autoconf tools<br />
* move the resulting library files ($HOME/repos/other/libtcod/build/autoconf/.libs/*) into a proper folder (/usr/local/lib)<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47210Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:56:03Z<p>Bix: /* Setting up Python */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all python related dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47209Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:55:32Z<p>Bix: /* Setting up Python */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
=== Pip ===<br />
Python 3 comes bundled with a python package manager called `pip` which is used to install all python related dependencies. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47208Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:54:55Z<p>Bix: /* Python Virtual Environments */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47207Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:54:23Z<p>Bix: /* Setting up Python */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47206Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:53:14Z<p>Bix: /* Installing libtcod */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47205Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:52:52Z<p>Bix: /* Installing libtcod */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
<br />
===== Installing scons generated binary =====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47204Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:52:23Z<p>Bix: /* Creating a virtual environment */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
Back to the top of the repo:<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
Most python developers will use a virtual environment to isolate python and any project related dependencies from being installed into the system. This practice is a safety measure to prevent a developer's computer from becoming unstable and is highly recommended.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47203Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:49:18Z<p>Bix: /* Configuring the build tools */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
Back to the top of the repo:<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47202Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:46:18Z<p>Bix: /* Installing libtcod into our project */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd /path/to/libtcod/repository</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
Back to the top of the repo:<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) $ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47201Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:45:05Z<p>Bix: /* Creating a virtual environment */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd /path/to/libtcod/repository</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
Back to the top of the repo:<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.5". Note that the version number and virtual env name is also in the command-line prompt<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47200Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:44:05Z<p>Bix: /* Configuring the build tools */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd /path/to/libtcod/repository</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
Back to the top of the repo:<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47199Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:43:48Z<p>Bix: /* Installing scons generated binary */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd /path/to/libtcod/repository</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make'</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
Back to the top of the repo:<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod<br />
mkdir -p /usr/local/lib<br />
cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47198Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:43:27Z<p>Bix: /* Installing scons generated binary */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd /path/to/libtcod/repository</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make'</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
Back to the top of the repo:<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">bash-3.2$ cd $HOME/repos/other/libtcod<br />
bash-3.2$ mkdir -p /usr/local/lib<br />
bash-3.2$ cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47197Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:42:15Z<p>Bix: /* Build libtcod */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Configuring the build tools ====<br />
There are several options for building, but the most straightforward mechanism is to use autotools to build.<br />
<br />
Make sure you're at the top of the libtcod repo:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd /path/to/libtcod/repository</syntaxhighlight></div><br />
<br />
Change to the autotools build folder:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div><br />
<br />
Run autotools configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div><br />
<br />
Run libtcod compilation configuration:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div><br />
<br />
Build libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make'</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
Back to the top of the repo:<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">bash-3.2$ cd -<br />
bash-3.2$ mkdir -p /usr/local/lib<br />
bash-3.2$ cp -rf build/autotools/.libs/* /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47196Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:28:08Z<p>Bix: /* Install libtcod dependencies */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod build and runtime dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run and several other packages to build. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install autoconf automake libtool pkg-config sdl2 mercurial</syntaxhighlight></div><br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
===== Installing python library =====<br />
<br />
After installing the binary and updating the system variable, the python wrapper library (libtcodpy) must also be installed so that python can access the libtcod library.<br />
<br />
1. cd /to/top/of/libtcod/repository<br />
2. pip install ./python<br />
<br />
==== Validating libtcod Installation ====<br />
Assuming you have followed the steps above, you should be able to run the following command without an error:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47195Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:26:34Z<p>Bix: /* Setting up Python */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install sdl2</syntaxhighlight></div><br />
<br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
===== Installing python library =====<br />
<br />
After installing the binary and updating the system variable, the python wrapper library (libtcodpy) must also be installed so that python can access the libtcod library.<br />
<br />
1. cd /to/top/of/libtcod/repository<br />
2. pip install ./python<br />
<br />
==== Validating libtcod Installation ====<br />
Assuming you have followed the steps above, you should be able to run the following command without an error:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47194Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T16:01:21Z<p>Bix: /* Validating libtcod Installation */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python3 correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install sdl2</syntaxhighlight></div><br />
<br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
===== Installing python library =====<br />
<br />
After installing the binary and updating the system variable, the python wrapper library (libtcodpy) must also be installed so that python can access the libtcod library.<br />
<br />
1. cd /to/top/of/libtcod/repository<br />
2. pip install ./python<br />
<br />
==== Validating libtcod Installation ====<br />
Assuming you have followed the steps above, you should be able to run the following command without an error:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47193Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T15:55:43Z<p>Bix: /* Installing vsh */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python3 correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install sdl2</syntaxhighlight></div><br />
<br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
===== Installing python library =====<br />
<br />
After installing the binary and updating the system variable, the python wrapper library (libtcodpy) must also be installed so that python can access the libtcod library.<br />
<br />
1. cd /to/top/of/libtcod/repository<br />
2. pip install ./python<br />
<br />
==== Validating libtcod Installation ====<br />
Run the following command:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcod"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. Additionally, vsh runs the python virtualenv in a sub-process shell To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div><br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47192Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T15:55:00Z<p>Bix: /* Creating a virtual environment using vsh */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python3 correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install sdl2</syntaxhighlight></div><br />
<br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
===== Installing python library =====<br />
<br />
After installing the binary and updating the system variable, the python wrapper library (libtcodpy) must also be installed so that python can access the libtcod library.<br />
<br />
1. cd /to/top/of/libtcod/repository<br />
2. pip install ./python<br />
<br />
==== Validating libtcod Installation ====<br />
Run the following command:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcod"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div> <br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47191Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T15:54:23Z<p>Bix: /* Setup Mac */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python3 correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install sdl2</syntaxhighlight></div><br />
<br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
===== Installing python library =====<br />
<br />
After installing the binary and updating the system variable, the python wrapper library (libtcodpy) must also be installed so that python can access the libtcod library.<br />
<br />
1. cd /to/top/of/libtcod/repository<br />
2. pip install ./python<br />
<br />
==== Validating libtcod Installation ====<br />
Run the following command:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcod"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
==== Installing vsh ====<br />
<br />
Vsh is a virtual environment generation tool. This will help you manage and build virtual environments. Most python developers will use a virtual environment to isolate their projects from their system. To install:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m pip install vsh</syntaxhighlight></div> <br />
<br />
==== Creating a virtual environment using vsh ====<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh --python python3 -m rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47190Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T15:50:49Z<p>Bix: /* Installing libtcod */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python3 correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install sdl2</syntaxhighlight></div><br />
<br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
===== Installing python library =====<br />
<br />
After installing the binary and updating the system variable, the python wrapper library (libtcodpy) must also be installed so that python can access the libtcod library.<br />
<br />
1. cd /to/top/of/libtcod/repository<br />
2. pip install ./python<br />
<br />
==== Validating libtcod Installation ====<br />
Run the following command:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcod"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vex --python python3 -m rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47189Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T15:50:15Z<p>Bix: /* Installing libtcod binary */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python3 correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install sdl2</syntaxhighlight></div><br />
<br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
<br />
Scons will generate a compiled binary from the c code found within the libtcod repository. However, Scons will not actually install the library itself to your system. Instead, you must add the compiled binary to a standard system path.<br />
<br />
===== Installing scons generated binary =====<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
===== Updating system environment variables =====<br />
Additionally, we must add an environment variable to your system so that libtcod can find the binary file.<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ export LIBTCOD_DLL_PATH=/usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
Special note: the above command should be added to your shell environment on startup (e.g. if you use the default bash, add the export line to bashrc)<br />
<br />
<br />
===== Installing python library =====<br />
<br />
After installing the binary and updating the system variable, the python wrapper library (libtcodpy) must also be installed so that python can access the libtcod library.<br />
<br />
1. cd /to/top/of/libtcod/repository<br />
2. pip install ./python<br />
<br />
==== Validating libtcod Installation ====<br />
Run the following command:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcod"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vex --python python3 -m rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47188Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T15:42:40Z<p>Bix: /* Installing libtcod */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python3 correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install sdl2</syntaxhighlight></div><br />
<br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod binary ====<br />
The best place to put the new library file, `libtcod.dylib`, is in a system folder: `/usr/local/lib`. The rules for path resolution for finding `libtcod.dylib` within the python library are as follows:<br />
<br />
1. Look for `libtcod.dylib` in the python `site-packages` folder where the python library was installed (this will be in a virtual environment path)<br />
2. Look for `libtcod.dylib` from where the current script is being executed (so, for example, where you run your rogue-like project)<br />
3. Look for any paths defined within the environment variable, `LIBTCOD_DLL_PATH` (we defined this in our .bashrc file)<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
==== Validating libtcod Installation ====<br />
Run the following command:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcod"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vex --python python3 -m rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_setup_Mac&diff=47187Complete Roguelike Tutorial, using python3+libtcod, setup Mac2018-06-10T15:42:10Z<p>Bix: /* Installing libtcod */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
= Setup Mac =<br />
<br />
== Installing an Editor ==<br />
Editors are often a personal choice. Their primary function is to allow the creation and editing of code. Additional functionality may be included such as code intelligence, code completion, static analysis, testing, debugging, etc. usually through plugins. Some editors are also IDEs or integrated development environments. They can help manage the lifetime of a project. For this tutorial, you only need an editor and none of the extra functionality, but an IDE may be very helpful when learning programming (or Python) for the first time.<br />
<br />
The following editors are all excellent for python development. You only need one. <br />
<br />
'''Note that these are listed in order of decreasing preference for development with python as a beginner. (i.e. pick one of the top ones for the best out of the box python experience)'''<br />
<br />
* [https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=mac&code=PCC Pycharm (IDE)]<br />
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]<br />
* [https://macromates.com/ Text Mate (Editor)]<br />
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]<br />
<br />
<br />
<br />
== Setting up Bash ==<br />
Open finder (cmd + space) and go to your home folder and then press CMD + SHIFT + . (command + shift + period). Look for a ".bashrc" file within finder. Open it up in your editor or create a new file.<br />
Make sure the following lines show up at the bottom of the file:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash"><br />
# Setup prompt colors<br />
reset_color="\[\e[m\]"<br />
declare -A fg<br />
magenta="\[\e[35m\]"<br />
yellow="\[\e[33m\]"<br />
green="\[\e[32m\]"<br />
<br />
function virtualenv_prompt() {<br />
if [ -n "$VIRTUAL_ENV" ]; then<br />
pyver=$(python -V 2>&1 | cut -f2 -d' ')<br />
echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "<br />
fi<br />
}<br />
<br />
export PS1="$(virtualenv_prompt)${PS1}"<br />
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"<br />
</syntaxhighlight></div><br />
<br />
== Opening a Terminal ==<br />
While Mac comes with a Terminal built-in, [https://www.iterm2.com/ Iterm2] is the recommend install for a terminal. Once installed, simply press `Cmd + Space Bar` and type in iterm or terminal (for the default mac terminal). Whether you've installed iterm or are using the default terminal, we will use the word "terminal" below to refer to your preferred program (iterm or terminal).<br />
<br />
<br />
== Setting up Homebrew ==<br />
[https://docs.brew.sh/Installation.html Homebrew] is a package manager for Mac. Within a terminal, type the following to install homebrew:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</syntaxhighlight></div><br />
<br />
<br />
== Setting up Mercurial ==<br />
Now that we have homebrew, we can install several dependencies. [https://www.mercurial-scm.org/ Mercurial] allows us to download the `libtcod` library from `bitbucket.org` where the source code resides. Mercurial is an alternative to [https://git-scm.com/ Git] for [https://en.wikipedia.org/wiki/Software_configuration_management software configuration management]. Within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div><br />
<br />
<br />
== Setting up Python ==<br />
Because we would like to build libtcod with [http://scons.org/ scons], we'll need to make sure that we have Python 3 installed. To install the latest python version, simply run within a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install python3</syntaxhighlight></div><br />
<br />
To validate that we've installed python3 correctly, within a terminal, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div><br />
<br />
The version should be at least "Python 3.6.5".<br />
<br />
<br />
<br />
=== Setting up Python dependencies ===<br />
<br />
==== Pip ====<br />
Python 3 comes bundled with a python package manager called `pip`. To make sure we have `pip` installed run in a terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python -m ensurepip</syntaxhighlight></div><br />
<br />
==== Python Virtual Environments ====<br />
Additionally, we'll want the scons build system ready to go for libtcod. The correct way to isolate python packages is using a [https://docs.python.org/3/tutorial/venv.html virtual environment], which sandboxes python versions and their packages.<br />
<br />
== Installing libtcod ==<br />
At this point, we should be ready to download and install libtcod. There are several basic steps required to make this work.<br />
<br />
* install libtcod dependencies<br />
* download the source code from bitbucket using mercurial<br />
* build the source code with scons from within our virtual environment<br />
* move the resulting library file (libtcod.dylib) into a proper folder<br />
<br />
<br />
=== Install libtcod dependencies ===<br />
Libtcod relies upon [https://www.libsdl.org/download-2.0.php SDL2] to run. To install sdl, run within the terminal:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install sdl2</syntaxhighlight></div><br />
<br />
<br />
=== Download libtcod ===<br />
The build process is fairly straight forward, but first we need to download the source code from bitbucket.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME<br />
mkdir -p $HOME/repos/other/<br />
cd $HOME/repos/other<br />
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div><br />
<br />
<br />
<br />
=== Build libtcod ===<br />
<br />
==== Creating a libtcod build environment ====<br />
We need create a virtual environment to install and run scons. Running the following command will create the virtual environment, libtcod-build.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python3 -m venv ~/.virtualenvs/libtcod-build</syntaxhighlight></div><br />
<br />
To enter the virtual environment, run:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">. ~/.virtualenvs/libtcod-build/bin/activate</syntaxhighlight></div><br />
<br />
You should now see a prompt that looks something like this:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$</syntaxhighlight></div><br />
<br />
==== Installing scons within Virtual Environment ====<br />
To install scons, we simply use `pip` from within the virtual environment:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ pip install scons</syntaxhighlight></div><br />
<br />
<br />
==== Build using Scons ====<br />
We'll want to build from the scons folder.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ cd libtcod/build/scons/<br />
(libtcod-build) bash-3.2$ scons build<br />
(libtcod-build) bash-3.2$ cd ../../</syntaxhighlight></div><br />
<br />
This should produce a mac binary library file called "libtcod.dylib" under a new folder, libtcod-1.6.6-x86.x86_64-DEBUG. You can check to see if the file is created:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ find . -name "libtcod.dylib"</syntaxhighlight></div><br />
<br />
==== Installing libtcod ====<br />
The best place to put the new library file, `libtcod.dylib`, is in a system folder: `/usr/local/lib`. The rules for path resolution for finding `libtcod.dylib` within the python library are as follows:<br />
<br />
1. Look for `libtcod.dylib` in the python `site-packages` folder where the python library was installed (this will be in a virtual environment path)<br />
2. Look for `libtcod.dylib` from where the current script is being executed (so, for example, where you run your rogue-like project)<br />
3. Look for any paths defined within the environment variable, `LIBTCOD_DLL_PATH` (we defined this in our .bashrc file)<br />
<br />
To install in the recommended system path:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ mkdir -p /usr/local/lib<br />
(libtcod-build) bash-3.2$ cp `find . -name "libtcod.dylib"` /usr/local/lib<br />
</syntaxhighlight></div><br />
<br />
==== Validating libtcod Installation ====<br />
Run the following command:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(libtcod-build) bash-3.2$ python -c "import libtcod"</syntaxhighlight></div><br />
<br />
== Setting up your Project ==<br />
<br />
=== Creating a virtual environment ===<br />
To exit out of the python 2, libtcod-build virtual environment from above, just type in "exit" or press: control + d<br />
<br />
We'll want to make sure we have a python 3 environment going forward, and we need to make sure that we've installed libtcod within that virtual environment.<br />
<br />
To create a new virtual environment with python 3 for our project, just use the following:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vex --python python3 -m rl</syntaxhighlight></div><br />
<br />
You should now be in your virtual environment<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$</syntaxhighlight></div><br />
<br />
To make sure, test your python version. You should see something like version "3.6.2".<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python --version</syntaxhighlight></div><br />
<br />
<br />
=== Installing libtcod into our project ===<br />
Navigate to where libtcod was downloaded and then run the installation or libtcod:<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ cd $HOME/repos/other/libtcod/python<br />
(venv:rl|3.6.2) bash-3.2$ pip install .<br />
(venv:rl|3.6.2) bash-3.2$ cd -</syntaxhighlight></div><br />
<br />
Validate that we have libtcod installed.<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.2) bash-3.2$ python -c "import libtcodpy"</syntaxhighlight></div><br />
<br />
=== Folder structure ===<br />
<br />
Our first roguelike will be run entirely from a single file, ''firstrl.py''. However, to support some future needs regarding packaging and to make development easier, we need to add our dependencies into the project folder.<br />
<br />
When we're all done, the basic folder structure should look like this:<br />
<br />
* \project\<br />
*: \firstrl.py<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">mkdir -p $HOME/repos/mine/roguelike<br />
touch $HOME/repos/mine/roguelike/firstrl.py<br />
</syntaxhighlight></div><br />
<br />
== Finishing touches ==<br />
Congratulations!<br />
<br />
You're ready to start editing ''firstrl.py''!<br />
<br />
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_Python3%2Blibtcod,_extras&diff=47069Complete Roguelike Tutorial, using Python3+libtcod, extras2018-05-09T21:19:44Z<p>Bix: </p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
__TOC__<br />
<br />
<center><h1>'''Extras'''</h1></center><br />
<br />
== A neat Python shortcut for Notepad++ ==<br />
<br />
Although Notepad++ is light and has many nice features, it can be tricky to set up a shortcut to quickly run your game and see any errors or debug output. After reading the docs on Run commands, you might try to open a console (''cmd'') that doesn't close after the program runs so you can see debug output (''/k''), and using the path to the currently open file:<br />
<br />
<br />
<pre style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0">cmd /k "$(FULL_CURRENT_PATH)"</pre><br />
<br />
<br />
You then may puzzle over the following error (or similar), which doesn't seem connected to Notepad++ at all:<br />
<br />
<br />
<pre style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0">WindowsError: [Error 126] The specified module could not be found</pre><br />
<br />
<br />
The problem is this: Notepad++ and Python are smart enough to run the file, but not smart enough to initialize the "current directory" to the file's directory. So when trying to load libtcod, Python looks for it in all the usual places ''except'' where you put libtcod. (Computers can be so thick sometimes!) The fix is to previously "change directory" there, which can be done with a batch file.<br />
<br />
Here's my setup. I want to launch the file in a console, so I can see any debug output, and I wanna keep it around in case of errors (to see the traceback) but close it automatically if the program runs fine -- accumulating lots of console windows when there are no errors is annoying. (To explicitly "pause" the console even when there are no errors call the built-in function raw_input from your program.) I simply created a windows batch file ''debug_py.bat'' with this:<br />
<br />
<br />
<pre style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0">@echo off<br />
<br />
cd %1<br />
%2<br />
<br />
if not errorlevel 1 goto quit<br />
echo.<br />
echo.<br />
pause<br />
:quit</pre><br />
<br />
<br />
Assuming parameter %1 is the file's directory, and %2 is the file name, this just changes to that dir and runs it, pausing the console if there's an error. The notepad++ shortcut that executes it with the correct parameters is this:<br />
<br />
<br />
<pre style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0">"C:\whatever\debug_py.bat" "$(CURRENT_DIRECTORY)" $(FILE_NAME)</pre><br />
<br />
<br />
Just change "C:\whatever\debug_py.bat" to the full path of the bat file you created. To create that shortcut in Notepad++ go to menu Run->Run..., paste that line into the textbox, then choose Save. No more problems, and the console closes automatically if there are no errors.<br />
<br />
<br />
== Old-school wall and floor tiles ==<br />
<br />
Not a big fan of the new-school look of the map tiles in the tutorial? Perhaps you want your game to look more like one of the [[Major Roguelikes]], with ' '''.''' ' characters for floor and ' '''#''' ' for wall tiles. No problem, just a couple of tweaks.<br />
<br />
First, the most obvious one: in the ''render_all()'' function, replace the ''console_set_back'' calls, which only change the background color of a tile, with ''console_put_char_ex'', which changes everything (here's the [http://roguecentral.org/doryen/data/libtcod/doc/1.5.1/html2/console_draw_basic.html?c=false&cpp=false&cs=false&py=true&lua=false#7 relevant page of the manual.]). So a line like this:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python">libtcod.console_set_back(con, x, y, color_dark_wall, libtcod.BKGND_SET)</syntaxhighlight></div><br />
<br />
<br />
Changes to something like this:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python">libtcod.console_put_char_ex(con, x, y, '#', libtcod.white, libtcod.dark_blue)</syntaxhighlight></div><br />
<br />
<br />
Which will change it to a white ' '''#''' ' character on a dark blue background. This is only an example; I'm sure you'd like to choose other colors!<br />
<br />
If you change the floor characters too, it doesn't quite behave as expected, since the player erases the floor characters it steps on. To fix this you need to change the ''Object'' 's ''clear'' method, to this:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> def clear(self):<br />
#erase the character that represents this object<br />
if libtcod.map_is_in_fov(fov_map, self.x, self.y):<br />
libtcod.console_put_char_ex(con, self.x, self.y, '.', libtcod.white, libtcod.dark_blue)</syntaxhighlight></div><br />
<br />
<br />
Assuming, of course, those are the colors and character you wanted for a lit ground tile!<br />
<br />
<br />
== Real-time combat ==<br />
<br />
Ok, so you're making a real-time game. You got through the section on combat, but the system described there is more or less turn-based. Don't despair! The only thing missing is a speed system.<br />
<br />
Each object will have a ''wait'' value, which tells the number of frames it has to wait until it can take another action (move or attack). It's decreased by 1 every frame, and when it's 0, the object can move or hit again! Doing it will increase the ''wait'' value, so it has to wait again.<br />
<br />
We'll start by defining a few speed constants, which are the values that ''wait'' is increased to when taking certain actions.<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python">#number of frames to wait after moving/attacking<br />
PLAYER_SPEED = 2<br />
DEFAULT_SPEED = 8<br />
DEFAULT_ATTACK_SPEED = 20</syntaxhighlight></div><br />
<br />
<br />
As you can see, we're making it a bit easier on the player, so he can maneuver around the monsters fast, but both attack at the same rate. These numbers are entirely tweakable of course!<br />
<br />
The ''Object'' 's ''__init__'' method must accept a move speed. Just add ''speed=DEFAULT_SPEED'' as a parameter; if unspecified, the default speed is used. The initialization code stores it, and sets ''wait'' to 0:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> self.speed = speed<br />
self.wait = 0</syntaxhighlight></div><br />
<br />
<br />
Whenever the object moves, it has to wait. At the end of the ''move'' method:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python">self.wait = self.speed</syntaxhighlight></div><br />
<br />
<br />
The ''Fighter'' class stores the attack speed, so it's very similar. Its ''__init__'' method accepts the parameter ''attack_speed=DEFAULT_ATTACK_SPEED'', and stores it with ''self.attack_speed = attack_speed''. At the end of the ''attack'' method, the object has to wait until it can attack or move again:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> self.owner.wait = self.attack_speed</syntaxhighlight></div><br />
<br />
<br />
Ok, but how do we enforce these wait periods? For the player, before testing for the movement keys (in ''handle_keys''), add the wait logic right after the ''if game_state == 'playing' ...'' line:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> if player.wait > 0: #don't take a turn yet if still waiting<br />
player.wait -= 1<br />
return</syntaxhighlight></div><br />
<br />
<br />
So the movement/attack keys aren't used if the player has to wait. Next, the same behavior for the monsters! Replace the ''object.ai.take_turn()'' line with the block:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> if object.wait > 0: #don't take a turn yet if still waiting<br />
object.wait -= 1<br />
else:<br />
object.ai.take_turn()</syntaxhighlight></div><br />
<br />
<br />
Also, there's a condition that only lets the monsters move/attack after the player moved or attacked. This is appropriate for a turn-based game, but for real-time, it has to be removed. A few lines above the code you just modified, replace the line ''if game_state == 'playing' and player_action != 'didnt-take-turn':'' with just ''if game_state == 'playing':''.<br />
<br />
And don't forget to switch to the non-blocking function in ''handle_keys()'':<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> key = libtcod.console_check_for_keypress()</syntaxhighlight></div><br />
<br />
<br />
There it is, a speed system for a real-time game! Don't forget to add ''speed=PLAYER_SPEED'' when creating the player, or any other speeds you want to modify. I left all the others as default.<br />
<br />
<br />
== Mouse-driven Menus ==<br />
Adding basic mouse support to the menu system is really easy. If you've completed [http://www.roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_part_9 Part 9: Spells and ranged combat], then you've already seen the code needed to check the mouse position and button presses. We're going to make a tiny change to code in [http://www.roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_part_10 Part 10: Main menu and saving], so keep that in mind if you haven't gotten that far yet.<br />
<br />
Most of our changes will be to ''menu()''. Let's start by adding the globals "key" and "mouse" to the top, so we can use these already defined references:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python">def menu(header, options, width):<br />
global key, mouse</syntaxhighlight></div><br />
<br />
<br />
At this point, also move the following two lines from ''play_game()'' to the very end of the file, just before ''main_menu()'' is called. We'll need to initialize them now, since we're going to be using them in all menu code, including the main menu.<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python">mouse = libtcod.Mouse()<br />
key = libtcod.Key()</syntaxhighlight></div><br />
<br />
<br />
Next, we'll get the x- and y-offsets of the menu options. These will let us calculate coordinates for the mouse relative to the menu options, instead of the screen, making it simple to tell if the mouse is inside the menu and where:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> #compute x and y offsets to convert console position to menu position<br />
x_offset = x #x is the left edge of the menu<br />
y_offset = y + header_height #subtract the height of the header from the top edge of the menu</syntaxhighlight></div><br />
<br />
<br />
Because the main loop is paused while a menu is open, if we try to check the mouse position now, we'll only get its position the last time it was updated, at the start of the main loop. The same issue arose in the ''target_tile()'' and ''target_monster()'' functions, with the same solution. We'll enclose the rest of the ''menu()'' function in a loop of its own, so that we can continuously update the mouse and check for input. Only when the player makes a selection will we break out of it and return to the game loop.<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python"> while True:<br />
#present the root console to the player and check for input<br />
libtcod.console_flush()<br />
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse)</syntaxhighlight></div><br />
<br />
<br />
Notice that we changed from ''console_wait_for_keypress'' to ''sys_check_for_event''. This lets us both update the mouse and avoid pausing execution. If you play the game now, you'll see that the inventory is unusable - it immediately closes itself. That's because we haven't added any checks yet, so the function immediately returns None before the player can react. Here is the updated ''menu()'' function:<br />
<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="python">def menu(header, options, width):<br />
global key, mouse<br />
<br />
if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.')<br />
<br />
#calculate total height for the header (after auto-wrap) and one line per option<br />
header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header)<br />
if header == '':<br />
header_height = 0<br />
height = len(options) + header_height<br />
<br />
#create an off-screen console that represents the menu's window<br />
window = libtcod.console_new(width, height)<br />
<br />
#print the header, with auto-wrap<br />
libtcod.console_set_default_foreground(window, libtcod.white)<br />
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)<br />
<br />
#print all the options<br />
y = header_height<br />
letter_index = ord('a')<br />
for option_text in options:<br />
text = '(' + chr(letter_index) + ') ' + option_text<br />
libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text)<br />
y += 1<br />
letter_index += 1<br />
<br />
#blit the contents of "window" to the root console<br />
x = SCREEN_WIDTH/2 - width/2<br />
y = SCREEN_HEIGHT/2 - height/2<br />
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)<br />
<br />
#compute x and y offsets to convert console position to menu position<br />
x_offset = x #x is the left edge of the menu<br />
y_offset = y + header_height #subtract the height of the header from the top edge of the menu<br />
<br />
while True:<br />
#present the root console to the player and check for input<br />
libtcod.console_flush()<br />
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse)<br />
<br />
if (mouse.lbutton_pressed):<br />
(menu_x, menu_y) = (mouse.cx - x_offset, mouse.cy - y_offset)<br />
#check if click is within the menu and on a choice<br />
if menu_x >= 0 and menu_x < width and menu_y >= 0 and menu_y < height - header_height:<br />
return menu_y<br />
<br />
if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE:<br />
return None #cancel if the player right-clicked or pressed Escape<br />
<br />
if key.vk == libtcod.KEY_ENTER and key.lalt:<br />
#Alt+Enter: toggle fullscreen<br />
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())<br />
<br />
#convert the ASCII code to an index; if it corresponds to an option, return it<br />
index = key.c - ord('a')<br />
if index >= 0 and index < len(options): return index<br />
#if they pressed a letter that is not an option, return None<br />
if index >= 0 and index <= 26: return None</syntaxhighlight></div><br />
<br />
<br />
The idea is to get the latest inputs from the mouse and keyboard, then check them against the menu contents. If the mouse is left-clicked, use the x- and y-offsets we calculated earlier to determine where in the menu the user clicked. The leftmost tile of the first option (not the header!) will be (0, 0), while the rightmost tile of the last option will be (width - 1, height - 1). Because the choices are numbered top to bottom starting from 0, and can only take up one line, ''menu_y'' will always equal the index of a valid choice if it lies inside the bounds of the menu. The rest is just checking for a few different ways to cancel and allowing the player to toggle fullscreen from inside a menu.<br />
<br />
<br />
== Creating a Binary ==<br />
Source is great, but let's be honest, forcing your players to recreate your development environment isn't very polite. Let's do them the courtesy of packaging it all up in a nice and tidy executable.<br />
<br />
=== Windows+py2exe ===<br />
Py2exe is a set of tools for creating stand-alone Windows programs from python scripts. Perfect, this is exactly what we want! [http://sourceforge.net/projects/py2exe/files/py2exe/ Download py2exe] (version 0.6.9) for your installed version of Python and install it. The project homepage has a [http://www.py2exe.org/index.cgi/Tutorial nice tutorial] which can help get you started, or you can use my script I've provided below.<br />
<br />
If you are using my script, set the target_file to the name of the script that serves as the main point of entry for your game. Then just run the script! You should see a deal of console text about byte-compiling and copying, and if all goes well there should be two new directories. The dist folder is the one we care about, so go check it out.<br />
<br />
Inside the dist folder there should be a shiny new executable file and a few other files. In my test I had: main.exe, SDL.dll, lib-tcod.dll, and w9xpopen.exe. Don't delete these, you need them! Run the executable to verify everything works fine and you are ready to package and distribute your game!<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
from distutils.core import setup<br />
import py2exe<br />
import os<br />
import sys<br />
<br />
sys.argv.append('py2exe')<br />
<br />
# The filename of the script you use to start your program.<br />
target_file = 'main.py'<br />
<br />
# The root directory containing your assets, libraries, etc.<br />
assets_dir = '.\\'<br />
<br />
# Filetypes not to be included in the above.<br />
excluded_file_types = ['py','pyc','project','pydevproject']<br />
<br />
def get_data_files(base_dir, target_dir, list=[]):<br />
"""<br />
" * get_data_files<br />
" * base_dir: The full path to the current working directory.<br />
" * target_dir: The directory of assets to include.<br />
" * list: Current list of assets. Used for recursion.<br />
" *<br />
" * returns: A list of relative and full path pairs. This is <br />
" * specified by distutils.<br />
"""<br />
for file in os.listdir(base_dir + target_dir):<br />
<br />
full_path = base_dir + target_dir + file<br />
if os.path.isdir(full_path):<br />
get_data_files(base_dir, target_dir + file + '\\', list)<br />
elif os.path.isfile(full_path):<br />
if (len(file.split('.')) == 2 and file.split('.')[1] not in excluded_file_types):<br />
list.append((target_dir, [full_path]))<br />
<br />
return list<br />
<br />
# The directory of assets to include.<br />
my_files = get_data_files(sys.path[0] + '\\', assets_dir)<br />
<br />
# Build a dictionary of the options we want.<br />
opts = { 'py2exe': {<br />
'ascii':'True',<br />
'excludes':['_ssl','_hashlib'],<br />
'includes' : ['anydbm', 'dbhash'],<br />
'bundle_files':'1',<br />
'compressed':'True'}}<br />
<br />
# Run the setup utility.<br />
setup(console=[target_file],<br />
data_files=my_files,<br />
zipfile=None,<br />
options=opts)<br />
<br />
<br />
</syntaxhighlight><br />
</div><br />
<br />
=== Linux+cx_Freeze ===<br />
[http://cx-freeze.sourceforge.net/ Cx_Freeze] is a set of tools for creating Linux executables.<br />
<br />
<br />
=== Mac+py2app ===<br />
[http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html Py2app] is a set of tools for creating Mac OSX applications.<br />
<br />
== A* Pathfinding ==<br />
The basic tutorial contains a simple pathfinding, but there's a lot of issues: for example the monsters cannot do a diagonal movement into a corridor, and they cannot navigate around other monsters.<br />
<br />
Libtcod contains a pathfinding module that includes a simple A* pathfinding. The following function will fix all these issues.<br />
<br />
First we will fix the AI class BasicMonster. We'll replace the movement function move_towards(player.x, player.y) with move_astar(player) that takes the player as the parameter into which to move.<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
<br />
class BasicMonster:<br />
#AI for a basic monster.<br />
def take_turn(self):<br />
#a basic monster takes its turn. if you can see it, it can see you<br />
monster = self.owner<br />
if libtcod.map_is_in_fov(fov_map, monster.x, monster.y):<br />
<br />
#move towards player if far away<br />
if monster.distance_to(player) >= 2:<br />
monster.move_astar(player)<br />
<br />
#close enough, attack! (if the player is still alive.)<br />
elif player.fighter.hp > 0:<br />
monster.fighter.attack(player)<br />
<br />
</syntaxhighlight><br />
</div><br />
<br />
The second thing is to create this function inside the Object class. The function creates a new path each turn that takes into account other monsters or unwalkable objects (these could be pillars, wells, altars)<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
<br />
def move_astar(self, target):<br />
#Create a FOV map that has the dimensions of the map<br />
fov = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)<br />
<br />
#Scan the current map each turn and set all the walls as unwalkable<br />
for y1 in range(MAP_HEIGHT):<br />
for x1 in range(MAP_WIDTH):<br />
libtcod.map_set_properties(fov, x1, y1, not map[x1][y1].block_sight, not map[x1][y1].blocked)<br />
<br />
#Scan all the objects to see if there are objects that must be navigated around<br />
#Check also that the object isn't self or the target (so that the start and the end points are free)<br />
#The AI class handles the situation if self is next to the target so it will not use this A* function anyway <br />
for obj in objects:<br />
if obj.blocks and obj != self and obj != target:<br />
#Set the tile as a wall so it must be navigated around<br />
libtcod.map_set_properties(fov, obj.x, obj.y, True, False)<br />
<br />
#Allocate a A* path<br />
#The 1.41 is the normal diagonal cost of moving, it can be set as 0.0 if diagonal moves are prohibited<br />
my_path = libtcod.path_new_using_map(fov, 1.41)<br />
<br />
#Compute the path between self's coordinates and the target's coordinates<br />
libtcod.path_compute(my_path, self.x, self.y, target.x, target.y)<br />
<br />
#Check if the path exists, and in this case, also the path is shorter than 25 tiles<br />
#The path size matters if you want the monster to use alternative longer paths (for example through other rooms) if for example the player is in a corridor<br />
#It makes sense to keep path size relatively low to keep the monsters from running around the map if there's an alternative path really far away <br />
if not libtcod.path_is_empty(my_path) and libtcod.path_size(my_path) < 25:<br />
#Find the next coordinates in the computed full path<br />
x, y = libtcod.path_walk(my_path, True)<br />
if x or y:<br />
#Set self's coordinates to the next path tile<br />
self.x = x<br />
self.y = y<br />
else:<br />
#Keep the old move function as a backup so that if there are no paths (for example another monster blocks a corridor)<br />
#it will still try to move towards the player (closer to the corridor opening)<br />
self.move_towards(target.x, target.y) <br />
<br />
#Delete the path to free memory<br />
libtcod.path_delete(my_path)<br />
<br />
</syntaxhighlight><br />
</div><br />
<br />
<br />
== Using Graphical Tiles ==<br />
This tutorial shows how to use graphical tiles instead of ASCII or solid color graphics.<br />
<br />
Here's a sample font file that's used in this tutorial. Save this file as TiledFont.png. The tiles are from Angband's free 16x16 tileset with the font from roarl's nethack font:<br />
<br />
{|<br />
|http://i.imgur.com/hji1UI8.png<br />
|}<br />
<br />
New tiles can be added to that file with an image editor that preserves the transparency.<br />
<br />
There are some things that MUST be taken into account when using tiles with libtcod.<br />
<br />
1. The tile size must be related to the font size. It cannot be smaller than the font size, but it can be larger. If it's larger, it's size must be related to the font size by a multiplier. If the font size is 16x16, you can use 16x16, 16x32, 32x32 etc. sized tiles. Larger tiles must be drawn in multiple chunks of 16x16 if the font size is 16x16.<br />
<br />
2. If the tile is shown as a solid block instead of the tile, it's determined as a greyscale tile. Each tile must have at least one pixel that's RGB values are not the same. It's easiest to change one of the pixels for example from [132, 132, 132] to [132, 132, 133] RGB values. It doesn't look any different, but libtcod interpretes it differently.<br />
<br />
3. The font file must have empty rows at the bottom. If the tiles won't appear, add empty rows to the file.<br />
<br />
4. Tiles are not transparent in one sense, you cannot draw one tile on top of the other so that the bottom tile will be shown too. This results in items not showing floor below them. If you've got only one floor tile, you can actually draw the items to the floor tile so it won't look weird.<br />
<br />
First we will load the font file by modifing the font row at the bottom:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
#The font has 32 chars in a row, and there's a total of 10 rows. Increase the "10" when you add new rows to the sample font file<br />
libtcod.console_set_custom_font('TiledFont.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD, 32, 10)<br />
</syntaxhighlight><br />
</div><br />
<br />
Then we must load the custom rows from the font so the tiles can be used. Be sure also to call it from somewhere at the beginning:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
def load_customfont():<br />
#The index of the first custom tile in the file<br />
a = 256<br />
<br />
#The "y" is the row index, here we load the sixth row in the font file. Increase the "6" to load any new rows from the file<br />
for y in range(5,6):<br />
libtcod.console_map_ascii_codes_to_font(a, 32, 0, y)<br />
a += 32 <br />
</syntaxhighlight><br />
</div><br />
<br />
Then we will add variables to the tiles at where the other constants are declared at the top of the file. Here's all the tiles needed for the tutorial:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
wall_tile = 256 <br />
floor_tile = 257<br />
player_tile = 258<br />
orc_tile = 259<br />
troll_tile = 260<br />
scroll_tile = 261<br />
healingpotion_tile = 262<br />
sword_tile = 263<br />
shield_tile = 264<br />
stairsdown_tile = 265<br />
dagger_tile = 266<br />
</syntaxhighlight><br />
</div><br />
<br />
Now we modify the render_all() function to render the map with the tiles. In the "console_put_char_ex" function the first color is how the tile will be tinted. The next color will tell how the transparent parts will be colored, black is good enough now. We tint the "seen, but not in the FOV" tiles as grey, others will be untinted with white:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
for y in range(MAP_HEIGHT):<br />
for x in range(MAP_WIDTH):<br />
visible = libtcod.map_is_in_fov(fov_map, x, y)<br />
wall = map[x][y].block_sight<br />
if not visible:<br />
#if it's not visible right now, the player can only see it if it's explored<br />
if map[x][y].explored:<br />
if wall:<br />
libtcod.console_put_char_ex(con, x, y, wall_tile, libtcod.grey, libtcod.black)<br />
else:<br />
libtcod.console_put_char_ex(con, x, y, floor_tile, libtcod.grey, libtcod.black)<br />
else:<br />
#it's visible<br />
if wall:<br />
libtcod.console_put_char_ex(con, x, y, wall_tile, libtcod.white, libtcod.black)<br />
else:<br />
libtcod.console_put_char_ex(con, x, y, floor_tile, libtcod.white, libtcod.black)<br />
#since it's visible, explore it<br />
map[x][y].explored = True<br />
</syntaxhighlight><br />
</div><br />
<br />
Then we can modify the actual objects to use the tiles. Here's the "@" changed into player_tile. Remember to also make the color white so it will be untinted:<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
player = Object(0, 0, player_tile, 'player', libtcod.white, blocks=True, fighter=fighter_component)<br />
</syntaxhighlight><br />
</div><br />
<br />
Now you can change the other objects also to use the graphical tiles' variables and then you can enjoy the results!<br />
<br />
{|<br />
|http://i.imgur.com/S95piRG.png<br />
|}<br />
<br />
== BSP Dungeon Generator ==<br />
This tutorial explains how to use the Binary Space Partitioning module in libtcod. It's used for generating dungeons. The advantage in here is that the dungeons generated fill the map completely with rooms, instead of placing random rectangles and digging corridors between them.<br />
<br />
BSP simplified is that a root node is divided into two nodes and then the two resulting nodes will be divided in two and repeated for the DEPTH amount. Then each of the nodes, so called leafs, will be transformed into rooms.<br />
<br />
This tutorial is adapted to fit the main tutorial from the samples provided by libtcod.<br />
<br />
First, add this in the top of the file. It will be used when determining the stairs room and the starting room<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
import random<br />
</syntaxhighlight><br />
</div><br />
<br />
Next we create some constants for use within the dungeon generator. <br />
<br />
DEPTH determines the amount of recursive rooms.<br />
<br />
MIN_SIZE is the smallest room size.<br />
<br />
FULL_ROOMS when False will create random sized rooms, when True it will create rooms that will be largest possible within the node area. It is recommended to use False to create more natural looking dungeons. Using True might be good when adding this into an existing area, for example creating a house indoor areas.<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
DEPTH = 10<br />
MIN_SIZE = 5<br />
FULL_ROOMS = False<br />
</syntaxhighlight><br />
</div><br />
<br />
The following function is the main function that can be called instead of the basic tutorial's make_map() function.<br />
<br />
It essentially creates a dungeon using the BSP module, picks one of the rooms for stairs, and one for the player start, and then for the rest of the rooms adds enemies and items.<br />
<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
def make_bsp():<br />
global map, objects, stairs, bsp_rooms<br />
<br />
objects = [player]<br />
<br />
map = [[Tile(True) for y in range(MAP_HEIGHT)] for x in range(MAP_WIDTH)]<br />
<br />
#Empty global list for storing room coordinates<br />
bsp_rooms = []<br />
<br />
#New root node<br />
bsp = libtcod.bsp_new_with_size(0, 0, MAP_WIDTH, MAP_HEIGHT)<br />
<br />
#Split into nodes<br />
libtcod.bsp_split_recursive(bsp, 0, DEPTH, MIN_SIZE + 1, MIN_SIZE + 1, 1.5, 1.5)<br />
<br />
#Traverse the nodes and create rooms <br />
libtcod.bsp_traverse_inverted_level_order(bsp, traverse_node)<br />
<br />
#Random room for the stairs<br />
stairs_location = random.choice(bsp_rooms)<br />
bsp_rooms.remove(stairs_location)<br />
stairs = Object(stairs_location[0], stairs_location[1], '<', 'stairs', libtcod.white, always_visible=True)<br />
objects.append(stairs)<br />
stairs.send_to_back()<br />
<br />
#Random room for player start<br />
player_room = random.choice(bsp_rooms)<br />
bsp_rooms.remove(player_room)<br />
player.x = player_room[0]<br />
player.y = player_room[1]<br />
<br />
#Add monsters and items<br />
for room in bsp_rooms:<br />
new_room = Rect(room[0], room[1], 2, 2)<br />
place_objects(new_room)<br />
<br />
initialize_fov()<br />
</syntaxhighlight><br />
</div><br />
<br />
This function is where the BSP stuff happens<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"> <br />
def traverse_node(node, dat):<br />
global map, bsp_rooms<br />
<br />
#Create rooms<br />
if libtcod.bsp_is_leaf(node):<br />
minx = node.x + 1<br />
maxx = node.x + node.w - 1<br />
miny = node.y + 1<br />
maxy = node.y + node.h - 1<br />
<br />
if maxx == MAP_WIDTH - 1:<br />
maxx -= 1<br />
if maxy == MAP_HEIGHT - 1:<br />
maxy -= 1<br />
<br />
#If it's False the rooms sizes are random, else the rooms are filled to the node's size<br />
if FULL_ROOMS == False:<br />
minx = libtcod.random_get_int(None, minx, maxx - MIN_SIZE + 1)<br />
miny = libtcod.random_get_int(None, miny, maxy - MIN_SIZE + 1)<br />
maxx = libtcod.random_get_int(None, minx + MIN_SIZE - 2, maxx)<br />
maxy = libtcod.random_get_int(None, miny + MIN_SIZE - 2, maxy)<br />
<br />
node.x = minx<br />
node.y = miny<br />
node.w = maxx-minx + 1<br />
node.h = maxy-miny + 1<br />
<br />
#Dig room<br />
for x in range(minx, maxx + 1):<br />
for y in range(miny, maxy + 1):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
#Add center coordinates to the list of rooms<br />
bsp_rooms.append(((minx + maxx) / 2, (miny + maxy) / 2))<br />
<br />
#Create corridors <br />
else:<br />
left = libtcod.bsp_left(node)<br />
right = libtcod.bsp_right(node)<br />
node.x = min(left.x, right.x)<br />
node.y = min(left.y, right.y)<br />
node.w = max(left.x + left.w, right.x + right.w) - node.x<br />
node.h = max(left.y + left.h, right.y + right.h) - node.y<br />
if node.horizontal:<br />
if left.x + left.w - 1 < right.x or right.x + right.w - 1 < left.x:<br />
x1 = libtcod.random_get_int(None, left.x, left.x + left.w - 1)<br />
x2 = libtcod.random_get_int(None, right.x, right.x + right.w - 1)<br />
y = libtcod.random_get_int(None, left.y + left.h, right.y)<br />
vline_up(map, x1, y - 1)<br />
hline(map, x1, y, x2)<br />
vline_down(map, x2, y + 1)<br />
<br />
else:<br />
minx = max(left.x, right.x)<br />
maxx = min(left.x + left.w - 1, right.x + right.w - 1)<br />
x = libtcod.random_get_int(None, minx, maxx)<br />
<br />
# catch out-of-bounds attempts<br />
while x > MAP_WIDTH - 1:<br />
x -= 1<br />
<br />
vline_down(map, x, right.y)<br />
vline_up(map, x, right.y - 1)<br />
<br />
else:<br />
if left.y + left.h - 1 < right.y or right.y + right.h - 1 < left.y:<br />
y1 = libtcod.random_get_int(None, left.y, left.y + left.h - 1)<br />
y2 = libtcod.random_get_int(None, right.y, right.y + right.h - 1)<br />
x = libtcod.random_get_int(None, left.x + left.w, right.x)<br />
hline_left(map, x - 1, y1)<br />
vline(map, x, y1, y2)<br />
hline_right(map, x + 1, y2)<br />
else:<br />
miny = max(left.y, right.y)<br />
maxy = min(left.y + left.h - 1, right.y + right.h - 1)<br />
y = libtcod.random_get_int(None, miny, maxy)<br />
<br />
# catch out-of-bounds attempts<br />
while y > MAP_HEIGHT - 1:<br />
y -= 1<br />
<br />
hline_left(map, right.x - 1, y)<br />
hline_right(map, right.x, y)<br />
<br />
return True<br />
</syntaxhighlight><br />
</div><br />
<br />
All the corridor diggings are made within these for every direction<br />
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><br />
<syntaxhighlight lang="python"><br />
def vline(map, x, y1, y2):<br />
if y1 > y2:<br />
y1,y2 = y2,y1<br />
<br />
for y in range(y1,y2+1):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
def vline_up(map, x, y):<br />
while y >= 0 and map[x][y].blocked == True:<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
y -= 1<br />
<br />
def vline_down(map, x, y):<br />
while y < MAP_HEIGHT and map[x][y].blocked == True:<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
y += 1<br />
<br />
def hline(map, x1, y, x2):<br />
if x1 > x2:<br />
x1,x2 = x2,x1<br />
for x in range(x1,x2+1):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
def hline_left(map, x, y):<br />
while x >= 0 and map[x][y].blocked == True:<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
x -= 1<br />
<br />
def hline_right(map, x, y):<br />
while x < MAP_WIDTH and map[x][y].blocked == True:<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
x += 1<br />
</syntaxhighlight><br />
</div><br />
<br />
{|<br />
|http://i.imgur.com/XP2oe0v.png<br />
|http://i.imgur.com/GpCNShI.png<br />
|-<br />
|FULL_ROOMS = True<br />
|FULL_ROOMS = False<br />
|}<br />
<br />
[[Category:Developing]]</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_part_13_code&diff=47057Complete Roguelike Tutorial, using python3+libtcod, part 13 code2018-05-08T00:57:37Z<p>Bix: /* Adventure gear */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
<br />
== Adventure gear ==<br />
<div style="background-color: #EEEEEE; border-style: dotted"><syntaxhighlight lang="python"><br />
# !/usr/bin/env python3<br />
#<br />
# libtcod python tutorial<br />
#<br />
<br />
import math<br />
import shelve<br />
import textwrap<br />
<br />
import libtcodpy as libtcod<br />
<br />
# actual size of the window<br />
SCREEN_WIDTH = 80<br />
SCREEN_HEIGHT = 50<br />
<br />
# size of the map<br />
MAP_WIDTH = 80<br />
MAP_HEIGHT = 43<br />
<br />
# sizes and coordinates relevant for the GUI<br />
BAR_WIDTH = 20<br />
PANEL_HEIGHT = 7<br />
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT<br />
MSG_X = BAR_WIDTH + 2<br />
MSG_WIDTH = SCREEN_WIDTH - BAR_WIDTH - 2<br />
MSG_HEIGHT = PANEL_HEIGHT - 1<br />
INVENTORY_WIDTH = 50<br />
CHARACTER_SCREEN_WIDTH = 30<br />
LEVEL_SCREEN_WIDTH = 40<br />
<br />
# parameters for dungeon generator<br />
ROOM_MAX_SIZE = 10<br />
ROOM_MIN_SIZE = 6<br />
MAX_ROOMS = 30<br />
<br />
# spell values<br />
HEAL_AMOUNT = 40<br />
LIGHTNING_DAMAGE = 40<br />
LIGHTNING_RANGE = 5<br />
CONFUSE_RANGE = 8<br />
CONFUSE_NUM_TURNS = 10<br />
FIREBALL_RADIUS = 3<br />
FIREBALL_DAMAGE = 25<br />
<br />
# experience and level-ups<br />
LEVEL_UP_BASE = 200<br />
LEVEL_UP_FACTOR = 150<br />
<br />
<br />
FOV_ALGO = 0 # default FOV algorithm<br />
FOV_LIGHT_WALLS = True # light walls or not<br />
TORCH_RADIUS = 10<br />
<br />
LIMIT_FPS = 20 # 20 frames-per-second maximum<br />
<br />
<br />
color_dark_wall = libtcod.Color(0, 0, 100)<br />
color_light_wall = libtcod.Color(130, 110, 50)<br />
color_dark_ground = libtcod.Color(50, 50, 150)<br />
color_light_ground = libtcod.Color(200, 180, 50)<br />
<br />
<br />
class Tile:<br />
# a tile of the map and its properties<br />
def __init__(self, blocked, block_sight=None):<br />
self.blocked = blocked<br />
<br />
# all tiles start unexplored<br />
self.explored = False<br />
<br />
# by default, if a tile is blocked, it also blocks sight<br />
if block_sight is None:<br />
block_sight = blocked<br />
self.block_sight = block_sight<br />
<br />
<br />
class Rect:<br />
# a rectangle on the map. used to characterize a room.<br />
def __init__(self, x, y, w, h):<br />
self.x1 = x<br />
self.y1 = y<br />
self.x2 = x + w<br />
self.y2 = y + h<br />
<br />
def center(self):<br />
center_x = (self.x1 + self.x2) // 2<br />
center_y = (self.y1 + self.y2) // 2<br />
return (center_x, center_y)<br />
<br />
def intersect(self, other):<br />
# returns true if this rectangle intersects with another one<br />
return (self.x1 <= other.x2 and self.x2 >= other.x1 and<br />
self.y1 <= other.y2 and self.y2 >= other.y1)<br />
<br />
<br />
class Object:<br />
# this is a generic object: the player, a monster, an item, the stairs...<br />
# it's always represented by a character on screen.<br />
def __init__(self, x, y, char, name, color, blocks=False, always_visible=False, fighter=None, ai=None, item=None, equipment=None):<br />
self.x = x<br />
self.y = y<br />
self.char = char<br />
self.name = name<br />
self.color = color<br />
self.blocks = blocks<br />
self.always_visible = always_visible<br />
self.fighter = fighter<br />
if self.fighter: # let the fighter component know who owns it<br />
self.fighter.owner = self<br />
<br />
self.ai = ai<br />
if self.ai: # let the AI component know who owns it<br />
self.ai.owner = self<br />
<br />
self.item = item<br />
if self.item: # let the Item component know who owns it<br />
self.item.owner = self<br />
<br />
self.equipment = equipment<br />
if self.equipment: # let the Equipment component know who owns it<br />
self.equipment.owner = self<br />
<br />
# there must be an Item component for the Equipment component to work properly<br />
self.item = Item()<br />
self.item.owner = self<br />
<br />
def move(self, dx, dy):<br />
# move by the given amount, if the destination is not blocked<br />
if not is_blocked(self.x + dx, self.y + dy):<br />
self.x += dx<br />
self.y += dy<br />
<br />
def move_towards(self, target_x, target_y):<br />
# vector from this object to the target, and distance<br />
dx = target_x - self.x<br />
dy = target_y - self.y<br />
distance = math.sqrt(dx ** 2 + dy ** 2)<br />
<br />
# normalize it to length 1 (preserving direction), then round it and<br />
# convert to integer so the movement is restricted to the map grid<br />
dx = int(round(dx // distance))<br />
dy = int(round(dy // distance))<br />
self.move(dx, dy)<br />
<br />
def distance_to(self, other):<br />
# return the distance to another object<br />
dx = other.x - self.x<br />
dy = other.y - self.y<br />
return math.sqrt(dx ** 2 + dy ** 2)<br />
<br />
def distance(self, x, y):<br />
# return the distance to some coordinates<br />
return math.sqrt((x - self.x) ** 2 + (y - self.y) ** 2)<br />
<br />
def send_to_back(self):<br />
# make this object be drawn first, so all others appear above it if they're in the same tile.<br />
global objects<br />
objects.remove(self)<br />
objects.insert(0, self)<br />
<br />
def draw(self):<br />
# only show if it's visible to the player; or it's set to "always visible" and on an explored tile<br />
if (libtcod.map_is_in_fov(fov_map, self.x, self.y) or<br />
(self.always_visible and map[self.x][self.y].explored)):<br />
# set the color and then draw the character that represents this object at its position<br />
libtcod.console_set_default_foreground(con, self.color)<br />
libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)<br />
<br />
def clear(self):<br />
# erase the character that represents this object<br />
libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)<br />
<br />
<br />
class Fighter:<br />
# combat-related properties and methods (monster, player, NPC).<br />
def __init__(self, hp, defense, power, xp, death_function=None):<br />
self.base_max_hp = hp<br />
self.hp = hp<br />
self.base_defense = defense<br />
self.base_power = power<br />
self.xp = xp<br />
self.death_function = death_function<br />
<br />
@property<br />
def power(self): # return actual power, by summing up the bonuses from all equipped items<br />
bonus = sum(equipment.power_bonus for equipment in get_all_equipped(self.owner))<br />
return self.base_power + bonus<br />
<br />
@property<br />
def defense(self): # return actual defense, by summing up the bonuses from all equipped items<br />
bonus = sum(equipment.defense_bonus for equipment in get_all_equipped(self.owner))<br />
return self.base_defense + bonus<br />
<br />
@property<br />
def max_hp(self): # return actual max_hp, by summing up the bonuses from all equipped items<br />
bonus = sum(equipment.max_hp_bonus for equipment in get_all_equipped(self.owner))<br />
return self.base_max_hp + bonus<br />
<br />
def attack(self, target):<br />
# a simple formula for attack damage<br />
damage = self.power - target.fighter.defense<br />
<br />
if damage > 0:<br />
# make the target take some damage<br />
message(self.owner.name.capitalize() + ' attacks ' + target.name + ' for ' + str(damage) + ' hit points.')<br />
target.fighter.take_damage(damage)<br />
else:<br />
message(self.owner.name.capitalize() + ' attacks ' + target.name + ' but it has no effect!')<br />
<br />
def take_damage(self, damage):<br />
# apply damage if possible<br />
if damage > 0:<br />
self.hp -= damage<br />
<br />
# check for death. if there's a death function, call it<br />
if self.hp <= 0:<br />
function = self.death_function<br />
if function is not None:<br />
function(self.owner)<br />
<br />
if self.owner != player: # yield experience to the player<br />
player.fighter.xp += self.xp<br />
<br />
def heal(self, amount):<br />
# heal by the given amount, without going over the maximum<br />
self.hp += amount<br />
if self.hp > self.max_hp:<br />
self.hp = self.max_hp<br />
<br />
<br />
class BasicMonster:<br />
# AI for a basic monster.<br />
def take_turn(self):<br />
# a basic monster takes its turn. if you can see it, it can see you<br />
monster = self.owner<br />
if libtcod.map_is_in_fov(fov_map, monster.x, monster.y):<br />
<br />
# move towards player if far away<br />
if monster.distance_to(player) >= 2:<br />
monster.move_towards(player.x, player.y)<br />
<br />
# close enough, attack! (if the player is still alive.)<br />
elif player.fighter.hp > 0:<br />
monster.fighter.attack(player)<br />
<br />
<br />
class ConfusedMonster:<br />
# AI for a temporarily confused monster (reverts to previous AI after a while).<br />
def __init__(self, old_ai, num_turns=CONFUSE_NUM_TURNS):<br />
self.old_ai = old_ai<br />
self.num_turns = num_turns<br />
<br />
def take_turn(self):<br />
if self.num_turns > 0: # still confused...<br />
# move in a random direction, and decrease the number of turns confused<br />
self.owner.move(libtcod.random_get_int(0, -1, 1), libtcod.random_get_int(0, -1, 1))<br />
self.num_turns -= 1<br />
<br />
else: # restore the previous AI (this one will be deleted because it's not referenced anymore)<br />
self.owner.ai = self.old_ai<br />
message('The ' + self.owner.name + ' is no longer confused!', libtcod.red)<br />
<br />
<br />
class Item:<br />
# an item that can be picked up and used.<br />
def __init__(self, use_function=None):<br />
self.use_function = use_function<br />
<br />
def pick_up(self):<br />
# add to the player's inventory and remove from the map<br />
if len(inventory) >= 26:<br />
message('Your inventory is full, cannot pick up ' + self.owner.name + '.', libtcod.red)<br />
else:<br />
inventory.append(self.owner)<br />
objects.remove(self.owner)<br />
message('You picked up a ' + self.owner.name + '!', libtcod.green)<br />
<br />
# special case: automatically equip, if the corresponding equipment slot is unused<br />
equipment = self.owner.equipment<br />
if equipment and get_equipped_in_slot(equipment.slot) is None:<br />
equipment.equip()<br />
<br />
def drop(self):<br />
# special case: if the object has the Equipment component, dequip it before dropping<br />
if self.owner.equipment:<br />
self.owner.equipment.dequip()<br />
<br />
# add to the map and remove from the player's inventory. also, place it at the player's coordinates<br />
objects.append(self.owner)<br />
inventory.remove(self.owner)<br />
self.owner.x = player.x<br />
self.owner.y = player.y<br />
message('You dropped a ' + self.owner.name + '.', libtcod.yellow)<br />
<br />
def use(self):<br />
# special case: if the object has the Equipment component, the "use" action is to equip/dequip<br />
if self.owner.equipment:<br />
self.owner.equipment.toggle_equip()<br />
return<br />
<br />
# just call the "use_function" if it is defined<br />
if self.use_function is None:<br />
message('The ' + self.owner.name + ' cannot be used.')<br />
else:<br />
if self.use_function() != 'cancelled':<br />
inventory.remove(self.owner) # destroy after use, unless it was cancelled for some reason<br />
<br />
<br />
class Equipment:<br />
# an object that can be equipped, yielding bonuses. automatically adds the Item component.<br />
def __init__(self, slot, power_bonus=0, defense_bonus=0, max_hp_bonus=0):<br />
self.power_bonus = power_bonus<br />
self.defense_bonus = defense_bonus<br />
self.max_hp_bonus = max_hp_bonus<br />
<br />
self.slot = slot<br />
self.is_equipped = False<br />
<br />
def toggle_equip(self): # toggle equip/dequip status<br />
if self.is_equipped:<br />
self.dequip()<br />
else:<br />
self.equip()<br />
<br />
def equip(self):<br />
# if the slot is already being used, dequip whatever is there first<br />
old_equipment = get_equipped_in_slot(self.slot)<br />
if old_equipment is not None:<br />
old_equipment.dequip()<br />
<br />
# equip object and show a message about it<br />
self.is_equipped = True<br />
message('Equipped ' + self.owner.name + ' on ' + self.slot + '.', libtcod.light_green)<br />
<br />
def dequip(self):<br />
# dequip object and show a message about it<br />
if not self.is_equipped:<br />
return<br />
self.is_equipped = False<br />
message('Dequipped ' + self.owner.name + ' from ' + self.slot + '.', libtcod.light_yellow)<br />
<br />
<br />
def get_equipped_in_slot(slot): # returns the equipment in a slot, or None if it's empty<br />
for obj in inventory:<br />
if obj.equipment and obj.equipment.slot == slot and obj.equipment.is_equipped:<br />
return obj.equipment<br />
return None<br />
<br />
<br />
def get_all_equipped(obj): # returns a list of equipped items<br />
if obj == player:<br />
equipped_list = []<br />
for item in inventory:<br />
if item.equipment and item.equipment.is_equipped:<br />
equipped_list.append(item.equipment)<br />
return equipped_list<br />
else:<br />
return [] # other objects have no equipment<br />
<br />
<br />
def is_blocked(x, y):<br />
# first test the map tile<br />
if map[x][y].blocked:<br />
return True<br />
<br />
# now check for any blocking objects<br />
for object in objects:<br />
if object.blocks and object.x == x and object.y == y:<br />
return True<br />
<br />
return False<br />
<br />
<br />
def create_room(room):<br />
global map<br />
# go through the tiles in the rectangle and make them passable<br />
for x in range(room.x1 + 1, room.x2):<br />
for y in range(room.y1 + 1, room.y2):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
<br />
def create_h_tunnel(x1, x2, y):<br />
global map<br />
# horizontal tunnel. min() and max() are used in case x1>x2<br />
for x in range(min(x1, x2), max(x1, x2) + 1):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
<br />
def create_v_tunnel(y1, y2, x):<br />
global map<br />
# vertical tunnel<br />
for y in range(min(y1, y2), max(y1, y2) + 1):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
<br />
def make_map():<br />
global map, objects, stairs<br />
<br />
# the list of objects with just the player<br />
objects = [player]<br />
<br />
# fill map with "blocked" tiles<br />
map = [[Tile(True)<br />
for y in range(MAP_HEIGHT)]<br />
for x in range(MAP_WIDTH)]<br />
<br />
rooms = []<br />
num_rooms = 0<br />
<br />
for r in range(MAX_ROOMS):<br />
# random width and height<br />
w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)<br />
h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)<br />
# random position without going out of the boundaries of the map<br />
x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1)<br />
y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1)<br />
<br />
# "Rect" class makes rectangles easier to work with<br />
new_room = Rect(x, y, w, h)<br />
<br />
# run through the other rooms and see if they intersect with this one<br />
failed = False<br />
for other_room in rooms:<br />
if new_room.intersect(other_room):<br />
failed = True<br />
break<br />
<br />
if not failed:<br />
# this means there are no intersections, so this room is valid<br />
<br />
# "paint" it to the map's tiles<br />
create_room(new_room)<br />
<br />
# center coordinates of new room, will be useful later<br />
(new_x, new_y) = new_room.center()<br />
<br />
if num_rooms == 0:<br />
# this is the first room, where the player starts at<br />
player.x = new_x<br />
player.y = new_y<br />
else:<br />
# all rooms after the first:<br />
# connect it to the previous room with a tunnel<br />
<br />
# center coordinates of previous room<br />
(prev_x, prev_y) = rooms[num_rooms - 1].center()<br />
<br />
# draw a coin (random number that is either 0 or 1)<br />
if libtcod.random_get_int(0, 0, 1) == 1:<br />
# first move horizontally, then vertically<br />
create_h_tunnel(prev_x, new_x, prev_y)<br />
create_v_tunnel(prev_y, new_y, new_x)<br />
else:<br />
# first move vertically, then horizontally<br />
create_v_tunnel(prev_y, new_y, prev_x)<br />
create_h_tunnel(prev_x, new_x, new_y)<br />
<br />
# add some contents to this room, such as monsters<br />
place_objects(new_room)<br />
<br />
# finally, append the new room to the list<br />
rooms.append(new_room)<br />
num_rooms += 1<br />
<br />
# create stairs at the center of the last room<br />
stairs = Object(new_x, new_y, '<', 'stairs', libtcod.white, always_visible=True)<br />
objects.append(stairs)<br />
stairs.send_to_back() # so it's drawn below the monsters<br />
<br />
<br />
def random_choice_index(chances): # choose one option from list of chances, returning its index<br />
# the dice will land on some number between 1 and the sum of the chances<br />
dice = libtcod.random_get_int(0, 1, sum(chances))<br />
<br />
# go through all chances, keeping the sum so far<br />
running_sum = 0<br />
choice = 0<br />
for w in chances:<br />
running_sum += w<br />
<br />
# see if the dice landed in the part that corresponds to this choice<br />
if dice <= running_sum:<br />
return choice<br />
choice += 1<br />
<br />
<br />
def random_choice(chances_dict):<br />
# choose one option from dictionary of chances, returning its key<br />
chances = list(chances_dict.values())<br />
strings = list(chances_dict.keys())<br />
<br />
return strings[random_choice_index(chances)]<br />
<br />
<br />
def from_dungeon_level(table):<br />
# returns a value that depends on level. the table specifies what value occurs after each level, default is 0.<br />
for (value, level) in reversed(table):<br />
if dungeon_level >= level:<br />
return value<br />
return 0<br />
<br />
<br />
def place_objects(room):<br />
# this is where we decide the chance of each monster or item appearing.<br />
<br />
# maximum number of monsters per room<br />
max_monsters = from_dungeon_level([[2, 1], [3, 4], [5, 6]])<br />
<br />
# chance of each monster<br />
monster_chances = {}<br />
monster_chances['orc'] = 80 # orc always shows up, even if all other monsters have 0 chance<br />
monster_chances['troll'] = from_dungeon_level([[15, 3], [30, 5], [60, 7]])<br />
<br />
# maximum number of items per room<br />
max_items = from_dungeon_level([[1, 1], [2, 4]])<br />
<br />
# chance of each item (by default they have a chance of 0 at level 1, which then goes up)<br />
item_chances = {}<br />
item_chances['heal'] = 35 # healing potion always shows up, even if all other items have 0 chance<br />
item_chances['lightning'] = from_dungeon_level([[25, 4]])<br />
item_chances['fireball'] = from_dungeon_level([[25, 6]])<br />
item_chances['confuse'] = from_dungeon_level([[10, 2]])<br />
item_chances['sword'] = from_dungeon_level([[5, 4]])<br />
item_chances['shield'] = from_dungeon_level([[15, 8]])<br />
<br />
# choose random number of monsters<br />
num_monsters = libtcod.random_get_int(0, 0, max_monsters)<br />
<br />
for i in range(num_monsters):<br />
# choose random spot for this monster<br />
x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1)<br />
y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1)<br />
<br />
# only place it if the tile is not blocked<br />
if not is_blocked(x, y):<br />
choice = random_choice(monster_chances)<br />
if choice == 'orc':<br />
# create an orc<br />
fighter_component = Fighter(hp=20, defense=0, power=4, xp=35, death_function=monster_death)<br />
ai_component = BasicMonster()<br />
<br />
monster = Object(x, y, 'o', 'orc', libtcod.desaturated_green,<br />
blocks=True, fighter=fighter_component, ai=ai_component)<br />
<br />
elif choice == 'troll':<br />
# create a troll<br />
fighter_component = Fighter(hp=30, defense=2, power=8, xp=100, death_function=monster_death)<br />
ai_component = BasicMonster()<br />
<br />
monster = Object(x, y, 'T', 'troll', libtcod.darker_green,<br />
blocks=True, fighter=fighter_component, ai=ai_component)<br />
<br />
objects.append(monster)<br />
<br />
# choose random number of items<br />
num_items = libtcod.random_get_int(0, 0, max_items)<br />
<br />
for i in range(num_items):<br />
# choose random spot for this item<br />
x = libtcod.random_get_int(0, room.x1 + 1, room.x2 - 1)<br />
y = libtcod.random_get_int(0, room.y1 + 1, room.y2 - 1)<br />
<br />
# only place it if the tile is not blocked<br />
if not is_blocked(x, y):<br />
choice = random_choice(item_chances)<br />
if choice == 'heal':<br />
# create a healing potion<br />
item_component = Item(use_function=cast_heal)<br />
item = Object(x, y, '!', 'healing potion', libtcod.violet, item=item_component)<br />
<br />
elif choice == 'lightning':<br />
# create a lightning bolt scroll<br />
item_component = Item(use_function=cast_lightning)<br />
item = Object(x, y, '# ', 'scroll of lightning bolt', libtcod.light_yellow, item=item_component)<br />
<br />
elif choice == 'fireball':<br />
# create a fireball scroll<br />
item_component = Item(use_function=cast_fireball)<br />
item = Object(x, y, '# ', 'scroll of fireball', libtcod.light_yellow, item=item_component)<br />
<br />
elif choice == 'confuse':<br />
# create a confuse scroll<br />
item_component = Item(use_function=cast_confuse)<br />
item = Object(x, y, '# ', 'scroll of confusion', libtcod.light_yellow, item=item_component)<br />
<br />
elif choice == 'sword':<br />
# create a sword<br />
equipment_component = Equipment(slot='right hand', power_bonus=3)<br />
item = Object(x, y, '/', 'sword', libtcod.sky, equipment=equipment_component)<br />
<br />
elif choice == 'shield':<br />
# create a shield<br />
equipment_component = Equipment(slot='left hand', defense_bonus=1)<br />
item = Object(x, y, '[', 'shield', libtcod.darker_orange, equipment=equipment_component)<br />
<br />
objects.append(item)<br />
item.send_to_back() # items appear below other objects<br />
item.always_visible = True # items are visible even out-of-FOV, if in an explored area<br />
<br />
<br />
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color):<br />
# render a bar (HP, experience, etc). first calculate the width of the bar<br />
bar_width = int(float(value) // maximum * total_width)<br />
<br />
# render the background first<br />
libtcod.console_set_default_background(panel, back_color)<br />
libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN)<br />
<br />
# now render the bar on top<br />
libtcod.console_set_default_background(panel, bar_color)<br />
if bar_width > 0:<br />
libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN)<br />
<br />
# finally, some centered text with the values<br />
libtcod.console_set_default_foreground(panel, libtcod.white)<br />
libtcod.console_print_ex(panel, x + total_width // 2, y, libtcod.BKGND_NONE, libtcod.CENTER,<br />
name + ': ' + str(value) + '/' + str(maximum))<br />
<br />
<br />
def get_names_under_mouse():<br />
global mouse<br />
# return a string with the names of all objects under the mouse<br />
<br />
(x, y) = (mouse.cx, mouse.cy)<br />
<br />
# create a list with the names of all objects at the mouse's coordinates and in FOV<br />
names = [obj.name for obj in objects<br />
if obj.x == x and obj.y == y and libtcod.map_is_in_fov(fov_map, obj.x, obj.y)]<br />
<br />
names = ', '.join(names) # join the names, separated by commas<br />
return names.capitalize()<br />
<br />
<br />
def render_all():<br />
global fov_map, color_dark_wall, color_light_wall<br />
global color_dark_ground, color_light_ground<br />
global fov_recompute<br />
<br />
if fov_recompute:<br />
# recompute FOV if needed (the player moved or something)<br />
fov_recompute = False<br />
libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)<br />
<br />
# go through all tiles, and set their background color according to the FOV<br />
for y in range(MAP_HEIGHT):<br />
for x in range(MAP_WIDTH):<br />
visible = libtcod.map_is_in_fov(fov_map, x, y)<br />
wall = map[x][y].block_sight<br />
if not visible:<br />
# if it's not visible right now, the player can only see it if it's explored<br />
if map[x][y].explored:<br />
if wall:<br />
libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET)<br />
else:<br />
libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET)<br />
else:<br />
# it's visible<br />
if wall:<br />
libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET)<br />
else:<br />
libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET)<br />
# since it's visible, explore it<br />
map[x][y].explored = True<br />
<br />
# draw all objects in the list, except the player. we want it to<br />
# always appear over all other objects! so it's drawn later.<br />
for object in objects:<br />
if object != player:<br />
object.draw()<br />
player.draw()<br />
<br />
# blit the contents of "con" to the root console<br />
libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0)<br />
<br />
# prepare to render the GUI panel<br />
libtcod.console_set_default_background(panel, libtcod.black)<br />
libtcod.console_clear(panel)<br />
<br />
# print the game messages, one line at a time<br />
y = 1<br />
for (line, color) in game_msgs:<br />
libtcod.console_set_default_foreground(panel, color)<br />
libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT, line)<br />
y += 1<br />
<br />
# show the player's stats<br />
render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp,<br />
libtcod.light_red, libtcod.darker_red)<br />
libtcod.console_print_ex(panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Dungeon level ' + str(dungeon_level))<br />
<br />
# display names of objects under the mouse<br />
libtcod.console_set_default_foreground(panel, libtcod.light_gray)<br />
libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse())<br />
<br />
# blit the contents of "panel" to the root console<br />
libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)<br />
<br />
<br />
def message(new_msg, color=libtcod.white):<br />
# split the message if necessary, among multiple lines<br />
new_msg_lines = textwrap.wrap(new_msg, MSG_WIDTH)<br />
<br />
for line in new_msg_lines:<br />
# if the buffer is full, remove the first line to make room for the new one<br />
if len(game_msgs) == MSG_HEIGHT:<br />
del game_msgs[0]<br />
<br />
# add the new line as a tuple, with the text and the color<br />
game_msgs.append((line, color))<br />
<br />
<br />
def player_move_or_attack(dx, dy):<br />
global fov_recompute<br />
<br />
# the coordinates the player is moving to/attacking<br />
x = player.x + dx<br />
y = player.y + dy<br />
<br />
# try to find an attackable object there<br />
target = None<br />
for object in objects:<br />
if object.fighter and object.x == x and object.y == y:<br />
target = object<br />
break<br />
<br />
# attack if target found, move otherwise<br />
if target is not None:<br />
player.fighter.attack(target)<br />
else:<br />
player.move(dx, dy)<br />
fov_recompute = True<br />
<br />
<br />
def menu(header, options, width):<br />
if len(options) > 26:<br />
raise ValueError('Cannot have a menu with more than 26 options.')<br />
<br />
# calculate total height for the header (after auto-wrap) and one line per option<br />
header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header)<br />
if header == '':<br />
header_height = 0<br />
height = len(options) + header_height<br />
<br />
# create an off-screen console that represents the menu's window<br />
window = libtcod.console_new(width, height)<br />
<br />
# print the header, with auto-wrap<br />
libtcod.console_set_default_foreground(window, libtcod.white)<br />
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)<br />
<br />
# print all the options<br />
y = header_height<br />
letter_index = ord('a')<br />
for option_text in options:<br />
text = '(' + chr(letter_index) + ') ' + option_text<br />
libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text)<br />
y += 1<br />
letter_index += 1<br />
<br />
# blit the contents of "window" to the root console<br />
x = SCREEN_WIDTH // 2 - width // 2<br />
y = SCREEN_HEIGHT // 2 - height // 2<br />
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)<br />
<br />
# present the root console to the player and wait for a key-press<br />
libtcod.console_flush()<br />
key = libtcod.console_wait_for_keypress(True)<br />
<br />
if key.vk == libtcod.KEY_ENTER and key.lalt: # (special case) Alt+Enter: toggle fullscreen<br />
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen)<br />
<br />
# convert the ASCII code to an index; if it corresponds to an option, return it<br />
index = key.c - ord('a')<br />
if index >= 0 and index < len(options):<br />
return index<br />
return None<br />
<br />
<br />
def inventory_menu(header):<br />
# show a menu with each item of the inventory as an option<br />
if len(inventory) == 0:<br />
options = ['Inventory is empty.']<br />
else:<br />
options = []<br />
for item in inventory:<br />
text = item.name<br />
# show additional information, in case it's equipped<br />
if item.equipment and item.equipment.is_equipped:<br />
text = text + ' (on ' + item.equipment.slot + ')'<br />
options.append(text)<br />
<br />
index = menu(header, options, INVENTORY_WIDTH)<br />
<br />
# if an item was chosen, return it<br />
if index is None or len(inventory) == 0:<br />
return None<br />
return inventory[index].item<br />
<br />
<br />
def msgbox(text, width=50):<br />
menu(text, [], width) # use menu() as a sort of "message box"<br />
<br />
<br />
def handle_keys():<br />
global key<br />
<br />
if key.vk == libtcod.KEY_ENTER and key.lalt:<br />
# Alt+Enter: toggle fullscreen<br />
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())<br />
<br />
elif key.vk == libtcod.KEY_ESCAPE:<br />
return 'exit' # exit game<br />
<br />
if game_state == 'playing':<br />
# movement keys<br />
if key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_KP8:<br />
player_move_or_attack(0, -1)<br />
elif key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_KP2:<br />
player_move_or_attack(0, 1)<br />
elif key.vk == libtcod.KEY_LEFT or key.vk == libtcod.KEY_KP4:<br />
player_move_or_attack(-1, 0)<br />
elif key.vk == libtcod.KEY_RIGHT or key.vk == libtcod.KEY_KP6:<br />
player_move_or_attack(1, 0)<br />
elif key.vk == libtcod.KEY_HOME or key.vk == libtcod.KEY_KP7:<br />
player_move_or_attack(-1, -1)<br />
elif key.vk == libtcod.KEY_PAGEUP or key.vk == libtcod.KEY_KP9:<br />
player_move_or_attack(1, -1)<br />
elif key.vk == libtcod.KEY_END or key.vk == libtcod.KEY_KP1:<br />
player_move_or_attack(-1, 1)<br />
elif key.vk == libtcod.KEY_PAGEDOWN or key.vk == libtcod.KEY_KP3:<br />
player_move_or_attack(1, 1)<br />
elif key.vk == libtcod.KEY_KP5:<br />
pass # do nothing ie wait for the monster to come to you<br />
else:<br />
# test for other keys<br />
key_char = chr(key.c)<br />
<br />
if key_char == 'g':<br />
# pick up an item<br />
for object in objects: # look for an item in the player's tile<br />
if object.x == player.x and object.y == player.y and object.item:<br />
object.item.pick_up()<br />
break<br />
<br />
if key_char == 'i':<br />
# show the inventory; if an item is selected, use it<br />
chosen_item = inventory_menu('Press the key next to an item to use it, or any other to cancel.\n')<br />
if chosen_item is not None:<br />
chosen_item.use()<br />
<br />
if key_char == 'd':<br />
# show the inventory; if an item is selected, drop it<br />
chosen_item = inventory_menu('Press the key next to an item to drop it, or any other to cancel.\n')<br />
if chosen_item is not None:<br />
chosen_item.drop()<br />
<br />
if key_char == 'c':<br />
# show character information<br />
level_up_xp = LEVEL_UP_BASE + player.level * LEVEL_UP_FACTOR<br />
msgbox('Character Information\n\nLevel: ' + str(player.level) + '\nExperience: ' + str(player.fighter.xp) +<br />
'\nExperience to level up: ' + str(level_up_xp) + '\n\nMaximum HP: ' + str(player.fighter.max_hp) +<br />
'\nAttack: ' + str(player.fighter.power) + '\nDefense: ' + str(player.fighter.defense), CHARACTER_SCREEN_WIDTH)<br />
<br />
if key_char == '<':<br />
# go down stairs, if the player is on them<br />
if stairs.x == player.x and stairs.y == player.y:<br />
next_level()<br />
<br />
return 'didnt-take-turn'<br />
<br />
<br />
def check_level_up():<br />
# see if the player's experience is enough to level-up<br />
level_up_xp = LEVEL_UP_BASE + player.level * LEVEL_UP_FACTOR<br />
if player.fighter.xp >= level_up_xp:<br />
# it is! level up and ask to raise some stats<br />
player.level += 1<br />
player.fighter.xp -= level_up_xp<br />
message('Your battle skills grow stronger! You reached level ' + str(player.level) + '!', libtcod.yellow)<br />
<br />
choice = None<br />
while choice is None: # keep asking until a choice is made<br />
choice = menu('Level up! Choose a stat to raise:\n',<br />
['Constitution (+20 HP, from ' + str(player.fighter.max_hp) + ')',<br />
'Strength (+1 attack, from ' + str(player.fighter.power) + ')',<br />
'Agility (+1 defense, from ' + str(player.fighter.defense) + ')'], LEVEL_SCREEN_WIDTH)<br />
<br />
if choice == 0:<br />
player.fighter.base_max_hp += 20<br />
player.fighter.hp += 20<br />
elif choice == 1:<br />
player.fighter.base_power += 1<br />
elif choice == 2:<br />
player.fighter.base_defense += 1<br />
<br />
<br />
def player_death(player):<br />
# the game ended!<br />
global game_state<br />
message('You died!', libtcod.red)<br />
game_state = 'dead'<br />
<br />
# for added effect, transform the player into a corpse!<br />
player.char = '%'<br />
player.color = libtcod.dark_red<br />
<br />
<br />
def monster_death(monster):<br />
# transform it into a nasty corpse! it doesn't block, can't be<br />
# attacked and doesn't move<br />
message('The ' + monster.name + ' is dead! You gain ' + str(monster.fighter.xp) + ' experience points.', libtcod.orange)<br />
monster.char = '%'<br />
monster.color = libtcod.dark_red<br />
monster.blocks = False<br />
monster.fighter = None<br />
monster.ai = None<br />
monster.name = 'remains of ' + monster.name<br />
monster.send_to_back()<br />
<br />
<br />
def target_tile(max_range=None):<br />
global key, mouse<br />
# return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked.<br />
while True:<br />
# render the screen. this erases the inventory and shows the names of objects under the mouse.<br />
libtcod.console_flush()<br />
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)<br />
render_all()<br />
<br />
(x, y) = (mouse.cx, mouse.cy)<br />
<br />
if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE:<br />
return (None, None) # cancel if the player right-clicked or pressed Escape<br />
<br />
# accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range<br />
if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and<br />
(max_range is None or player.distance(x, y) <= max_range)):<br />
return (x, y)<br />
<br />
<br />
def target_monster(max_range=None):<br />
# returns a clicked monster inside FOV up to a range, or None if right-clicked<br />
while True:<br />
(x, y) = target_tile(max_range)<br />
if x is None: # player cancelled<br />
return None<br />
<br />
# return the first clicked monster, otherwise continue looping<br />
for obj in objects:<br />
if obj.x == x and obj.y == y and obj.fighter and obj != player:<br />
return obj<br />
<br />
<br />
def closest_monster(max_range):<br />
# find closest enemy, up to a maximum range, and in the player's FOV<br />
closest_enemy = None<br />
closest_dist = max_range + 1 # start with (slightly more than) maximum range<br />
<br />
for object in objects:<br />
if object.fighter and not object == player and libtcod.map_is_in_fov(fov_map, object.x, object.y):<br />
# calculate distance between this object and the player<br />
dist = player.distance_to(object)<br />
if dist < closest_dist: # it's closer, so remember it<br />
closest_enemy = object<br />
closest_dist = dist<br />
return closest_enemy<br />
<br />
<br />
def cast_heal():<br />
# heal the player<br />
if player.fighter.hp == player.fighter.max_hp:<br />
message('You are already at full health.', libtcod.red)<br />
return 'cancelled'<br />
<br />
message('Your wounds start to feel better!', libtcod.light_violet)<br />
player.fighter.heal(HEAL_AMOUNT)<br />
<br />
<br />
def cast_lightning():<br />
# find closest enemy (inside a maximum range) and damage it<br />
monster = closest_monster(LIGHTNING_RANGE)<br />
if monster is None: # no enemy found within maximum range<br />
message('No enemy is close enough to strike.', libtcod.red)<br />
return 'cancelled'<br />
<br />
# zap it!<br />
message('A lighting bolt strikes the ' + monster.name + ' with a loud thunder! The damage is '<br />
+ str(LIGHTNING_DAMAGE) + ' hit points.', libtcod.light_blue)<br />
monster.fighter.take_damage(LIGHTNING_DAMAGE)<br />
<br />
<br />
def cast_fireball():<br />
# ask the player for a target tile to throw a fireball at<br />
message('Left-click a target tile for the fireball, or right-click to cancel.', libtcod.light_cyan)<br />
(x, y) = target_tile()<br />
if x is None:<br />
return 'cancelled'<br />
message('The fireball explodes, burning everything within ' + str(FIREBALL_RADIUS) + ' tiles!', libtcod.orange)<br />
<br />
for obj in objects: # damage every fighter in range, including the player<br />
if obj.distance(x, y) <= FIREBALL_RADIUS and obj.fighter:<br />
message('The ' + obj.name + ' gets burned for ' + str(FIREBALL_DAMAGE) + ' hit points.', libtcod.orange)<br />
obj.fighter.take_damage(FIREBALL_DAMAGE)<br />
<br />
<br />
def cast_confuse():<br />
# ask the player for a target to confuse<br />
message('Left-click an enemy to confuse it, or right-click to cancel.', libtcod.light_cyan)<br />
monster = target_monster(CONFUSE_RANGE)<br />
if monster is None:<br />
return 'cancelled'<br />
<br />
# replace the monster's AI with a "confused" one; after some turns it will restore the old AI<br />
old_ai = monster.ai<br />
monster.ai = ConfusedMonster(old_ai)<br />
monster.ai.owner = monster # tell the new component who owns it<br />
message('The eyes of the ' + monster.name + ' look vacant, as he starts to stumble around!', libtcod.light_green)<br />
<br />
<br />
def save_game():<br />
# open a new empty shelve (possibly overwriting an old one) to write the game data<br />
file = shelve.open('savegame', 'n')<br />
file['map'] = map<br />
file['objects'] = objects<br />
file['player_index'] = objects.index(player) # index of player in objects list<br />
file['stairs_index'] = objects.index(stairs) # same for the stairs<br />
file['inventory'] = inventory<br />
file['game_msgs'] = game_msgs<br />
file['game_state'] = game_state<br />
file['dungeon_level'] = dungeon_level<br />
file.close()<br />
<br />
<br />
def load_game():<br />
# open the previously saved shelve and load the game data<br />
global map, objects, player, stairs, inventory, game_msgs, game_state, dungeon_level<br />
<br />
file = shelve.open('savegame', 'r')<br />
map = file['map']<br />
objects = file['objects']<br />
player = objects[file['player_index']] # get index of player in objects list and access it<br />
stairs = objects[file['stairs_index']] # same for the stairs<br />
inventory = file['inventory']<br />
game_msgs = file['game_msgs']<br />
game_state = file['game_state']<br />
dungeon_level = file['dungeon_level']<br />
file.close()<br />
<br />
initialize_fov()<br />
<br />
<br />
def new_game():<br />
global player, inventory, game_msgs, game_state, dungeon_level<br />
<br />
# create object representing the player<br />
fighter_component = Fighter(hp=100, defense=1, power=2, xp=0, death_function=player_death)<br />
player = Object(0, 0, '@', 'player', libtcod.white, blocks=True, fighter=fighter_component)<br />
<br />
player.level = 1<br />
<br />
# generate map (at this point it's not drawn to the screen)<br />
dungeon_level = 1<br />
make_map()<br />
initialize_fov()<br />
<br />
game_state = 'playing'<br />
inventory = []<br />
<br />
# create the list of game messages and their colors, starts empty<br />
game_msgs = []<br />
<br />
# a warm welcoming message!<br />
message('Welcome stranger! Prepare to perish in the Tombs of the Ancient Kings.', libtcod.red)<br />
<br />
# initial equipment: a dagger<br />
equipment_component = Equipment(slot='right hand', power_bonus=2)<br />
obj = Object(0, 0, '-', 'dagger', libtcod.sky, equipment=equipment_component)<br />
inventory.append(obj)<br />
equipment_component.equip()<br />
obj.always_visible = True<br />
<br />
<br />
def next_level():<br />
# advance to the next level<br />
global dungeon_level<br />
message('You take a moment to rest, and recover your strength.', libtcod.light_violet)<br />
player.fighter.heal(player.fighter.max_hp // 2) # heal the player by 50%<br />
<br />
dungeon_level += 1<br />
message('After a rare moment of peace, you descend deeper into the heart of the dungeon...', libtcod.red)<br />
make_map() # create a fresh new level!<br />
initialize_fov()<br />
<br />
<br />
def initialize_fov():<br />
global fov_recompute, fov_map<br />
fov_recompute = True<br />
<br />
# create the FOV map, according to the generated map<br />
fov_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)<br />
for y in range(MAP_HEIGHT):<br />
for x in range(MAP_WIDTH):<br />
libtcod.map_set_properties(fov_map, x, y, not map[x][y].block_sight, not map[x][y].blocked)<br />
<br />
libtcod.console_clear(con) # unexplored areas start black (which is the default background color)<br />
<br />
<br />
def play_game():<br />
global key, mouse<br />
<br />
player_action = None<br />
<br />
mouse = libtcod.Mouse()<br />
key = libtcod.Key()<br />
# main loop<br />
while not libtcod.console_is_window_closed():<br />
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)<br />
# render the screen<br />
render_all()<br />
<br />
libtcod.console_flush()<br />
<br />
# level up if needed<br />
check_level_up()<br />
<br />
# erase all objects at their old locations, before they move<br />
for object in objects:<br />
object.clear()<br />
<br />
# handle keys and exit game if needed<br />
player_action = handle_keys()<br />
if player_action == 'exit':<br />
save_game()<br />
break<br />
<br />
# let monsters take their turn<br />
if game_state == 'playing' and player_action != 'didnt-take-turn':<br />
for object in objects:<br />
if object.ai:<br />
object.ai.take_turn()<br />
<br />
<br />
def main_menu():<br />
img = libtcod.image_load('menu_background.png')<br />
<br />
while not libtcod.console_is_window_closed():<br />
# show the background image, at twice the regular console resolution<br />
libtcod.image_blit_2x(img, 0, 0, 0)<br />
<br />
# show the game's title, and some credits!<br />
libtcod.console_set_default_foreground(0, libtcod.light_yellow)<br />
libtcod.console_print_ex(0, SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 4, libtcod.BKGND_NONE, libtcod.CENTER,<br />
'TOMBS OF THE ANCIENT KINGS')<br />
libtcod.console_print_ex(0, SCREEN_WIDTH // 2, SCREEN_HEIGHT - 2, libtcod.BKGND_NONE, libtcod.CENTER, 'By Jotaf')<br />
<br />
# show options and wait for the player's choice<br />
choice = menu('', ['Play a new game', 'Continue last game', 'Quit'], 24)<br />
<br />
if choice == 0: # new game<br />
new_game()<br />
play_game()<br />
if choice == 1: # load last game<br />
try:<br />
load_game()<br />
except Exception:<br />
msgbox('\n No saved game to load.\n', 24)<br />
continue<br />
play_game()<br />
elif choice == 2: # quit<br />
break<br />
<br />
<br />
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)<br />
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)<br />
libtcod.sys_set_fps(LIMIT_FPS)<br />
con = libtcod.console_new(MAP_WIDTH, MAP_HEIGHT)<br />
panel = libtcod.console_new(SCREEN_WIDTH, PANEL_HEIGHT)<br />
<br />
main_menu()<br />
<br />
</syntaxhighlight></div><br />
<br />
[[Category:Developing]]</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_part_13_code&diff=47056Complete Roguelike Tutorial, using python3+libtcod, part 13 code2018-05-07T16:11:25Z<p>Bix: /* Adventure gear */</p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
<br />
== Adventure gear ==<br />
<div style="background-color: #EEEEEE; border-style: dotted"><syntaxhighlight lang="python"><br />
#!/usr/bin/env python3<br />
#<br />
# libtcod python tutorial<br />
#<br />
<br />
import libtcodpy as libtcod<br />
import math<br />
import textwrap<br />
import shelve<br />
<br />
<br />
#actual size of the window<br />
SCREEN_WIDTH = 80<br />
SCREEN_HEIGHT = 50<br />
<br />
#size of the map<br />
MAP_WIDTH = 80<br />
MAP_HEIGHT = 43<br />
<br />
#sizes and coordinates relevant for the GUI<br />
BAR_WIDTH = 20<br />
PANEL_HEIGHT = 7<br />
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT<br />
MSG_X = BAR_WIDTH + 2<br />
MSG_WIDTH = SCREEN_WIDTH - BAR_WIDTH - 2<br />
MSG_HEIGHT = PANEL_HEIGHT - 1<br />
INVENTORY_WIDTH = 50<br />
CHARACTER_SCREEN_WIDTH = 30<br />
LEVEL_SCREEN_WIDTH = 40<br />
<br />
#parameters for dungeon generator<br />
ROOM_MAX_SIZE = 10<br />
ROOM_MIN_SIZE = 6<br />
MAX_ROOMS = 30<br />
<br />
#spell values<br />
HEAL_AMOUNT = 40<br />
LIGHTNING_DAMAGE = 40<br />
LIGHTNING_RANGE = 5<br />
CONFUSE_RANGE = 8<br />
CONFUSE_NUM_TURNS = 10<br />
FIREBALL_RADIUS = 3<br />
FIREBALL_DAMAGE = 25<br />
<br />
#experience and level-ups<br />
LEVEL_UP_BASE = 200<br />
LEVEL_UP_FACTOR = 150<br />
<br />
<br />
FOV_ALGO = 0 #default FOV algorithm<br />
FOV_LIGHT_WALLS = True #light walls or not<br />
TORCH_RADIUS = 10<br />
<br />
LIMIT_FPS = 20 #20 frames-per-second maximum<br />
<br />
<br />
color_dark_wall = libtcod.Color(0, 0, 100)<br />
color_light_wall = libtcod.Color(130, 110, 50)<br />
color_dark_ground = libtcod.Color(50, 50, 150)<br />
color_light_ground = libtcod.Color(200, 180, 50)<br />
<br />
<br />
class Tile:<br />
#a tile of the map and its properties<br />
def __init__(self, blocked, block_sight = None):<br />
self.blocked = blocked<br />
<br />
#all tiles start unexplored<br />
self.explored = False<br />
<br />
#by default, if a tile is blocked, it also blocks sight<br />
if block_sight is None: block_sight = blocked<br />
self.block_sight = block_sight<br />
<br />
class Rect:<br />
#a rectangle on the map. used to characterize a room.<br />
def __init__(self, x, y, w, h):<br />
self.x1 = x<br />
self.y1 = y<br />
self.x2 = x + w<br />
self.y2 = y + h<br />
<br />
def center(self):<br />
center_x = (self.x1 + self.x2) / 2<br />
center_y = (self.y1 + self.y2) / 2<br />
return (center_x, center_y)<br />
<br />
def intersect(self, other):<br />
#returns true if this rectangle intersects with another one<br />
return (self.x1 <= other.x2 and self.x2 >= other.x1 and<br />
self.y1 <= other.y2 and self.y2 >= other.y1)<br />
<br />
class Object:<br />
#this is a generic object: the player, a monster, an item, the stairs...<br />
#it's always represented by a character on screen.<br />
def __init__(self, x, y, char, name, color, blocks=False, always_visible=False, fighter=None, ai=None, item=None, equipment=None):<br />
self.x = x<br />
self.y = y<br />
self.char = char<br />
self.name = name<br />
self.color = color<br />
self.blocks = blocks<br />
self.always_visible = always_visible<br />
self.fighter = fighter<br />
if self.fighter: #let the fighter component know who owns it<br />
self.fighter.owner = self<br />
<br />
self.ai = ai<br />
if self.ai: #let the AI component know who owns it<br />
self.ai.owner = self<br />
<br />
self.item = item<br />
if self.item: #let the Item component know who owns it<br />
self.item.owner = self<br />
<br />
self.equipment = equipment<br />
if self.equipment: #let the Equipment component know who owns it<br />
self.equipment.owner = self<br />
<br />
#there must be an Item component for the Equipment component to work properly<br />
self.item = Item()<br />
self.item.owner = self<br />
<br />
def move(self, dx, dy):<br />
#move by the given amount, if the destination is not blocked<br />
if not is_blocked(self.x + dx, self.y + dy):<br />
self.x += dx<br />
self.y += dy<br />
<br />
def move_towards(self, target_x, target_y):<br />
#vector from this object to the target, and distance<br />
dx = target_x - self.x<br />
dy = target_y - self.y<br />
distance = math.sqrt(dx ** 2 + dy ** 2)<br />
<br />
#normalize it to length 1 (preserving direction), then round it and<br />
#convert to integer so the movement is restricted to the map grid<br />
dx = int(round(dx / distance))<br />
dy = int(round(dy / distance))<br />
self.move(dx, dy)<br />
<br />
def distance_to(self, other):<br />
#return the distance to another object<br />
dx = other.x - self.x<br />
dy = other.y - self.y<br />
return math.sqrt(dx ** 2 + dy ** 2)<br />
<br />
def distance(self, x, y):<br />
#return the distance to some coordinates<br />
return math.sqrt((x - self.x) ** 2 + (y - self.y) ** 2)<br />
<br />
def send_to_back(self):<br />
#make this object be drawn first, so all others appear above it if they're in the same tile.<br />
global objects<br />
objects.remove(self)<br />
objects.insert(0, self)<br />
<br />
def draw(self):<br />
#only show if it's visible to the player; or it's set to "always visible" and on an explored tile<br />
if (libtcod.map_is_in_fov(fov_map, self.x, self.y) or<br />
(self.always_visible and map[self.x][self.y].explored)):<br />
#set the color and then draw the character that represents this object at its position<br />
libtcod.console_set_default_foreground(con, self.color)<br />
libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)<br />
<br />
def clear(self):<br />
#erase the character that represents this object<br />
libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)<br />
<br />
<br />
class Fighter:<br />
#combat-related properties and methods (monster, player, NPC).<br />
def __init__(self, hp, defense, power, xp, death_function=None):<br />
self.base_max_hp = hp<br />
self.hp = hp<br />
self.base_defense = defense<br />
self.base_power = power<br />
self.xp = xp<br />
self.death_function = death_function<br />
<br />
@property<br />
def power(self): #return actual power, by summing up the bonuses from all equipped items<br />
bonus = sum(equipment.power_bonus for equipment in get_all_equipped(self.owner))<br />
return self.base_power + bonus<br />
<br />
@property<br />
def defense(self): #return actual defense, by summing up the bonuses from all equipped items<br />
bonus = sum(equipment.defense_bonus for equipment in get_all_equipped(self.owner))<br />
return self.base_defense + bonus<br />
<br />
@property<br />
def max_hp(self): #return actual max_hp, by summing up the bonuses from all equipped items<br />
bonus = sum(equipment.max_hp_bonus for equipment in get_all_equipped(self.owner))<br />
return self.base_max_hp + bonus<br />
<br />
def attack(self, target):<br />
#a simple formula for attack damage<br />
damage = self.power - target.fighter.defense<br />
<br />
if damage > 0:<br />
#make the target take some damage<br />
message(self.owner.name.capitalize() + ' attacks ' + target.name + ' for ' + str(damage) + ' hit points.')<br />
target.fighter.take_damage(damage)<br />
else:<br />
message(self.owner.name.capitalize() + ' attacks ' + target.name + ' but it has no effect!')<br />
<br />
def take_damage(self, damage):<br />
#apply damage if possible<br />
if damage > 0:<br />
self.hp -= damage<br />
<br />
#check for death. if there's a death function, call it<br />
if self.hp <= 0:<br />
function = self.death_function<br />
if function is not None:<br />
function(self.owner)<br />
<br />
if self.owner != player: #yield experience to the player<br />
player.fighter.xp += self.xp<br />
<br />
def heal(self, amount):<br />
#heal by the given amount, without going over the maximum<br />
self.hp += amount<br />
if self.hp > self.max_hp:<br />
self.hp = self.max_hp<br />
<br />
class BasicMonster:<br />
#AI for a basic monster.<br />
def take_turn(self):<br />
#a basic monster takes its turn. if you can see it, it can see you<br />
monster = self.owner<br />
if libtcod.map_is_in_fov(fov_map, monster.x, monster.y):<br />
<br />
#move towards player if far away<br />
if monster.distance_to(player) >= 2:<br />
monster.move_towards(player.x, player.y)<br />
<br />
#close enough, attack! (if the player is still alive.)<br />
elif player.fighter.hp > 0:<br />
monster.fighter.attack(player)<br />
<br />
class ConfusedMonster:<br />
#AI for a temporarily confused monster (reverts to previous AI after a while).<br />
def __init__(self, old_ai, num_turns=CONFUSE_NUM_TURNS):<br />
self.old_ai = old_ai<br />
self.num_turns = num_turns<br />
<br />
def take_turn(self):<br />
if self.num_turns > 0: #still confused...<br />
#move in a random direction, and decrease the number of turns confused<br />
self.owner.move(libtcod.random_get_int(0, -1, 1), libtcod.random_get_int(0, -1, 1))<br />
self.num_turns -= 1<br />
<br />
else: #restore the previous AI (this one will be deleted because it's not referenced anymore)<br />
self.owner.ai = self.old_ai<br />
message('The ' + self.owner.name + ' is no longer confused!', libtcod.red)<br />
<br />
class Item:<br />
#an item that can be picked up and used.<br />
def __init__(self, use_function=None):<br />
self.use_function = use_function<br />
<br />
def pick_up(self):<br />
#add to the player's inventory and remove from the map<br />
if len(inventory) >= 26:<br />
message('Your inventory is full, cannot pick up ' + self.owner.name + '.', libtcod.red)<br />
else:<br />
inventory.append(self.owner)<br />
objects.remove(self.owner)<br />
message('You picked up a ' + self.owner.name + '!', libtcod.green)<br />
<br />
#special case: automatically equip, if the corresponding equipment slot is unused<br />
equipment = self.owner.equipment<br />
if equipment and get_equipped_in_slot(equipment.slot) is None:<br />
equipment.equip()<br />
<br />
def drop(self):<br />
#special case: if the object has the Equipment component, dequip it before dropping<br />
if self.owner.equipment:<br />
self.owner.equipment.dequip()<br />
<br />
#add to the map and remove from the player's inventory. also, place it at the player's coordinates<br />
objects.append(self.owner)<br />
inventory.remove(self.owner)<br />
self.owner.x = player.x<br />
self.owner.y = player.y<br />
message('You dropped a ' + self.owner.name + '.', libtcod.yellow)<br />
<br />
def use(self):<br />
#special case: if the object has the Equipment component, the "use" action is to equip/dequip<br />
if self.owner.equipment:<br />
self.owner.equipment.toggle_equip()<br />
return<br />
<br />
#just call the "use_function" if it is defined<br />
if self.use_function is None:<br />
message('The ' + self.owner.name + ' cannot be used.')<br />
else:<br />
if self.use_function() != 'cancelled':<br />
inventory.remove(self.owner) #destroy after use, unless it was cancelled for some reason<br />
<br />
class Equipment:<br />
#an object that can be equipped, yielding bonuses. automatically adds the Item component.<br />
def __init__(self, slot, power_bonus=0, defense_bonus=0, max_hp_bonus=0):<br />
self.power_bonus = power_bonus<br />
self.defense_bonus = defense_bonus<br />
self.max_hp_bonus = max_hp_bonus<br />
<br />
self.slot = slot<br />
self.is_equipped = False<br />
<br />
def toggle_equip(self): #toggle equip/dequip status<br />
if self.is_equipped:<br />
self.dequip()<br />
else:<br />
self.equip()<br />
<br />
def equip(self):<br />
#if the slot is already being used, dequip whatever is there first<br />
old_equipment = get_equipped_in_slot(self.slot)<br />
if old_equipment is not None:<br />
old_equipment.dequip()<br />
<br />
#equip object and show a message about it<br />
self.is_equipped = True<br />
message('Equipped ' + self.owner.name + ' on ' + self.slot + '.', libtcod.light_green)<br />
<br />
def dequip(self):<br />
#dequip object and show a message about it<br />
if not self.is_equipped: return<br />
self.is_equipped = False<br />
message('Dequipped ' + self.owner.name + ' from ' + self.slot + '.', libtcod.light_yellow)<br />
<br />
<br />
def get_equipped_in_slot(slot): #returns the equipment in a slot, or None if it's empty<br />
for obj in inventory:<br />
if obj.equipment and obj.equipment.slot == slot and obj.equipment.is_equipped:<br />
return obj.equipment<br />
return None<br />
<br />
def get_all_equipped(obj): #returns a list of equipped items<br />
if obj == player:<br />
equipped_list = []<br />
for item in inventory:<br />
if item.equipment and item.equipment.is_equipped:<br />
equipped_list.append(item.equipment)<br />
return equipped_list<br />
else:<br />
return [] #other objects have no equipment<br />
<br />
<br />
def is_blocked(x, y):<br />
#first test the map tile<br />
if map[x][y].blocked:<br />
return True<br />
<br />
#now check for any blocking objects<br />
for object in objects:<br />
if object.blocks and object.x == x and object.y == y:<br />
return True<br />
<br />
return False<br />
<br />
def create_room(room):<br />
global map<br />
#go through the tiles in the rectangle and make them passable<br />
for x in range(room.x1 + 1, room.x2):<br />
for y in range(room.y1 + 1, room.y2):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
def create_h_tunnel(x1, x2, y):<br />
global map<br />
#horizontal tunnel. min() and max() are used in case x1>x2<br />
for x in range(min(x1, x2), max(x1, x2) + 1):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
def create_v_tunnel(y1, y2, x):<br />
global map<br />
#vertical tunnel<br />
for y in range(min(y1, y2), max(y1, y2) + 1):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
def make_map():<br />
global map, objects, stairs<br />
<br />
#the list of objects with just the player<br />
objects = [player]<br />
<br />
#fill map with "blocked" tiles<br />
map = [[ Tile(True)<br />
for y in range(MAP_HEIGHT) ]<br />
for x in range(MAP_WIDTH) ]<br />
<br />
rooms = []<br />
num_rooms = 0<br />
<br />
for r in range(MAX_ROOMS):<br />
#random width and height<br />
w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)<br />
h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)<br />
#random position without going out of the boundaries of the map<br />
x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1)<br />
y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1)<br />
<br />
#"Rect" class makes rectangles easier to work with<br />
new_room = Rect(x, y, w, h)<br />
<br />
#run through the other rooms and see if they intersect with this one<br />
failed = False<br />
for other_room in rooms:<br />
if new_room.intersect(other_room):<br />
failed = True<br />
break<br />
<br />
if not failed:<br />
#this means there are no intersections, so this room is valid<br />
<br />
#"paint" it to the map's tiles<br />
create_room(new_room)<br />
<br />
#center coordinates of new room, will be useful later<br />
(new_x, new_y) = new_room.center()<br />
<br />
if num_rooms == 0:<br />
#this is the first room, where the player starts at<br />
player.x = new_x<br />
player.y = new_y<br />
else:<br />
#all rooms after the first:<br />
#connect it to the previous room with a tunnel<br />
<br />
#center coordinates of previous room<br />
(prev_x, prev_y) = rooms[num_rooms-1].center()<br />
<br />
#draw a coin (random number that is either 0 or 1)<br />
if libtcod.random_get_int(0, 0, 1) == 1:<br />
#first move horizontally, then vertically<br />
create_h_tunnel(prev_x, new_x, prev_y)<br />
create_v_tunnel(prev_y, new_y, new_x)<br />
else:<br />
#first move vertically, then horizontally<br />
create_v_tunnel(prev_y, new_y, prev_x)<br />
create_h_tunnel(prev_x, new_x, new_y)<br />
<br />
#add some contents to this room, such as monsters<br />
place_objects(new_room)<br />
<br />
#finally, append the new room to the list<br />
rooms.append(new_room)<br />
num_rooms += 1<br />
<br />
#create stairs at the center of the last room<br />
stairs = Object(new_x, new_y, '<', 'stairs', libtcod.white, always_visible=True)<br />
objects.append(stairs)<br />
stairs.send_to_back() #so it's drawn below the monsters<br />
<br />
def random_choice_index(chances): #choose one option from list of chances, returning its index<br />
#the dice will land on some number between 1 and the sum of the chances<br />
dice = libtcod.random_get_int(0, 1, sum(chances))<br />
<br />
#go through all chances, keeping the sum so far<br />
running_sum = 0<br />
choice = 0<br />
for w in chances:<br />
running_sum += w<br />
<br />
#see if the dice landed in the part that corresponds to this choice<br />
if dice <= running_sum:<br />
return choice<br />
choice += 1<br />
<br />
def random_choice(chances_dict):<br />
#choose one option from dictionary of chances, returning its key<br />
chances = chances_dict.values()<br />
strings = chances_dict.keys()<br />
<br />
return strings[random_choice_index(chances)]<br />
<br />
def from_dungeon_level(table):<br />
#returns a value that depends on level. the table specifies what value occurs after each level, default is 0.<br />
for (value, level) in reversed(table):<br />
if dungeon_level >= level:<br />
return value<br />
return 0<br />
<br />
def place_objects(room):<br />
#this is where we decide the chance of each monster or item appearing.<br />
<br />
#maximum number of monsters per room<br />
max_monsters = from_dungeon_level([[2, 1], [3, 4], [5, 6]])<br />
<br />
#chance of each monster<br />
monster_chances = {}<br />
monster_chances['orc'] = 80 #orc always shows up, even if all other monsters have 0 chance<br />
monster_chances['troll'] = from_dungeon_level([[15, 3], [30, 5], [60, 7]])<br />
<br />
#maximum number of items per room<br />
max_items = from_dungeon_level([[1, 1], [2, 4]])<br />
<br />
#chance of each item (by default they have a chance of 0 at level 1, which then goes up)<br />
item_chances = {}<br />
item_chances['heal'] = 35 #healing potion always shows up, even if all other items have 0 chance<br />
item_chances['lightning'] = from_dungeon_level([[25, 4]])<br />
item_chances['fireball'] = from_dungeon_level([[25, 6]])<br />
item_chances['confuse'] = from_dungeon_level([[10, 2]])<br />
item_chances['sword'] = from_dungeon_level([[5, 4]])<br />
item_chances['shield'] = from_dungeon_level([[15, 8]])<br />
<br />
<br />
#choose random number of monsters<br />
num_monsters = libtcod.random_get_int(0, 0, max_monsters)<br />
<br />
for i in range(num_monsters):<br />
#choose random spot for this monster<br />
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)<br />
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)<br />
<br />
#only place it if the tile is not blocked<br />
if not is_blocked(x, y):<br />
choice = random_choice(monster_chances)<br />
if choice == 'orc':<br />
#create an orc<br />
fighter_component = Fighter(hp=20, defense=0, power=4, xp=35, death_function=monster_death)<br />
ai_component = BasicMonster()<br />
<br />
monster = Object(x, y, 'o', 'orc', libtcod.desaturated_green,<br />
blocks=True, fighter=fighter_component, ai=ai_component)<br />
<br />
elif choice == 'troll':<br />
#create a troll<br />
fighter_component = Fighter(hp=30, defense=2, power=8, xp=100, death_function=monster_death)<br />
ai_component = BasicMonster()<br />
<br />
monster = Object(x, y, 'T', 'troll', libtcod.darker_green,<br />
blocks=True, fighter=fighter_component, ai=ai_component)<br />
<br />
objects.append(monster)<br />
<br />
#choose random number of items<br />
num_items = libtcod.random_get_int(0, 0, max_items)<br />
<br />
for i in range(num_items):<br />
#choose random spot for this item<br />
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)<br />
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)<br />
<br />
#only place it if the tile is not blocked<br />
if not is_blocked(x, y):<br />
choice = random_choice(item_chances)<br />
if choice == 'heal':<br />
#create a healing potion<br />
item_component = Item(use_function=cast_heal)<br />
item = Object(x, y, '!', 'healing potion', libtcod.violet, item=item_component)<br />
<br />
elif choice == 'lightning':<br />
#create a lightning bolt scroll<br />
item_component = Item(use_function=cast_lightning)<br />
item = Object(x, y, '#', 'scroll of lightning bolt', libtcod.light_yellow, item=item_component)<br />
<br />
elif choice == 'fireball':<br />
#create a fireball scroll<br />
item_component = Item(use_function=cast_fireball)<br />
item = Object(x, y, '#', 'scroll of fireball', libtcod.light_yellow, item=item_component)<br />
<br />
elif choice == 'confuse':<br />
#create a confuse scroll<br />
item_component = Item(use_function=cast_confuse)<br />
item = Object(x, y, '#', 'scroll of confusion', libtcod.light_yellow, item=item_component)<br />
<br />
elif choice == 'sword':<br />
#create a sword<br />
equipment_component = Equipment(slot='right hand', power_bonus=3)<br />
item = Object(x, y, '/', 'sword', libtcod.sky, equipment=equipment_component)<br />
<br />
elif choice == 'shield':<br />
#create a shield<br />
equipment_component = Equipment(slot='left hand', defense_bonus=1)<br />
item = Object(x, y, '[', 'shield', libtcod.darker_orange, equipment=equipment_component)<br />
<br />
objects.append(item)<br />
item.send_to_back() #items appear below other objects<br />
item.always_visible = True #items are visible even out-of-FOV, if in an explored area<br />
<br />
<br />
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color):<br />
#render a bar (HP, experience, etc). first calculate the width of the bar<br />
bar_width = int(float(value) / maximum * total_width)<br />
<br />
#render the background first<br />
libtcod.console_set_default_background(panel, back_color)<br />
libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN)<br />
<br />
#now render the bar on top<br />
libtcod.console_set_default_background(panel, bar_color)<br />
if bar_width > 0:<br />
libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN)<br />
<br />
#finally, some centered text with the values<br />
libtcod.console_set_default_foreground(panel, libtcod.white)<br />
libtcod.console_print_ex(panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER,<br />
name + ': ' + str(value) + '/' + str(maximum))<br />
<br />
def get_names_under_mouse():<br />
global mouse<br />
#return a string with the names of all objects under the mouse<br />
<br />
(x, y) = (mouse.cx, mouse.cy)<br />
<br />
#create a list with the names of all objects at the mouse's coordinates and in FOV<br />
names = [obj.name for obj in objects<br />
if obj.x == x and obj.y == y and libtcod.map_is_in_fov(fov_map, obj.x, obj.y)]<br />
<br />
names = ', '.join(names) #join the names, separated by commas<br />
return names.capitalize()<br />
<br />
def render_all():<br />
global fov_map, color_dark_wall, color_light_wall<br />
global color_dark_ground, color_light_ground<br />
global fov_recompute<br />
<br />
if fov_recompute:<br />
#recompute FOV if needed (the player moved or something)<br />
fov_recompute = False<br />
libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)<br />
<br />
#go through all tiles, and set their background color according to the FOV<br />
for y in range(MAP_HEIGHT):<br />
for x in range(MAP_WIDTH):<br />
visible = libtcod.map_is_in_fov(fov_map, x, y)<br />
wall = map[x][y].block_sight<br />
if not visible:<br />
#if it's not visible right now, the player can only see it if it's explored<br />
if map[x][y].explored:<br />
if wall:<br />
libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET)<br />
else:<br />
libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET)<br />
else:<br />
#it's visible<br />
if wall:<br />
libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET )<br />
else:<br />
libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET )<br />
#since it's visible, explore it<br />
map[x][y].explored = True<br />
<br />
#draw all objects in the list, except the player. we want it to<br />
#always appear over all other objects! so it's drawn later.<br />
for object in objects:<br />
if object != player:<br />
object.draw()<br />
player.draw()<br />
<br />
#blit the contents of "con" to the root console<br />
libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0)<br />
<br />
<br />
#prepare to render the GUI panel<br />
libtcod.console_set_default_background(panel, libtcod.black)<br />
libtcod.console_clear(panel)<br />
<br />
#print the game messages, one line at a time<br />
y = 1<br />
for (line, color) in game_msgs:<br />
libtcod.console_set_default_foreground(panel, color)<br />
libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT,line)<br />
y += 1<br />
<br />
#show the player's stats<br />
render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp,<br />
libtcod.light_red, libtcod.darker_red)<br />
libtcod.console_print_ex(panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Dungeon level ' + str(dungeon_level))<br />
<br />
#display names of objects under the mouse<br />
libtcod.console_set_default_foreground(panel, libtcod.light_gray)<br />
libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse())<br />
<br />
#blit the contents of "panel" to the root console<br />
libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)<br />
<br />
<br />
def message(new_msg, color = libtcod.white):<br />
#split the message if necessary, among multiple lines<br />
new_msg_lines = textwrap.wrap(new_msg, MSG_WIDTH)<br />
<br />
for line in new_msg_lines:<br />
#if the buffer is full, remove the first line to make room for the new one<br />
if len(game_msgs) == MSG_HEIGHT:<br />
del game_msgs[0]<br />
<br />
#add the new line as a tuple, with the text and the color<br />
game_msgs.append( (line, color) )<br />
<br />
<br />
def player_move_or_attack(dx, dy):<br />
global fov_recompute<br />
<br />
#the coordinates the player is moving to/attacking<br />
x = player.x + dx<br />
y = player.y + dy<br />
<br />
#try to find an attackable object there<br />
target = None<br />
for object in objects:<br />
if object.fighter and object.x == x and object.y == y:<br />
target = object<br />
break<br />
<br />
#attack if target found, move otherwise<br />
if target is not None:<br />
player.fighter.attack(target)<br />
else:<br />
player.move(dx, dy)<br />
fov_recompute = True<br />
<br />
<br />
def menu(header, options, width):<br />
if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.')<br />
<br />
#calculate total height for the header (after auto-wrap) and one line per option<br />
header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header)<br />
if header == '':<br />
header_height = 0<br />
height = len(options) + header_height<br />
<br />
#create an off-screen console that represents the menu's window<br />
window = libtcod.console_new(width, height)<br />
<br />
#print the header, with auto-wrap<br />
libtcod.console_set_default_foreground(window, libtcod.white)<br />
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)<br />
<br />
#print all the options<br />
y = header_height<br />
letter_index = ord('a')<br />
for option_text in options:<br />
text = '(' + chr(letter_index) + ') ' + option_text<br />
libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text)<br />
y += 1<br />
letter_index += 1<br />
<br />
#blit the contents of "window" to the root console<br />
x = SCREEN_WIDTH/2 - width/2<br />
y = SCREEN_HEIGHT/2 - height/2<br />
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)<br />
<br />
#present the root console to the player and wait for a key-press<br />
libtcod.console_flush()<br />
key = libtcod.console_wait_for_keypress(True)<br />
<br />
if key.vk == libtcod.KEY_ENTER and key.lalt: #(special case) Alt+Enter: toggle fullscreen<br />
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen)<br />
<br />
#convert the ASCII code to an index; if it corresponds to an option, return it<br />
index = key.c - ord('a')<br />
if index >= 0 and index < len(options): return index<br />
return None<br />
<br />
def inventory_menu(header):<br />
#show a menu with each item of the inventory as an option<br />
if len(inventory) == 0:<br />
options = ['Inventory is empty.']<br />
else:<br />
options = []<br />
for item in inventory:<br />
text = item.name<br />
#show additional information, in case it's equipped<br />
if item.equipment and item.equipment.is_equipped:<br />
text = text + ' (on ' + item.equipment.slot + ')'<br />
options.append(text)<br />
<br />
index = menu(header, options, INVENTORY_WIDTH)<br />
<br />
#if an item was chosen, return it<br />
if index is None or len(inventory) == 0: return None<br />
return inventory[index].item<br />
<br />
def msgbox(text, width=50):<br />
menu(text, [], width) #use menu() as a sort of "message box"<br />
<br />
def handle_keys():<br />
global key<br />
<br />
if key.vk == libtcod.KEY_ENTER and key.lalt:<br />
#Alt+Enter: toggle fullscreen<br />
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())<br />
<br />
elif key.vk == libtcod.KEY_ESCAPE:<br />
return 'exit' #exit game<br />
<br />
if game_state == 'playing':<br />
#movement keys<br />
if key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_KP8:<br />
player_move_or_attack(0, -1)<br />
elif key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_KP2:<br />
player_move_or_attack(0, 1)<br />
elif key.vk == libtcod.KEY_LEFT or key.vk == libtcod.KEY_KP4:<br />
player_move_or_attack(-1, 0)<br />
elif key.vk == libtcod.KEY_RIGHT or key.vk == libtcod.KEY_KP6:<br />
player_move_or_attack(1, 0)<br />
elif key.vk == libtcod.KEY_HOME or key.vk == libtcod.KEY_KP7:<br />
player_move_or_attack(-1, -1)<br />
elif key.vk == libtcod.KEY_PAGEUP or key.vk == libtcod.KEY_KP9:<br />
player_move_or_attack(1, -1)<br />
elif key.vk == libtcod.KEY_END or key.vk == libtcod.KEY_KP1:<br />
player_move_or_attack(-1, 1)<br />
elif key.vk == libtcod.KEY_PAGEDOWN or key.vk == libtcod.KEY_KP3:<br />
player_move_or_attack(1, 1)<br />
elif key.vk == libtcod.KEY_KP5:<br />
pass #do nothing ie wait for the monster to come to you<br />
else:<br />
#test for other keys<br />
key_char = chr(key.c)<br />
<br />
if key_char == 'g':<br />
#pick up an item<br />
for object in objects: #look for an item in the player's tile<br />
if object.x == player.x and object.y == player.y and object.item:<br />
object.item.pick_up()<br />
break<br />
<br />
if key_char == 'i':<br />
#show the inventory; if an item is selected, use it<br />
chosen_item = inventory_menu('Press the key next to an item to use it, or any other to cancel.\n')<br />
if chosen_item is not None:<br />
chosen_item.use()<br />
<br />
if key_char == 'd':<br />
#show the inventory; if an item is selected, drop it<br />
chosen_item = inventory_menu('Press the key next to an item to drop it, or any other to cancel.\n')<br />
if chosen_item is not None:<br />
chosen_item.drop()<br />
<br />
if key_char == 'c':<br />
#show character information<br />
level_up_xp = LEVEL_UP_BASE + player.level * LEVEL_UP_FACTOR<br />
msgbox('Character Information\n\nLevel: ' + str(player.level) + '\nExperience: ' + str(player.fighter.xp) +<br />
'\nExperience to level up: ' + str(level_up_xp) + '\n\nMaximum HP: ' + str(player.fighter.max_hp) +<br />
'\nAttack: ' + str(player.fighter.power) + '\nDefense: ' + str(player.fighter.defense), CHARACTER_SCREEN_WIDTH)<br />
<br />
if key_char == '<':<br />
#go down stairs, if the player is on them<br />
if stairs.x == player.x and stairs.y == player.y:<br />
next_level()<br />
<br />
return 'didnt-take-turn'<br />
<br />
def check_level_up():<br />
#see if the player's experience is enough to level-up<br />
level_up_xp = LEVEL_UP_BASE + player.level * LEVEL_UP_FACTOR<br />
if player.fighter.xp >= level_up_xp:<br />
#it is! level up and ask to raise some stats<br />
player.level += 1<br />
player.fighter.xp -= level_up_xp<br />
message('Your battle skills grow stronger! You reached level ' + str(player.level) + '!', libtcod.yellow)<br />
<br />
choice = None<br />
while choice == None: #keep asking until a choice is made<br />
choice = menu('Level up! Choose a stat to raise:\n',<br />
['Constitution (+20 HP, from ' + str(player.fighter.max_hp) + ')',<br />
'Strength (+1 attack, from ' + str(player.fighter.power) + ')',<br />
'Agility (+1 defense, from ' + str(player.fighter.defense) + ')'], LEVEL_SCREEN_WIDTH)<br />
<br />
if choice == 0:<br />
player.fighter.base_max_hp += 20<br />
player.fighter.hp += 20<br />
elif choice == 1:<br />
player.fighter.base_power += 1<br />
elif choice == 2:<br />
player.fighter.base_defense += 1<br />
<br />
def player_death(player):<br />
#the game ended!<br />
global game_state<br />
message('You died!', libtcod.red)<br />
game_state = 'dead'<br />
<br />
#for added effect, transform the player into a corpse!<br />
player.char = '%'<br />
player.color = libtcod.dark_red<br />
<br />
def monster_death(monster):<br />
#transform it into a nasty corpse! it doesn't block, can't be<br />
#attacked and doesn't move<br />
message('The ' + monster.name + ' is dead! You gain ' + str(monster.fighter.xp) + ' experience points.', libtcod.orange)<br />
monster.char = '%'<br />
monster.color = libtcod.dark_red<br />
monster.blocks = False<br />
monster.fighter = None<br />
monster.ai = None<br />
monster.name = 'remains of ' + monster.name<br />
monster.send_to_back()<br />
<br />
def target_tile(max_range=None):<br />
global key, mouse<br />
#return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked.<br />
while True:<br />
#render the screen. this erases the inventory and shows the names of objects under the mouse.<br />
libtcod.console_flush()<br />
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)<br />
render_all()<br />
<br />
(x, y) = (mouse.cx, mouse.cy)<br />
<br />
if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE:<br />
return (None, None) #cancel if the player right-clicked or pressed Escape<br />
<br />
#accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range<br />
if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and<br />
(max_range is None or player.distance(x, y) <= max_range)):<br />
return (x, y)<br />
<br />
def target_monster(max_range=None):<br />
#returns a clicked monster inside FOV up to a range, or None if right-clicked<br />
while True:<br />
(x, y) = target_tile(max_range)<br />
if x is None: #player cancelled<br />
return None<br />
<br />
#return the first clicked monster, otherwise continue looping<br />
for obj in objects:<br />
if obj.x == x and obj.y == y and obj.fighter and obj != player:<br />
return obj<br />
<br />
def closest_monster(max_range):<br />
#find closest enemy, up to a maximum range, and in the player's FOV<br />
closest_enemy = None<br />
closest_dist = max_range + 1 #start with (slightly more than) maximum range<br />
<br />
for object in objects:<br />
if object.fighter and not object == player and libtcod.map_is_in_fov(fov_map, object.x, object.y):<br />
#calculate distance between this object and the player<br />
dist = player.distance_to(object)<br />
if dist < closest_dist: #it's closer, so remember it<br />
closest_enemy = object<br />
closest_dist = dist<br />
return closest_enemy<br />
<br />
def cast_heal():<br />
#heal the player<br />
if player.fighter.hp == player.fighter.max_hp:<br />
message('You are already at full health.', libtcod.red)<br />
return 'cancelled'<br />
<br />
message('Your wounds start to feel better!', libtcod.light_violet)<br />
player.fighter.heal(HEAL_AMOUNT)<br />
<br />
def cast_lightning():<br />
#find closest enemy (inside a maximum range) and damage it<br />
monster = closest_monster(LIGHTNING_RANGE)<br />
if monster is None: #no enemy found within maximum range<br />
message('No enemy is close enough to strike.', libtcod.red)<br />
return 'cancelled'<br />
<br />
#zap it!<br />
message('A lighting bolt strikes the ' + monster.name + ' with a loud thunder! The damage is '<br />
+ str(LIGHTNING_DAMAGE) + ' hit points.', libtcod.light_blue)<br />
monster.fighter.take_damage(LIGHTNING_DAMAGE)<br />
<br />
def cast_fireball():<br />
#ask the player for a target tile to throw a fireball at<br />
message('Left-click a target tile for the fireball, or right-click to cancel.', libtcod.light_cyan)<br />
(x, y) = target_tile()<br />
if x is None: return 'cancelled'<br />
message('The fireball explodes, burning everything within ' + str(FIREBALL_RADIUS) + ' tiles!', libtcod.orange)<br />
<br />
for obj in objects: #damage every fighter in range, including the player<br />
if obj.distance(x, y) <= FIREBALL_RADIUS and obj.fighter:<br />
message('The ' + obj.name + ' gets burned for ' + str(FIREBALL_DAMAGE) + ' hit points.', libtcod.orange)<br />
obj.fighter.take_damage(FIREBALL_DAMAGE)<br />
<br />
def cast_confuse():<br />
#ask the player for a target to confuse<br />
message('Left-click an enemy to confuse it, or right-click to cancel.', libtcod.light_cyan)<br />
monster = target_monster(CONFUSE_RANGE)<br />
if monster is None: return 'cancelled'<br />
<br />
#replace the monster's AI with a "confused" one; after some turns it will restore the old AI<br />
old_ai = monster.ai<br />
monster.ai = ConfusedMonster(old_ai)<br />
monster.ai.owner = monster #tell the new component who owns it<br />
message('The eyes of the ' + monster.name + ' look vacant, as he starts to stumble around!', libtcod.light_green)<br />
<br />
<br />
def save_game():<br />
#open a new empty shelve (possibly overwriting an old one) to write the game data<br />
file = shelve.open('savegame', 'n')<br />
file['map'] = map<br />
file['objects'] = objects<br />
file['player_index'] = objects.index(player) #index of player in objects list<br />
file['stairs_index'] = objects.index(stairs) #same for the stairs<br />
file['inventory'] = inventory<br />
file['game_msgs'] = game_msgs<br />
file['game_state'] = game_state<br />
file['dungeon_level'] = dungeon_level<br />
file.close()<br />
<br />
def load_game():<br />
#open the previously saved shelve and load the game data<br />
global map, objects, player, stairs, inventory, game_msgs, game_state, dungeon_level<br />
<br />
file = shelve.open('savegame', 'r')<br />
map = file['map']<br />
objects = file['objects']<br />
player = objects[file['player_index']] #get index of player in objects list and access it<br />
stairs = objects[file['stairs_index']] #same for the stairs<br />
inventory = file['inventory']<br />
game_msgs = file['game_msgs']<br />
game_state = file['game_state']<br />
dungeon_level = file['dungeon_level']<br />
file.close()<br />
<br />
initialize_fov()<br />
<br />
def new_game():<br />
global player, inventory, game_msgs, game_state, dungeon_level<br />
<br />
#create object representing the player<br />
fighter_component = Fighter(hp=100, defense=1, power=2, xp=0, death_function=player_death)<br />
player = Object(0, 0, '@', 'player', libtcod.white, blocks=True, fighter=fighter_component)<br />
<br />
player.level = 1<br />
<br />
#generate map (at this point it's not drawn to the screen)<br />
dungeon_level = 1<br />
make_map()<br />
initialize_fov()<br />
<br />
game_state = 'playing'<br />
inventory = []<br />
<br />
#create the list of game messages and their colors, starts empty<br />
game_msgs = []<br />
<br />
#a warm welcoming message!<br />
message('Welcome stranger! Prepare to perish in the Tombs of the Ancient Kings.', libtcod.red)<br />
<br />
#initial equipment: a dagger<br />
equipment_component = Equipment(slot='right hand', power_bonus=2)<br />
obj = Object(0, 0, '-', 'dagger', libtcod.sky, equipment=equipment_component)<br />
inventory.append(obj)<br />
equipment_component.equip()<br />
obj.always_visible = True<br />
<br />
def next_level():<br />
#advance to the next level<br />
global dungeon_level<br />
message('You take a moment to rest, and recover your strength.', libtcod.light_violet)<br />
player.fighter.heal(player.fighter.max_hp / 2) #heal the player by 50%<br />
<br />
dungeon_level += 1<br />
message('After a rare moment of peace, you descend deeper into the heart of the dungeon...', libtcod.red)<br />
make_map() #create a fresh new level!<br />
initialize_fov()<br />
<br />
def initialize_fov():<br />
global fov_recompute, fov_map<br />
fov_recompute = True<br />
<br />
#create the FOV map, according to the generated map<br />
fov_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)<br />
for y in range(MAP_HEIGHT):<br />
for x in range(MAP_WIDTH):<br />
libtcod.map_set_properties(fov_map, x, y, not map[x][y].block_sight, not map[x][y].blocked)<br />
<br />
libtcod.console_clear(con) #unexplored areas start black (which is the default background color)<br />
<br />
def play_game():<br />
global key, mouse<br />
<br />
player_action = None<br />
<br />
mouse = libtcod.Mouse()<br />
key = libtcod.Key()<br />
#main loop<br />
while not libtcod.console_is_window_closed():<br />
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)<br />
#render the screen<br />
render_all()<br />
<br />
libtcod.console_flush()<br />
<br />
#level up if needed<br />
check_level_up()<br />
<br />
#erase all objects at their old locations, before they move<br />
for object in objects:<br />
object.clear()<br />
<br />
#handle keys and exit game if needed<br />
player_action = handle_keys()<br />
if player_action == 'exit':<br />
save_game()<br />
break<br />
<br />
#let monsters take their turn<br />
if game_state == 'playing' and player_action != 'didnt-take-turn':<br />
for object in objects:<br />
if object.ai:<br />
object.ai.take_turn()<br />
<br />
def main_menu():<br />
img = libtcod.image_load('menu_background.png')<br />
<br />
while not libtcod.console_is_window_closed():<br />
#show the background image, at twice the regular console resolution<br />
libtcod.image_blit_2x(img, 0, 0, 0)<br />
<br />
#show the game's title, and some credits!<br />
libtcod.console_set_default_foreground(0, libtcod.light_yellow)<br />
libtcod.console_print_ex(0, SCREEN_WIDTH/2, SCREEN_HEIGHT/2-4, libtcod.BKGND_NONE, libtcod.CENTER,<br />
'TOMBS OF THE ANCIENT KINGS')<br />
libtcod.console_print_ex(0, SCREEN_WIDTH/2, SCREEN_HEIGHT-2, libtcod.BKGND_NONE, libtcod.CENTER, 'By Jotaf')<br />
<br />
#show options and wait for the player's choice<br />
choice = menu('', ['Play a new game', 'Continue last game', 'Quit'], 24)<br />
<br />
if choice == 0: #new game<br />
new_game()<br />
play_game()<br />
if choice == 1: #load last game<br />
try:<br />
load_game()<br />
except:<br />
msgbox('\n No saved game to load.\n', 24)<br />
continue<br />
play_game()<br />
elif choice == 2: #quit<br />
break<br />
<br />
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)<br />
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)<br />
libtcod.sys_set_fps(LIMIT_FPS)<br />
con = libtcod.console_new(MAP_WIDTH, MAP_HEIGHT)<br />
panel = libtcod.console_new(SCREEN_WIDTH, PANEL_HEIGHT)<br />
<br />
main_menu()<br />
<br />
</syntaxhighlight></div><br />
<br />
[[Category:Developing]]</div>Bixhttp://roguebasin.com/index.php?title=Complete_Roguelike_Tutorial,_using_python3%2Blibtcod,_part_13_code&diff=47055Complete Roguelike Tutorial, using python3+libtcod, part 13 code2018-05-07T16:03:33Z<p>Bix: </p>
<hr />
<div><center><table border="0" cellpadding="10" cellspacing="0" style="background:#F0E68C"><tr><td><center><br />
This is part of a series of tutorials; the main page can be found [[Complete Roguelike Tutorial, using python3+libtcod|here]].<br />
</center></td></tr></table></center><br />
<br />
<br />
== Adventure gear ==<br />
<div style="background-color: #EEEEEE; border-style: dotted"><syntaxhighlight lang="python"><br />
#!/usr/bin/python<br />
#<br />
# libtcod python tutorial<br />
#<br />
<br />
import libtcodpy as libtcod<br />
import math<br />
import textwrap<br />
import shelve<br />
<br />
<br />
#actual size of the window<br />
SCREEN_WIDTH = 80<br />
SCREEN_HEIGHT = 50<br />
<br />
#size of the map<br />
MAP_WIDTH = 80<br />
MAP_HEIGHT = 43<br />
<br />
#sizes and coordinates relevant for the GUI<br />
BAR_WIDTH = 20<br />
PANEL_HEIGHT = 7<br />
PANEL_Y = SCREEN_HEIGHT - PANEL_HEIGHT<br />
MSG_X = BAR_WIDTH + 2<br />
MSG_WIDTH = SCREEN_WIDTH - BAR_WIDTH - 2<br />
MSG_HEIGHT = PANEL_HEIGHT - 1<br />
INVENTORY_WIDTH = 50<br />
CHARACTER_SCREEN_WIDTH = 30<br />
LEVEL_SCREEN_WIDTH = 40<br />
<br />
#parameters for dungeon generator<br />
ROOM_MAX_SIZE = 10<br />
ROOM_MIN_SIZE = 6<br />
MAX_ROOMS = 30<br />
<br />
#spell values<br />
HEAL_AMOUNT = 40<br />
LIGHTNING_DAMAGE = 40<br />
LIGHTNING_RANGE = 5<br />
CONFUSE_RANGE = 8<br />
CONFUSE_NUM_TURNS = 10<br />
FIREBALL_RADIUS = 3<br />
FIREBALL_DAMAGE = 25<br />
<br />
#experience and level-ups<br />
LEVEL_UP_BASE = 200<br />
LEVEL_UP_FACTOR = 150<br />
<br />
<br />
FOV_ALGO = 0 #default FOV algorithm<br />
FOV_LIGHT_WALLS = True #light walls or not<br />
TORCH_RADIUS = 10<br />
<br />
LIMIT_FPS = 20 #20 frames-per-second maximum<br />
<br />
<br />
color_dark_wall = libtcod.Color(0, 0, 100)<br />
color_light_wall = libtcod.Color(130, 110, 50)<br />
color_dark_ground = libtcod.Color(50, 50, 150)<br />
color_light_ground = libtcod.Color(200, 180, 50)<br />
<br />
<br />
class Tile:<br />
#a tile of the map and its properties<br />
def __init__(self, blocked, block_sight = None):<br />
self.blocked = blocked<br />
<br />
#all tiles start unexplored<br />
self.explored = False<br />
<br />
#by default, if a tile is blocked, it also blocks sight<br />
if block_sight is None: block_sight = blocked<br />
self.block_sight = block_sight<br />
<br />
class Rect:<br />
#a rectangle on the map. used to characterize a room.<br />
def __init__(self, x, y, w, h):<br />
self.x1 = x<br />
self.y1 = y<br />
self.x2 = x + w<br />
self.y2 = y + h<br />
<br />
def center(self):<br />
center_x = (self.x1 + self.x2) / 2<br />
center_y = (self.y1 + self.y2) / 2<br />
return (center_x, center_y)<br />
<br />
def intersect(self, other):<br />
#returns true if this rectangle intersects with another one<br />
return (self.x1 <= other.x2 and self.x2 >= other.x1 and<br />
self.y1 <= other.y2 and self.y2 >= other.y1)<br />
<br />
class Object:<br />
#this is a generic object: the player, a monster, an item, the stairs...<br />
#it's always represented by a character on screen.<br />
def __init__(self, x, y, char, name, color, blocks=False, always_visible=False, fighter=None, ai=None, item=None, equipment=None):<br />
self.x = x<br />
self.y = y<br />
self.char = char<br />
self.name = name<br />
self.color = color<br />
self.blocks = blocks<br />
self.always_visible = always_visible<br />
self.fighter = fighter<br />
if self.fighter: #let the fighter component know who owns it<br />
self.fighter.owner = self<br />
<br />
self.ai = ai<br />
if self.ai: #let the AI component know who owns it<br />
self.ai.owner = self<br />
<br />
self.item = item<br />
if self.item: #let the Item component know who owns it<br />
self.item.owner = self<br />
<br />
self.equipment = equipment<br />
if self.equipment: #let the Equipment component know who owns it<br />
self.equipment.owner = self<br />
<br />
#there must be an Item component for the Equipment component to work properly<br />
self.item = Item()<br />
self.item.owner = self<br />
<br />
def move(self, dx, dy):<br />
#move by the given amount, if the destination is not blocked<br />
if not is_blocked(self.x + dx, self.y + dy):<br />
self.x += dx<br />
self.y += dy<br />
<br />
def move_towards(self, target_x, target_y):<br />
#vector from this object to the target, and distance<br />
dx = target_x - self.x<br />
dy = target_y - self.y<br />
distance = math.sqrt(dx ** 2 + dy ** 2)<br />
<br />
#normalize it to length 1 (preserving direction), then round it and<br />
#convert to integer so the movement is restricted to the map grid<br />
dx = int(round(dx / distance))<br />
dy = int(round(dy / distance))<br />
self.move(dx, dy)<br />
<br />
def distance_to(self, other):<br />
#return the distance to another object<br />
dx = other.x - self.x<br />
dy = other.y - self.y<br />
return math.sqrt(dx ** 2 + dy ** 2)<br />
<br />
def distance(self, x, y):<br />
#return the distance to some coordinates<br />
return math.sqrt((x - self.x) ** 2 + (y - self.y) ** 2)<br />
<br />
def send_to_back(self):<br />
#make this object be drawn first, so all others appear above it if they're in the same tile.<br />
global objects<br />
objects.remove(self)<br />
objects.insert(0, self)<br />
<br />
def draw(self):<br />
#only show if it's visible to the player; or it's set to "always visible" and on an explored tile<br />
if (libtcod.map_is_in_fov(fov_map, self.x, self.y) or<br />
(self.always_visible and map[self.x][self.y].explored)):<br />
#set the color and then draw the character that represents this object at its position<br />
libtcod.console_set_default_foreground(con, self.color)<br />
libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)<br />
<br />
def clear(self):<br />
#erase the character that represents this object<br />
libtcod.console_put_char(con, self.x, self.y, ' ', libtcod.BKGND_NONE)<br />
<br />
<br />
class Fighter:<br />
#combat-related properties and methods (monster, player, NPC).<br />
def __init__(self, hp, defense, power, xp, death_function=None):<br />
self.base_max_hp = hp<br />
self.hp = hp<br />
self.base_defense = defense<br />
self.base_power = power<br />
self.xp = xp<br />
self.death_function = death_function<br />
<br />
@property<br />
def power(self): #return actual power, by summing up the bonuses from all equipped items<br />
bonus = sum(equipment.power_bonus for equipment in get_all_equipped(self.owner))<br />
return self.base_power + bonus<br />
<br />
@property<br />
def defense(self): #return actual defense, by summing up the bonuses from all equipped items<br />
bonus = sum(equipment.defense_bonus for equipment in get_all_equipped(self.owner))<br />
return self.base_defense + bonus<br />
<br />
@property<br />
def max_hp(self): #return actual max_hp, by summing up the bonuses from all equipped items<br />
bonus = sum(equipment.max_hp_bonus for equipment in get_all_equipped(self.owner))<br />
return self.base_max_hp + bonus<br />
<br />
def attack(self, target):<br />
#a simple formula for attack damage<br />
damage = self.power - target.fighter.defense<br />
<br />
if damage > 0:<br />
#make the target take some damage<br />
message(self.owner.name.capitalize() + ' attacks ' + target.name + ' for ' + str(damage) + ' hit points.')<br />
target.fighter.take_damage(damage)<br />
else:<br />
message(self.owner.name.capitalize() + ' attacks ' + target.name + ' but it has no effect!')<br />
<br />
def take_damage(self, damage):<br />
#apply damage if possible<br />
if damage > 0:<br />
self.hp -= damage<br />
<br />
#check for death. if there's a death function, call it<br />
if self.hp <= 0:<br />
function = self.death_function<br />
if function is not None:<br />
function(self.owner)<br />
<br />
if self.owner != player: #yield experience to the player<br />
player.fighter.xp += self.xp<br />
<br />
def heal(self, amount):<br />
#heal by the given amount, without going over the maximum<br />
self.hp += amount<br />
if self.hp > self.max_hp:<br />
self.hp = self.max_hp<br />
<br />
class BasicMonster:<br />
#AI for a basic monster.<br />
def take_turn(self):<br />
#a basic monster takes its turn. if you can see it, it can see you<br />
monster = self.owner<br />
if libtcod.map_is_in_fov(fov_map, monster.x, monster.y):<br />
<br />
#move towards player if far away<br />
if monster.distance_to(player) >= 2:<br />
monster.move_towards(player.x, player.y)<br />
<br />
#close enough, attack! (if the player is still alive.)<br />
elif player.fighter.hp > 0:<br />
monster.fighter.attack(player)<br />
<br />
class ConfusedMonster:<br />
#AI for a temporarily confused monster (reverts to previous AI after a while).<br />
def __init__(self, old_ai, num_turns=CONFUSE_NUM_TURNS):<br />
self.old_ai = old_ai<br />
self.num_turns = num_turns<br />
<br />
def take_turn(self):<br />
if self.num_turns > 0: #still confused...<br />
#move in a random direction, and decrease the number of turns confused<br />
self.owner.move(libtcod.random_get_int(0, -1, 1), libtcod.random_get_int(0, -1, 1))<br />
self.num_turns -= 1<br />
<br />
else: #restore the previous AI (this one will be deleted because it's not referenced anymore)<br />
self.owner.ai = self.old_ai<br />
message('The ' + self.owner.name + ' is no longer confused!', libtcod.red)<br />
<br />
class Item:<br />
#an item that can be picked up and used.<br />
def __init__(self, use_function=None):<br />
self.use_function = use_function<br />
<br />
def pick_up(self):<br />
#add to the player's inventory and remove from the map<br />
if len(inventory) >= 26:<br />
message('Your inventory is full, cannot pick up ' + self.owner.name + '.', libtcod.red)<br />
else:<br />
inventory.append(self.owner)<br />
objects.remove(self.owner)<br />
message('You picked up a ' + self.owner.name + '!', libtcod.green)<br />
<br />
#special case: automatically equip, if the corresponding equipment slot is unused<br />
equipment = self.owner.equipment<br />
if equipment and get_equipped_in_slot(equipment.slot) is None:<br />
equipment.equip()<br />
<br />
def drop(self):<br />
#special case: if the object has the Equipment component, dequip it before dropping<br />
if self.owner.equipment:<br />
self.owner.equipment.dequip()<br />
<br />
#add to the map and remove from the player's inventory. also, place it at the player's coordinates<br />
objects.append(self.owner)<br />
inventory.remove(self.owner)<br />
self.owner.x = player.x<br />
self.owner.y = player.y<br />
message('You dropped a ' + self.owner.name + '.', libtcod.yellow)<br />
<br />
def use(self):<br />
#special case: if the object has the Equipment component, the "use" action is to equip/dequip<br />
if self.owner.equipment:<br />
self.owner.equipment.toggle_equip()<br />
return<br />
<br />
#just call the "use_function" if it is defined<br />
if self.use_function is None:<br />
message('The ' + self.owner.name + ' cannot be used.')<br />
else:<br />
if self.use_function() != 'cancelled':<br />
inventory.remove(self.owner) #destroy after use, unless it was cancelled for some reason<br />
<br />
class Equipment:<br />
#an object that can be equipped, yielding bonuses. automatically adds the Item component.<br />
def __init__(self, slot, power_bonus=0, defense_bonus=0, max_hp_bonus=0):<br />
self.power_bonus = power_bonus<br />
self.defense_bonus = defense_bonus<br />
self.max_hp_bonus = max_hp_bonus<br />
<br />
self.slot = slot<br />
self.is_equipped = False<br />
<br />
def toggle_equip(self): #toggle equip/dequip status<br />
if self.is_equipped:<br />
self.dequip()<br />
else:<br />
self.equip()<br />
<br />
def equip(self):<br />
#if the slot is already being used, dequip whatever is there first<br />
old_equipment = get_equipped_in_slot(self.slot)<br />
if old_equipment is not None:<br />
old_equipment.dequip()<br />
<br />
#equip object and show a message about it<br />
self.is_equipped = True<br />
message('Equipped ' + self.owner.name + ' on ' + self.slot + '.', libtcod.light_green)<br />
<br />
def dequip(self):<br />
#dequip object and show a message about it<br />
if not self.is_equipped: return<br />
self.is_equipped = False<br />
message('Dequipped ' + self.owner.name + ' from ' + self.slot + '.', libtcod.light_yellow)<br />
<br />
<br />
def get_equipped_in_slot(slot): #returns the equipment in a slot, or None if it's empty<br />
for obj in inventory:<br />
if obj.equipment and obj.equipment.slot == slot and obj.equipment.is_equipped:<br />
return obj.equipment<br />
return None<br />
<br />
def get_all_equipped(obj): #returns a list of equipped items<br />
if obj == player:<br />
equipped_list = []<br />
for item in inventory:<br />
if item.equipment and item.equipment.is_equipped:<br />
equipped_list.append(item.equipment)<br />
return equipped_list<br />
else:<br />
return [] #other objects have no equipment<br />
<br />
<br />
def is_blocked(x, y):<br />
#first test the map tile<br />
if map[x][y].blocked:<br />
return True<br />
<br />
#now check for any blocking objects<br />
for object in objects:<br />
if object.blocks and object.x == x and object.y == y:<br />
return True<br />
<br />
return False<br />
<br />
def create_room(room):<br />
global map<br />
#go through the tiles in the rectangle and make them passable<br />
for x in range(room.x1 + 1, room.x2):<br />
for y in range(room.y1 + 1, room.y2):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
def create_h_tunnel(x1, x2, y):<br />
global map<br />
#horizontal tunnel. min() and max() are used in case x1>x2<br />
for x in range(min(x1, x2), max(x1, x2) + 1):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
def create_v_tunnel(y1, y2, x):<br />
global map<br />
#vertical tunnel<br />
for y in range(min(y1, y2), max(y1, y2) + 1):<br />
map[x][y].blocked = False<br />
map[x][y].block_sight = False<br />
<br />
def make_map():<br />
global map, objects, stairs<br />
<br />
#the list of objects with just the player<br />
objects = [player]<br />
<br />
#fill map with "blocked" tiles<br />
map = [[ Tile(True)<br />
for y in range(MAP_HEIGHT) ]<br />
for x in range(MAP_WIDTH) ]<br />
<br />
rooms = []<br />
num_rooms = 0<br />
<br />
for r in range(MAX_ROOMS):<br />
#random width and height<br />
w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)<br />
h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE)<br />
#random position without going out of the boundaries of the map<br />
x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1)<br />
y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1)<br />
<br />
#"Rect" class makes rectangles easier to work with<br />
new_room = Rect(x, y, w, h)<br />
<br />
#run through the other rooms and see if they intersect with this one<br />
failed = False<br />
for other_room in rooms:<br />
if new_room.intersect(other_room):<br />
failed = True<br />
break<br />
<br />
if not failed:<br />
#this means there are no intersections, so this room is valid<br />
<br />
#"paint" it to the map's tiles<br />
create_room(new_room)<br />
<br />
#center coordinates of new room, will be useful later<br />
(new_x, new_y) = new_room.center()<br />
<br />
if num_rooms == 0:<br />
#this is the first room, where the player starts at<br />
player.x = new_x<br />
player.y = new_y<br />
else:<br />
#all rooms after the first:<br />
#connect it to the previous room with a tunnel<br />
<br />
#center coordinates of previous room<br />
(prev_x, prev_y) = rooms[num_rooms-1].center()<br />
<br />
#draw a coin (random number that is either 0 or 1)<br />
if libtcod.random_get_int(0, 0, 1) == 1:<br />
#first move horizontally, then vertically<br />
create_h_tunnel(prev_x, new_x, prev_y)<br />
create_v_tunnel(prev_y, new_y, new_x)<br />
else:<br />
#first move vertically, then horizontally<br />
create_v_tunnel(prev_y, new_y, prev_x)<br />
create_h_tunnel(prev_x, new_x, new_y)<br />
<br />
#add some contents to this room, such as monsters<br />
place_objects(new_room)<br />
<br />
#finally, append the new room to the list<br />
rooms.append(new_room)<br />
num_rooms += 1<br />
<br />
#create stairs at the center of the last room<br />
stairs = Object(new_x, new_y, '<', 'stairs', libtcod.white, always_visible=True)<br />
objects.append(stairs)<br />
stairs.send_to_back() #so it's drawn below the monsters<br />
<br />
def random_choice_index(chances): #choose one option from list of chances, returning its index<br />
#the dice will land on some number between 1 and the sum of the chances<br />
dice = libtcod.random_get_int(0, 1, sum(chances))<br />
<br />
#go through all chances, keeping the sum so far<br />
running_sum = 0<br />
choice = 0<br />
for w in chances:<br />
running_sum += w<br />
<br />
#see if the dice landed in the part that corresponds to this choice<br />
if dice <= running_sum:<br />
return choice<br />
choice += 1<br />
<br />
def random_choice(chances_dict):<br />
#choose one option from dictionary of chances, returning its key<br />
chances = chances_dict.values()<br />
strings = chances_dict.keys()<br />
<br />
return strings[random_choice_index(chances)]<br />
<br />
def from_dungeon_level(table):<br />
#returns a value that depends on level. the table specifies what value occurs after each level, default is 0.<br />
for (value, level) in reversed(table):<br />
if dungeon_level >= level:<br />
return value<br />
return 0<br />
<br />
def place_objects(room):<br />
#this is where we decide the chance of each monster or item appearing.<br />
<br />
#maximum number of monsters per room<br />
max_monsters = from_dungeon_level([[2, 1], [3, 4], [5, 6]])<br />
<br />
#chance of each monster<br />
monster_chances = {}<br />
monster_chances['orc'] = 80 #orc always shows up, even if all other monsters have 0 chance<br />
monster_chances['troll'] = from_dungeon_level([[15, 3], [30, 5], [60, 7]])<br />
<br />
#maximum number of items per room<br />
max_items = from_dungeon_level([[1, 1], [2, 4]])<br />
<br />
#chance of each item (by default they have a chance of 0 at level 1, which then goes up)<br />
item_chances = {}<br />
item_chances['heal'] = 35 #healing potion always shows up, even if all other items have 0 chance<br />
item_chances['lightning'] = from_dungeon_level([[25, 4]])<br />
item_chances['fireball'] = from_dungeon_level([[25, 6]])<br />
item_chances['confuse'] = from_dungeon_level([[10, 2]])<br />
item_chances['sword'] = from_dungeon_level([[5, 4]])<br />
item_chances['shield'] = from_dungeon_level([[15, 8]])<br />
<br />
<br />
#choose random number of monsters<br />
num_monsters = libtcod.random_get_int(0, 0, max_monsters)<br />
<br />
for i in range(num_monsters):<br />
#choose random spot for this monster<br />
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)<br />
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)<br />
<br />
#only place it if the tile is not blocked<br />
if not is_blocked(x, y):<br />
choice = random_choice(monster_chances)<br />
if choice == 'orc':<br />
#create an orc<br />
fighter_component = Fighter(hp=20, defense=0, power=4, xp=35, death_function=monster_death)<br />
ai_component = BasicMonster()<br />
<br />
monster = Object(x, y, 'o', 'orc', libtcod.desaturated_green,<br />
blocks=True, fighter=fighter_component, ai=ai_component)<br />
<br />
elif choice == 'troll':<br />
#create a troll<br />
fighter_component = Fighter(hp=30, defense=2, power=8, xp=100, death_function=monster_death)<br />
ai_component = BasicMonster()<br />
<br />
monster = Object(x, y, 'T', 'troll', libtcod.darker_green,<br />
blocks=True, fighter=fighter_component, ai=ai_component)<br />
<br />
objects.append(monster)<br />
<br />
#choose random number of items<br />
num_items = libtcod.random_get_int(0, 0, max_items)<br />
<br />
for i in range(num_items):<br />
#choose random spot for this item<br />
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)<br />
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)<br />
<br />
#only place it if the tile is not blocked<br />
if not is_blocked(x, y):<br />
choice = random_choice(item_chances)<br />
if choice == 'heal':<br />
#create a healing potion<br />
item_component = Item(use_function=cast_heal)<br />
item = Object(x, y, '!', 'healing potion', libtcod.violet, item=item_component)<br />
<br />
elif choice == 'lightning':<br />
#create a lightning bolt scroll<br />
item_component = Item(use_function=cast_lightning)<br />
item = Object(x, y, '#', 'scroll of lightning bolt', libtcod.light_yellow, item=item_component)<br />
<br />
elif choice == 'fireball':<br />
#create a fireball scroll<br />
item_component = Item(use_function=cast_fireball)<br />
item = Object(x, y, '#', 'scroll of fireball', libtcod.light_yellow, item=item_component)<br />
<br />
elif choice == 'confuse':<br />
#create a confuse scroll<br />
item_component = Item(use_function=cast_confuse)<br />
item = Object(x, y, '#', 'scroll of confusion', libtcod.light_yellow, item=item_component)<br />
<br />
elif choice == 'sword':<br />
#create a sword<br />
equipment_component = Equipment(slot='right hand', power_bonus=3)<br />
item = Object(x, y, '/', 'sword', libtcod.sky, equipment=equipment_component)<br />
<br />
elif choice == 'shield':<br />
#create a shield<br />
equipment_component = Equipment(slot='left hand', defense_bonus=1)<br />
item = Object(x, y, '[', 'shield', libtcod.darker_orange, equipment=equipment_component)<br />
<br />
objects.append(item)<br />
item.send_to_back() #items appear below other objects<br />
item.always_visible = True #items are visible even out-of-FOV, if in an explored area<br />
<br />
<br />
def render_bar(x, y, total_width, name, value, maximum, bar_color, back_color):<br />
#render a bar (HP, experience, etc). first calculate the width of the bar<br />
bar_width = int(float(value) / maximum * total_width)<br />
<br />
#render the background first<br />
libtcod.console_set_default_background(panel, back_color)<br />
libtcod.console_rect(panel, x, y, total_width, 1, False, libtcod.BKGND_SCREEN)<br />
<br />
#now render the bar on top<br />
libtcod.console_set_default_background(panel, bar_color)<br />
if bar_width > 0:<br />
libtcod.console_rect(panel, x, y, bar_width, 1, False, libtcod.BKGND_SCREEN)<br />
<br />
#finally, some centered text with the values<br />
libtcod.console_set_default_foreground(panel, libtcod.white)<br />
libtcod.console_print_ex(panel, x + total_width / 2, y, libtcod.BKGND_NONE, libtcod.CENTER,<br />
name + ': ' + str(value) + '/' + str(maximum))<br />
<br />
def get_names_under_mouse():<br />
global mouse<br />
#return a string with the names of all objects under the mouse<br />
<br />
(x, y) = (mouse.cx, mouse.cy)<br />
<br />
#create a list with the names of all objects at the mouse's coordinates and in FOV<br />
names = [obj.name for obj in objects<br />
if obj.x == x and obj.y == y and libtcod.map_is_in_fov(fov_map, obj.x, obj.y)]<br />
<br />
names = ', '.join(names) #join the names, separated by commas<br />
return names.capitalize()<br />
<br />
def render_all():<br />
global fov_map, color_dark_wall, color_light_wall<br />
global color_dark_ground, color_light_ground<br />
global fov_recompute<br />
<br />
if fov_recompute:<br />
#recompute FOV if needed (the player moved or something)<br />
fov_recompute = False<br />
libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO)<br />
<br />
#go through all tiles, and set their background color according to the FOV<br />
for y in range(MAP_HEIGHT):<br />
for x in range(MAP_WIDTH):<br />
visible = libtcod.map_is_in_fov(fov_map, x, y)<br />
wall = map[x][y].block_sight<br />
if not visible:<br />
#if it's not visible right now, the player can only see it if it's explored<br />
if map[x][y].explored:<br />
if wall:<br />
libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET)<br />
else:<br />
libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET)<br />
else:<br />
#it's visible<br />
if wall:<br />
libtcod.console_set_char_background(con, x, y, color_light_wall, libtcod.BKGND_SET )<br />
else:<br />
libtcod.console_set_char_background(con, x, y, color_light_ground, libtcod.BKGND_SET )<br />
#since it's visible, explore it<br />
map[x][y].explored = True<br />
<br />
#draw all objects in the list, except the player. we want it to<br />
#always appear over all other objects! so it's drawn later.<br />
for object in objects:<br />
if object != player:<br />
object.draw()<br />
player.draw()<br />
<br />
#blit the contents of "con" to the root console<br />
libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0)<br />
<br />
<br />
#prepare to render the GUI panel<br />
libtcod.console_set_default_background(panel, libtcod.black)<br />
libtcod.console_clear(panel)<br />
<br />
#print the game messages, one line at a time<br />
y = 1<br />
for (line, color) in game_msgs:<br />
libtcod.console_set_default_foreground(panel, color)<br />
libtcod.console_print_ex(panel, MSG_X, y, libtcod.BKGND_NONE, libtcod.LEFT,line)<br />
y += 1<br />
<br />
#show the player's stats<br />
render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp,<br />
libtcod.light_red, libtcod.darker_red)<br />
libtcod.console_print_ex(panel, 1, 3, libtcod.BKGND_NONE, libtcod.LEFT, 'Dungeon level ' + str(dungeon_level))<br />
<br />
#display names of objects under the mouse<br />
libtcod.console_set_default_foreground(panel, libtcod.light_gray)<br />
libtcod.console_print_ex(panel, 1, 0, libtcod.BKGND_NONE, libtcod.LEFT, get_names_under_mouse())<br />
<br />
#blit the contents of "panel" to the root console<br />
libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)<br />
<br />
<br />
def message(new_msg, color = libtcod.white):<br />
#split the message if necessary, among multiple lines<br />
new_msg_lines = textwrap.wrap(new_msg, MSG_WIDTH)<br />
<br />
for line in new_msg_lines:<br />
#if the buffer is full, remove the first line to make room for the new one<br />
if len(game_msgs) == MSG_HEIGHT:<br />
del game_msgs[0]<br />
<br />
#add the new line as a tuple, with the text and the color<br />
game_msgs.append( (line, color) )<br />
<br />
<br />
def player_move_or_attack(dx, dy):<br />
global fov_recompute<br />
<br />
#the coordinates the player is moving to/attacking<br />
x = player.x + dx<br />
y = player.y + dy<br />
<br />
#try to find an attackable object there<br />
target = None<br />
for object in objects:<br />
if object.fighter and object.x == x and object.y == y:<br />
target = object<br />
break<br />
<br />
#attack if target found, move otherwise<br />
if target is not None:<br />
player.fighter.attack(target)<br />
else:<br />
player.move(dx, dy)<br />
fov_recompute = True<br />
<br />
<br />
def menu(header, options, width):<br />
if len(options) > 26: raise ValueError('Cannot have a menu with more than 26 options.')<br />
<br />
#calculate total height for the header (after auto-wrap) and one line per option<br />
header_height = libtcod.console_get_height_rect(con, 0, 0, width, SCREEN_HEIGHT, header)<br />
if header == '':<br />
header_height = 0<br />
height = len(options) + header_height<br />
<br />
#create an off-screen console that represents the menu's window<br />
window = libtcod.console_new(width, height)<br />
<br />
#print the header, with auto-wrap<br />
libtcod.console_set_default_foreground(window, libtcod.white)<br />
libtcod.console_print_rect_ex(window, 0, 0, width, height, libtcod.BKGND_NONE, libtcod.LEFT, header)<br />
<br />
#print all the options<br />
y = header_height<br />
letter_index = ord('a')<br />
for option_text in options:<br />
text = '(' + chr(letter_index) + ') ' + option_text<br />
libtcod.console_print_ex(window, 0, y, libtcod.BKGND_NONE, libtcod.LEFT, text)<br />
y += 1<br />
letter_index += 1<br />
<br />
#blit the contents of "window" to the root console<br />
x = SCREEN_WIDTH/2 - width/2<br />
y = SCREEN_HEIGHT/2 - height/2<br />
libtcod.console_blit(window, 0, 0, width, height, 0, x, y, 1.0, 0.7)<br />
<br />
#present the root console to the player and wait for a key-press<br />
libtcod.console_flush()<br />
key = libtcod.console_wait_for_keypress(True)<br />
<br />
if key.vk == libtcod.KEY_ENTER and key.lalt: #(special case) Alt+Enter: toggle fullscreen<br />
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen)<br />
<br />
#convert the ASCII code to an index; if it corresponds to an option, return it<br />
index = key.c - ord('a')<br />
if index >= 0 and index < len(options): return index<br />
return None<br />
<br />
def inventory_menu(header):<br />
#show a menu with each item of the inventory as an option<br />
if len(inventory) == 0:<br />
options = ['Inventory is empty.']<br />
else:<br />
options = []<br />
for item in inventory:<br />
text = item.name<br />
#show additional information, in case it's equipped<br />
if item.equipment and item.equipment.is_equipped:<br />
text = text + ' (on ' + item.equipment.slot + ')'<br />
options.append(text)<br />
<br />
index = menu(header, options, INVENTORY_WIDTH)<br />
<br />
#if an item was chosen, return it<br />
if index is None or len(inventory) == 0: return None<br />
return inventory[index].item<br />
<br />
def msgbox(text, width=50):<br />
menu(text, [], width) #use menu() as a sort of "message box"<br />
<br />
def handle_keys():<br />
global key<br />
<br />
if key.vk == libtcod.KEY_ENTER and key.lalt:<br />
#Alt+Enter: toggle fullscreen<br />
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())<br />
<br />
elif key.vk == libtcod.KEY_ESCAPE:<br />
return 'exit' #exit game<br />
<br />
if game_state == 'playing':<br />
#movement keys<br />
if key.vk == libtcod.KEY_UP or key.vk == libtcod.KEY_KP8:<br />
player_move_or_attack(0, -1)<br />
elif key.vk == libtcod.KEY_DOWN or key.vk == libtcod.KEY_KP2:<br />
player_move_or_attack(0, 1)<br />
elif key.vk == libtcod.KEY_LEFT or key.vk == libtcod.KEY_KP4:<br />
player_move_or_attack(-1, 0)<br />
elif key.vk == libtcod.KEY_RIGHT or key.vk == libtcod.KEY_KP6:<br />
player_move_or_attack(1, 0)<br />
elif key.vk == libtcod.KEY_HOME or key.vk == libtcod.KEY_KP7:<br />
player_move_or_attack(-1, -1)<br />
elif key.vk == libtcod.KEY_PAGEUP or key.vk == libtcod.KEY_KP9:<br />
player_move_or_attack(1, -1)<br />
elif key.vk == libtcod.KEY_END or key.vk == libtcod.KEY_KP1:<br />
player_move_or_attack(-1, 1)<br />
elif key.vk == libtcod.KEY_PAGEDOWN or key.vk == libtcod.KEY_KP3:<br />
player_move_or_attack(1, 1)<br />
elif key.vk == libtcod.KEY_KP5:<br />
pass #do nothing ie wait for the monster to come to you<br />
else:<br />
#test for other keys<br />
key_char = chr(key.c)<br />
<br />
if key_char == 'g':<br />
#pick up an item<br />
for object in objects: #look for an item in the player's tile<br />
if object.x == player.x and object.y == player.y and object.item:<br />
object.item.pick_up()<br />
break<br />
<br />
if key_char == 'i':<br />
#show the inventory; if an item is selected, use it<br />
chosen_item = inventory_menu('Press the key next to an item to use it, or any other to cancel.\n')<br />
if chosen_item is not None:<br />
chosen_item.use()<br />
<br />
if key_char == 'd':<br />
#show the inventory; if an item is selected, drop it<br />
chosen_item = inventory_menu('Press the key next to an item to drop it, or any other to cancel.\n')<br />
if chosen_item is not None:<br />
chosen_item.drop()<br />
<br />
if key_char == 'c':<br />
#show character information<br />
level_up_xp = LEVEL_UP_BASE + player.level * LEVEL_UP_FACTOR<br />
msgbox('Character Information\n\nLevel: ' + str(player.level) + '\nExperience: ' + str(player.fighter.xp) +<br />
'\nExperience to level up: ' + str(level_up_xp) + '\n\nMaximum HP: ' + str(player.fighter.max_hp) +<br />
'\nAttack: ' + str(player.fighter.power) + '\nDefense: ' + str(player.fighter.defense), CHARACTER_SCREEN_WIDTH)<br />
<br />
if key_char == '<':<br />
#go down stairs, if the player is on them<br />
if stairs.x == player.x and stairs.y == player.y:<br />
next_level()<br />
<br />
return 'didnt-take-turn'<br />
<br />
def check_level_up():<br />
#see if the player's experience is enough to level-up<br />
level_up_xp = LEVEL_UP_BASE + player.level * LEVEL_UP_FACTOR<br />
if player.fighter.xp >= level_up_xp:<br />
#it is! level up and ask to raise some stats<br />
player.level += 1<br />
player.fighter.xp -= level_up_xp<br />
message('Your battle skills grow stronger! You reached level ' + str(player.level) + '!', libtcod.yellow)<br />
<br />
choice = None<br />
while choice == None: #keep asking until a choice is made<br />
choice = menu('Level up! Choose a stat to raise:\n',<br />
['Constitution (+20 HP, from ' + str(player.fighter.max_hp) + ')',<br />
'Strength (+1 attack, from ' + str(player.fighter.power) + ')',<br />
'Agility (+1 defense, from ' + str(player.fighter.defense) + ')'], LEVEL_SCREEN_WIDTH)<br />
<br />
if choice == 0:<br />
player.fighter.base_max_hp += 20<br />
player.fighter.hp += 20<br />
elif choice == 1:<br />
player.fighter.base_power += 1<br />
elif choice == 2:<br />
player.fighter.base_defense += 1<br />
<br />
def player_death(player):<br />
#the game ended!<br />
global game_state<br />
message('You died!', libtcod.red)<br />
game_state = 'dead'<br />
<br />
#for added effect, transform the player into a corpse!<br />
player.char = '%'<br />
player.color = libtcod.dark_red<br />
<br />
def monster_death(monster):<br />
#transform it into a nasty corpse! it doesn't block, can't be<br />
#attacked and doesn't move<br />
message('The ' + monster.name + ' is dead! You gain ' + str(monster.fighter.xp) + ' experience points.', libtcod.orange)<br />
monster.char = '%'<br />
monster.color = libtcod.dark_red<br />
monster.blocks = False<br />
monster.fighter = None<br />
monster.ai = None<br />
monster.name = 'remains of ' + monster.name<br />
monster.send_to_back()<br />
<br />
def target_tile(max_range=None):<br />
global key, mouse<br />
#return the position of a tile left-clicked in player's FOV (optionally in a range), or (None,None) if right-clicked.<br />
while True:<br />
#render the screen. this erases the inventory and shows the names of objects under the mouse.<br />
libtcod.console_flush()<br />
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)<br />
render_all()<br />
<br />
(x, y) = (mouse.cx, mouse.cy)<br />
<br />
if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE:<br />
return (None, None) #cancel if the player right-clicked or pressed Escape<br />
<br />
#accept the target if the player clicked in FOV, and in case a range is specified, if it's in that range<br />
if (mouse.lbutton_pressed and libtcod.map_is_in_fov(fov_map, x, y) and<br />
(max_range is None or player.distance(x, y) <= max_range)):<br />
return (x, y)<br />
<br />
def target_monster(max_range=None):<br />
#returns a clicked monster inside FOV up to a range, or None if right-clicked<br />
while True:<br />
(x, y) = target_tile(max_range)<br />
if x is None: #player cancelled<br />
return None<br />
<br />
#return the first clicked monster, otherwise continue looping<br />
for obj in objects:<br />
if obj.x == x and obj.y == y and obj.fighter and obj != player:<br />
return obj<br />
<br />
def closest_monster(max_range):<br />
#find closest enemy, up to a maximum range, and in the player's FOV<br />
closest_enemy = None<br />
closest_dist = max_range + 1 #start with (slightly more than) maximum range<br />
<br />
for object in objects:<br />
if object.fighter and not object == player and libtcod.map_is_in_fov(fov_map, object.x, object.y):<br />
#calculate distance between this object and the player<br />
dist = player.distance_to(object)<br />
if dist < closest_dist: #it's closer, so remember it<br />
closest_enemy = object<br />
closest_dist = dist<br />
return closest_enemy<br />
<br />
def cast_heal():<br />
#heal the player<br />
if player.fighter.hp == player.fighter.max_hp:<br />
message('You are already at full health.', libtcod.red)<br />
return 'cancelled'<br />
<br />
message('Your wounds start to feel better!', libtcod.light_violet)<br />
player.fighter.heal(HEAL_AMOUNT)<br />
<br />
def cast_lightning():<br />
#find closest enemy (inside a maximum range) and damage it<br />
monster = closest_monster(LIGHTNING_RANGE)<br />
if monster is None: #no enemy found within maximum range<br />
message('No enemy is close enough to strike.', libtcod.red)<br />
return 'cancelled'<br />
<br />
#zap it!<br />
message('A lighting bolt strikes the ' + monster.name + ' with a loud thunder! The damage is '<br />
+ str(LIGHTNING_DAMAGE) + ' hit points.', libtcod.light_blue)<br />
monster.fighter.take_damage(LIGHTNING_DAMAGE)<br />
<br />
def cast_fireball():<br />
#ask the player for a target tile to throw a fireball at<br />
message('Left-click a target tile for the fireball, or right-click to cancel.', libtcod.light_cyan)<br />
(x, y) = target_tile()<br />
if x is None: return 'cancelled'<br />
message('The fireball explodes, burning everything within ' + str(FIREBALL_RADIUS) + ' tiles!', libtcod.orange)<br />
<br />
for obj in objects: #damage every fighter in range, including the player<br />
if obj.distance(x, y) <= FIREBALL_RADIUS and obj.fighter:<br />
message('The ' + obj.name + ' gets burned for ' + str(FIREBALL_DAMAGE) + ' hit points.', libtcod.orange)<br />
obj.fighter.take_damage(FIREBALL_DAMAGE)<br />
<br />
def cast_confuse():<br />
#ask the player for a target to confuse<br />
message('Left-click an enemy to confuse it, or right-click to cancel.', libtcod.light_cyan)<br />
monster = target_monster(CONFUSE_RANGE)<br />
if monster is None: return 'cancelled'<br />
<br />
#replace the monster's AI with a "confused" one; after some turns it will restore the old AI<br />
old_ai = monster.ai<br />
monster.ai = ConfusedMonster(old_ai)<br />
monster.ai.owner = monster #tell the new component who owns it<br />
message('The eyes of the ' + monster.name + ' look vacant, as he starts to stumble around!', libtcod.light_green)<br />
<br />
<br />
def save_game():<br />
#open a new empty shelve (possibly overwriting an old one) to write the game data<br />
file = shelve.open('savegame', 'n')<br />
file['map'] = map<br />
file['objects'] = objects<br />
file['player_index'] = objects.index(player) #index of player in objects list<br />
file['stairs_index'] = objects.index(stairs) #same for the stairs<br />
file['inventory'] = inventory<br />
file['game_msgs'] = game_msgs<br />
file['game_state'] = game_state<br />
file['dungeon_level'] = dungeon_level<br />
file.close()<br />
<br />
def load_game():<br />
#open the previously saved shelve and load the game data<br />
global map, objects, player, stairs, inventory, game_msgs, game_state, dungeon_level<br />
<br />
file = shelve.open('savegame', 'r')<br />
map = file['map']<br />
objects = file['objects']<br />
player = objects[file['player_index']] #get index of player in objects list and access it<br />
stairs = objects[file['stairs_index']] #same for the stairs<br />
inventory = file['inventory']<br />
game_msgs = file['game_msgs']<br />
game_state = file['game_state']<br />
dungeon_level = file['dungeon_level']<br />
file.close()<br />
<br />
initialize_fov()<br />
<br />
def new_game():<br />
global player, inventory, game_msgs, game_state, dungeon_level<br />
<br />
#create object representing the player<br />
fighter_component = Fighter(hp=100, defense=1, power=2, xp=0, death_function=player_death)<br />
player = Object(0, 0, '@', 'player', libtcod.white, blocks=True, fighter=fighter_component)<br />
<br />
player.level = 1<br />
<br />
#generate map (at this point it's not drawn to the screen)<br />
dungeon_level = 1<br />
make_map()<br />
initialize_fov()<br />
<br />
game_state = 'playing'<br />
inventory = []<br />
<br />
#create the list of game messages and their colors, starts empty<br />
game_msgs = []<br />
<br />
#a warm welcoming message!<br />
message('Welcome stranger! Prepare to perish in the Tombs of the Ancient Kings.', libtcod.red)<br />
<br />
#initial equipment: a dagger<br />
equipment_component = Equipment(slot='right hand', power_bonus=2)<br />
obj = Object(0, 0, '-', 'dagger', libtcod.sky, equipment=equipment_component)<br />
inventory.append(obj)<br />
equipment_component.equip()<br />
obj.always_visible = True<br />
<br />
def next_level():<br />
#advance to the next level<br />
global dungeon_level<br />
message('You take a moment to rest, and recover your strength.', libtcod.light_violet)<br />
player.fighter.heal(player.fighter.max_hp / 2) #heal the player by 50%<br />
<br />
dungeon_level += 1<br />
message('After a rare moment of peace, you descend deeper into the heart of the dungeon...', libtcod.red)<br />
make_map() #create a fresh new level!<br />
initialize_fov()<br />
<br />
def initialize_fov():<br />
global fov_recompute, fov_map<br />
fov_recompute = True<br />
<br />
#create the FOV map, according to the generated map<br />
fov_map = libtcod.map_new(MAP_WIDTH, MAP_HEIGHT)<br />
for y in range(MAP_HEIGHT):<br />
for x in range(MAP_WIDTH):<br />
libtcod.map_set_properties(fov_map, x, y, not map[x][y].block_sight, not map[x][y].blocked)<br />
<br />
libtcod.console_clear(con) #unexplored areas start black (which is the default background color)<br />
<br />
def play_game():<br />
global key, mouse<br />
<br />
player_action = None<br />
<br />
mouse = libtcod.Mouse()<br />
key = libtcod.Key()<br />
#main loop<br />
while not libtcod.console_is_window_closed():<br />
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)<br />
#render the screen<br />
render_all()<br />
<br />
libtcod.console_flush()<br />
<br />
#level up if needed<br />
check_level_up()<br />
<br />
#erase all objects at their old locations, before they move<br />
for object in objects:<br />
object.clear()<br />
<br />
#handle keys and exit game if needed<br />
player_action = handle_keys()<br />
if player_action == 'exit':<br />
save_game()<br />
break<br />
<br />
#let monsters take their turn<br />
if game_state == 'playing' and player_action != 'didnt-take-turn':<br />
for object in objects:<br />
if object.ai:<br />
object.ai.take_turn()<br />
<br />
def main_menu():<br />
img = libtcod.image_load('menu_background.png')<br />
<br />
while not libtcod.console_is_window_closed():<br />
#show the background image, at twice the regular console resolution<br />
libtcod.image_blit_2x(img, 0, 0, 0)<br />
<br />
#show the game's title, and some credits!<br />
libtcod.console_set_default_foreground(0, libtcod.light_yellow)<br />
libtcod.console_print_ex(0, SCREEN_WIDTH/2, SCREEN_HEIGHT/2-4, libtcod.BKGND_NONE, libtcod.CENTER,<br />
'TOMBS OF THE ANCIENT KINGS')<br />
libtcod.console_print_ex(0, SCREEN_WIDTH/2, SCREEN_HEIGHT-2, libtcod.BKGND_NONE, libtcod.CENTER, 'By Jotaf')<br />
<br />
#show options and wait for the player's choice<br />
choice = menu('', ['Play a new game', 'Continue last game', 'Quit'], 24)<br />
<br />
if choice == 0: #new game<br />
new_game()<br />
play_game()<br />
if choice == 1: #load last game<br />
try:<br />
load_game()<br />
except:<br />
msgbox('\n No saved game to load.\n', 24)<br />
continue<br />
play_game()<br />
elif choice == 2: #quit<br />
break<br />
<br />
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)<br />
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)<br />
libtcod.sys_set_fps(LIMIT_FPS)<br />
con = libtcod.console_new(MAP_WIDTH, MAP_HEIGHT)<br />
panel = libtcod.console_new(SCREEN_WIDTH, PANEL_HEIGHT)<br />
<br />
main_menu()<br />
<br />
</syntaxhighlight></div><br />
<br />
[[Category:Developing]]</div>Bix