Build a 2-player maze game with Python Part 6

by Dan Duda

Requirements

  • Python 3.7
  • PyCharm Community (optional)
  • Basic Python knowledge

Goto Part 5

Introduction

In part 5 we finished the game, making it playable by two players on one keyboard with score keeping. However we’re really not done. It’s always good to have another person test your app. A few things came up when I had another family member try the game on their computer.

  1. It became more apparent that one player has it much easier when generating the maze from the corner.
  2. It did not run as fast on another computer. In fact it was quite sluggish.
  3. The other user had a lot of frustration with the controls trying to move into a pathway that is in the middle of a corridor.

Let’s tackle these in order.

Read More

Build a 2-player maze game with Python Part 5

by Dan Duda

Requirements

  • Python 3.7
  • PyCharm Community (optional)
  • Basic Python knowledge

Goto Part 4

Introduction

In part 4 of this tutorial series we were able to draw our maze centered on our display. Now it’s time to start making it into a playable game.

Player sprites

PyGame includes a Sprite object that we can use for our players. Sprites represent a discrete image on the screen that can move around, etc. Let’s create a new class that uses the Sprite object.

Read More

Build a 2-player maze game with Python Part 4

by Dan Duda

Requirements

  • Python 3.7
  • PyCharm Community (optional)
  • Basic Python knowledge

Goto Part 3

Introduction

In part 3 of this tutorial series we utilized the Recursive Back-Track algorithm to generate a random maze. Now we will look at actually drawing our maze to the screen.

Using PyGame to draw rectangles

Let’s first add some color constants to our constants. BACK_COLOR will be the background color for the entire window. We’ll have a WALL_COLOR for the walls and a MAZE_COLOR for the paths. We’ll also have an UNVISITED_COLOR so that when we watch it generate the map we’ll default areas not visited yet to this color.

1
2
3
4
5
# Define the colors we'll need
BACK_COLOR = (100, 100, 100)
WALL_COLOR = (18, 94, 32)
MAZE_COLOR = (255, 255, 255)
UNVISITED_COLOR = (0, 0, 0)
Read More

Build a 2-player maze game with Python Part 3

by Dan Duda

Requirements

  • Python 3.7
  • PyCharm Community (optional)
  • Basic Python knowledge

Goto Part 2

Introduction

In part 2 of this tutorial series we saw how the the Recursive Back-Tracker algorithm works and started to look out how to store our maze in code. We had decided to use the simple integer to store each cell of our maze and were left with the question of how to signify multiple properties of the cell with a single number. Recall the only properties we need to store for a cell are whether it has been visited and what are its exit paths. If each cell was a collection of of 5 boolean properties we could specify each cell with:

Visited = True or False

North Exit = True or False

East Exit = True or False

South Exit = True or False

West Exit = True or False

These 5 fields would tell us everything we need to know about a cell.

Read More

Build a 2-player maze game with Python Part 2

by Dan Duda

Requirements

  • Python 3.7
  • PyCharm Community (optional)
  • Basic Python knowledge

Goto Part 1

Introduction

In part 1 of this tutorial series we setup our project and created a barebones PyGame script. In this tutorial I want to refactor our script into a class so that going forward we’ll have better separation of concerns. I also want to start setting things like screen resolution in a module level variable at the top of the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import pygame as pg


# Global Settings
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600


class MazeGenerator:
def __init__(self):
# Need to initialize pygame before using it
pg.init()

# Create a display surface to draw our game on
self.screen = pg.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

# Set the title on the window
pg.display.set_caption('PyMaze')

def generate_maze(self):
pass

def run_game(self):
# Main game loop
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False

pg.quit()


mg = MazeGenerator()
mg.run_game()
Read More

Build a 2-player maze game with Python Part 1

by Dan Duda

Requirements

  • Python 3.7
  • PyCharm Community (optional)
  • Basic Python knowledge

Introduction

I wanted to come up with a project I could work on with my son that would teach a lot of programming fundamentals and also be fun. The idea of a maze hit me one day. I did some searching and came across a cool little algorithm called the Recursive Back-Tracker Algorithm. I’ve used PyGame before and decided to give it a go. PyGame is a light-weight module available for Python that lets you create 2D and 3D games. I like that you can do a lot with very little code and that it doesn’t get in the way of teaching programming concpets. In this blog tutorial series we will look at maze generation using the Recursive Back-Tracker Algorithm as well as concepts such as stacks, bit-wise logic, enumerations, and classes.

The finished game will look like this:


Part 1 will focus on getting our environment setup and running a simple PyGame script. I’ll assume you have a basic understanding of Python including lists, tuples, and dictionaries.

Configuring our development environment

I’ll be using the free PyCharm IDE to write my code. You can download it here. Any code or text editor should work though. It is assumed you have python installed on your machine. I will be using Python 3.7 virtual environment although it should work with any 3.x version of Python. The PyGame package will also need to be installed. You can use pip install to install the PyGame packages or use PyCharm as I will show below.

Read More

Generating a PDF Calendar

by Dan Duda

Requirements

  • HTML and CSS Experience
  • C# Experience
  • Visual Studio 2017 Community

Introduction

