Difference between revisions of "Complete Roguelike Tutorial, using python3+libtcod, setup Mac"

From RogueBasin
Jump to navigation Jump to search
 
(11 intermediate revisions by one other user not shown)
Line 17: Line 17:
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]
* [https://www.sublimetext.com/3 Sublime Text 3 (Editor)]
* [https://macromates.com/ Text Mate (Editor)]
* [https://macromates.com/ Text Mate (Editor)]
* [https://code.visualstudio.com/ VS Code (Editor)]
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]
* [http://blog.atom.io/2014/12/10/a-windows-installer-and-updater.html Atom (Editor)]


== Setting up Bash ==
== Setting up Bash ==
Line 51: Line 50:
<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>
<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>


== Setting up Mercurial ==
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:
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">brew install mercurial</syntaxhighlight></div>
== Installing libtcod ==
At this point, we should be ready to download and install libtcod.  There are several basic steps required to make this work.
* install libtcod dependencies
* download the source code from bitbucket using mercurial
* build the source code with autoconf tools
* move the resulting library files ($HOME/repos/other/libtcod/build/autoconf/.libs/*) into a proper folder (/usr/local/lib)
=== Install libtcod build and runtime dependencies ===
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:
<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>
=== Download libtcod ===
The build process is fairly straight forward, but first we need to download the source code from bitbucket.
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME
mkdir -p $HOME/repos/other/
cd $HOME/repos/other
hg clone https://bitbucket.org/libtcod/libtcod</syntaxhighlight></div>
=== Build libtcod ===
==== Configuring the build tools ====
There are several options for building, but the most straightforward mechanism is to use autotools to build.
Make sure you're at the top of the libtcod repo:
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod</syntaxhighlight></div>
Change to the autotools build folder:
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd build/autotools</syntaxhighlight></div>
Run autotools configuration:
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">autoreconf -i</syntaxhighlight></div>
Run libtcod compilation configuration:
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">./configure CFLAGS='-O2'</syntaxhighlight></div>
Build libtcod:
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">make</syntaxhighlight></div>
==== Installing libtcod ====
To install in the recommended system path:
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">cd $HOME/repos/other/libtcod
mkdir -p /usr/local/lib
cp -rf build/autotools/.libs/*  /usr/local/lib
</syntaxhighlight></div>


== Setting up Python ==
== Setting up Python ==
Line 114: Line 58:
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div>
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">python --version</syntaxhighlight></div>


The version should be at least "Python 3.6.5" (After 15 June 2018, this will be 3.7).
The version should be at least "Python 3.7.0" (See: [https://www.python.org/dev/peps/pep-0569/ Release Schedule]).


=== Troubleshooting ===
=== Troubleshooting ===


==== Environmental Variable: Path ====
==== Environmental Variable: Path ====
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:
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.
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">export PATH=/usr/local/opt/python/libexec/bin</syntaxhighlight></div>
<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>


=== Pip ===
=== Pip ===
Line 132: Line 76:


==== Installing vsh ====
==== Installing vsh ====
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:
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:


Line 138: Line 81:


==== Creating a virtual environment using vsh ====
==== Creating a virtual environment using vsh ====
To create a new virtual environment with python 3 for our project, just use the following:
To create a new virtual environment with python 3 for our project, just use the following:
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div>
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">vsh rl</syntaxhighlight></div>


You should now be in your virtual environment
You should now be in your virtual environment
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.6.5) bash-3.2$</syntaxhighlight></div>
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) bash-3.2$</syntaxhighlight></div>


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
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
<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>
<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>


=== Installing libtcod into our project ===
=== Installing libtcod into our project ===
Navigate to where libtcod was downloaded and then run the installation or libtcod:
Use pip
<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
<div style="padding: 5px; border: solid 1px #C0C0C0; background-color: #F0F0F0"><syntaxhighlight lang="bash">(venv:rl|3.7.0) $ pip install tcod</syntaxhighlight></div>
(venv:rl|3.6.2) $ pip install .</syntaxhighlight></div>


Validate that we have libtcod installed.
Validate that we have libtcod installed.
<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
<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>
(venv:rl|3.6.2) $ python -c "import libtcodpy"</syntaxhighlight></div>


=== Folder structure ===
=== Folder structure ===
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.
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.


Line 176: Line 115:


Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].
Now you're ready to [[Complete Roguelike Tutorial, using python3+libtcod, part 1|start writing code]].
[[Category:Developing]]

Latest revision as of 07:58, 4 December 2018

This is part of a series of tutorials; the main page can be found here.

Setup Mac

Installing an Editor

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.

The following editors are all excellent for python development. You only need one.

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)

Setting up Bash

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. Make sure the following lines show up at the bottom of the file:

# Setup prompt colors
reset_color="\[\e[m\]"
declare -A fg
magenta="\[\e[35m\]"
yellow="\[\e[33m\]"
green="\[\e[32m\]"

function virtualenv_prompt() {
    if [ -n "$VIRTUAL_ENV" ]; then
        pyver=$(python -V 2>&1 | cut -f2 -d' ')
        echo "(${magenta}venv${reset_color}:${yellow}${VIRTUAL_ENV##*/}$reset_color|${green}${pyver}${reset_color}) "
    fi
}

export PS1="$(virtualenv_prompt)${PS1}"
export LIBTCOD_DLL_PATH="/usr/local/lib;/usr/lib;$HOME/.local/lib;$HOME/lib"

Opening a Terminal

While Mac comes with a Terminal built-in, 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).


Setting up Homebrew

Homebrew is a package manager for Mac. Within a terminal, type the following to install homebrew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


Setting up Python

To install the latest python version, simply run within a terminal:

brew install python

To validate that we've installed python correctly, within a terminal, run:

python --version

The version should be at least "Python 3.7.0" (See: Release Schedule).

Troubleshooting

Environmental Variable: Path

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.

export PATH=/usr/local/opt/python/libexec/bin:$PATH

Pip

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:

python -m ensurepip

Setting up your Project

Creating a virtual environment

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.

Installing vsh

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:

python3 -m pip install vsh

Creating a virtual environment using vsh

To create a new virtual environment with python 3 for our project, just use the following:

vsh rl

You should now be in your virtual environment

(venv:rl|3.7.0) bash-3.2$

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

(venv:rl|3.7.0) bash-3.2$ python --version

Installing libtcod into our project

Use pip

(venv:rl|3.7.0) $ pip install tcod

Validate that we have libtcod installed.

(venv:rl|3.7.0) $ python -c "import tcod"

Folder structure

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.

When we're all done, the basic folder structure should look like this:

  • \project\
    \firstrl.py
mkdir -p $HOME/repos/mine/roguelike
touch $HOME/repos/mine/roguelike/firstrl.py

Finishing touches

Congratulations!

You're ready to start editing firstrl.py!

Now you're ready to start writing code.