# HAC Tutorial #02: Player movements

# Introduction

The second tutorial of our series is about learning to make the player move (for the kids from our Coding Class, it's the lesson entitled "I like to move it, move it").
We will also dig more into game management to create a nice way of quitting the game, create a main event loop, correctly display the game, etc.

# hac-game-lib

To follow this tutorial you will need the hac-game-lib on your system.
You can either get it on Github or (this is the preferred solution) install it from PyPi by running:

pip3 install virtualenv --user
virtualenv hgl-tutorial-2
cd hgl-tutorial-2
source bin/activate
pip3 install -i https://test.pypi.org/simple/ hac-game-lib

# Video

This second tutorial is also done under the form of a code along video available on Youtube (still a bad one but bear with us, it'll become better over time).

# Code

Here is the final code of this tutorial.


In the video there is an error in the code that display the board. The clear_screen() and display_board() are done before moving the player.
This results in a one turn delay of the movement.

from gamelib.Game import Game
from gamelib.Board import Board
import gamelib.Utils as Utils
import gamelib.Sprites as Sprites
import gamelib.Constants as Constants
from gamelib.Characters import Player
import time

mygame = Game(name='Demo game')
board1 = Board(name='Level 1', ui_borders=Sprites.WALL, ui_board_void_cell=Utils.BLACK_SQUARE, player_starting_position=[0,0])
board2 = Board(name='Level 2', ui_borders=Utils.RED_SQUARE, ui_board_void_cell=Utils.BLACK_SQUARE, player_starting_position=[4,4])

mygame.player = Player(name='DaPlay3r', model=Sprites.UNICORN_FACE)



key = None
# Main game loop
while True:
    if key == 'q':
        print( Utils.yellow_bright("Good bye and thank you for playing!") )
    elif key == 'w':
    elif key == 's':
    elif key == 'a':
    elif key == 'd':
    elif key == '3':
    key = Utils.get_key()

# Share

If you like that content, feel free to share it on social platforms:

# Comments

Last Updated: 5/18/2019, 6:46:55 AM