In the age of smart phones and online calendars I find I still like to also have a paper calendar at my desk. Having a single page per month gives a lot of space to jot down notes and quickly glance at upcoming appointments, etc. Over the years I have come across downloadable year calendars in PDF format that you can print out and staple together. But I wanted more control to add my own recurring “special” dates and holidays. I also wanted to be able to include colorful icons with my events.

My first concern was how to render the calendar. I wanted to keep it simple so I decided to use just plain HTML and tables with CSS to style them. This will also enable the use of the web browser for printing and PDF conversion without having to write anything myself. There were some things I learned along the way that I hope may help someone else. One in particular was the use of media queries to override the browsers default print themes. Another was discovering the FontAwesome library for icons.

Project Setup

For this project I decided to create a console application in C# and the .Net framework. I’ll be using Visual Studio 2017 Comminity Edition. The first step is to create a “C# console application”. I’m using .net framework version 4.7.


By default Visual Studio gives us a single class file for console applications called Program.cs with a single method called Main. I didn’t feel I really needed a GUI for this and wanted to keep it simple so chose the console application approach which means it will run on the command line. The program should take a single command line paramter for the year to generate. We’ll check that the input is a valid number and then also check that it’s a pratical year. Yes, I’m pretty optimistic putting in 2400 as an end year. :)

Read More

Build a recursive word finding algorithm with Python - Part 4

by Dan Duda

Requirements

  • Python 3.x
  • PyCharm Community (optional)
  • A text file of English words
  • Basic Python knowledge

Goto Part 3

Introduction

Is there a way we could store the list of words so that it’s more efficient to check if a string of letters is a valid word? The answer of course is yes. One way that I’m particularly fond of is to create a tree made up of nodes representing letters. The tree would start with all 26 letters and the child nodes of each letter node would form a path to valid words. Let’s look at a diagram to help explain it.


Here is a partial sample of what our tree might look like. From this we can have many optimizations. For example, say we’re checking the word part, ‘cb’. If we traverse our sample tree we first go to the ‘c’ node. We then check if the ‘c’ node has a child node of ‘b’. It does not so we can stop processing right there. If we had a bunch of remaining letters our current algorithm would keep on adding every combination of the letters onto ‘cb’ and checking against our word list. If we know ‘cb’ is not a valid path, then we know there can be no words starting with ‘cb’ so we can quit processing early.

Now since multiple words can start with the same combination of letters like “cat” and “cater” we need to know when a path is a valid word. To do this we can just add a flag to our nodes indicating if it’s a valid word. So, the first ‘c’ node under the root node would be set to false because there are words that start with ‘c’ but would not be a valid word itself. Same with the ‘a’ node under the ‘c’ node’. But when we get to the ‘b’ node we’d signify that it’s a valid word. The same with the ‘T’ node which would give us “cat”.

Read More

Build a recursive word finding algorithm with Python - Part 3

by Dan Duda

Requirements

  • Python 3.x
  • PyCharm Community (optional)
  • A text file of English words
  • Basic Python knowledge

Goto Part 2

Introduction

In the last tutorial we created a program that can find all the words that can be made from a string of letters. We also noticed that our program wasn’t very fast or efficient. Let’s fix that.

Before we try and optimize our code we should deternmine if that’s even needed. Our program did run very slow when using more than a few letters but to be sure we should add some timers to our code. We’ll also restructure our code into classes so that it’s easier to test.

Adding a class

Let’s add a new file to our project folder called “word_finder_simple.py”. We’ll pull our algorithm out of our main words.py and into this new file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class WordFinderSimple:
def __init__(self, word_file_path):
f = open(word_file_path, "r")
self.words = [l.rstrip() for l in f.readlines()]
f.close()

def find_words(self, letters):
found = []
self.search_words('', letters, found)
found.sort()
return found

def search_words(self, word_part, remaining_letters, found_words):
if len(word_part) >= 3 and word_part not in found_words and word_part in self.words:
found_words.append(word_part)

for i in range(len(remaining_letters)):
remaining_letters_copy = remaining_letters.copy()
next_letter = remaining_letters_copy[i]
del remaining_letters_copy[i]
self.search_words(word_part + next_letter, remaining_letters_copy, found_words)
Read More

Build a recursive word finding algorithm with Python - Part 2

by Dan Duda

Requirements

  • Python 3.x
  • PyCharm Community (optional)
  • A text file of English words
  • Basic Python knowledge

Goto Part 1

Introduction

In the last tutorial we setup our project to read in a list of words from a text file and then prompt the user for a word. We then print if the word was found in the list or not. In this tutorial we’ll create an algorithm to check all the combinations of letters that we enter to see what words, if any, we can make from them.

Recursive functions are functions that call themselves. In theory any recursive algorithm can be written non-recursively using just loops but depending on the scenario it can be much more complex to write. The downside of recursive functions is that they can take up a lot more memory depending on the scenario. Each recursive call to the function means that execution must leave the current function call by adding another call onto the stack in memory. Depending on how deep the recursion goes you could end up with a stack overflow error. A recursive function will have a base case and a recursive case. The base case causes the recursive function to end (stop calling itself) whereas the recursive case will call itself again. We can visualize recursion using a simple example of calculating factorials. A factorial is the product of all positive integers less than or equal to n, where n is a non-negative integer, and is denoted by n!. So for example:

1
5! = 5 X 4 X 3 X 2 X 1 = 120
Read More