Skip to main content

Write your own function to solve the maze!

Project description

MaGame

MaGame is a package currently involving two maze games: Mappy and Pac-man. In the game Mappy, a mouse cop is searching for the criminal cat in the maze, while in the other game Pac-man, the character is trying to eat all dots in the maze.



Once you installed the package with the pip command, you may follow steps below to run the game. Step 1: import the game and mazes from the MaGame package

from magame import mappy    # Import the Mappy game
from magame import pacman   # Import the Pac-man game
from magame import mazes    # Impor the pre-defined mazes
pygame 2.0.0.dev6 (SDL 2.0.10, python 3.7.4)
Hello from the pygame community. https://www.pygame.org/contribute.html

The variable mazes is a tuple, containing four nested lists representing the mazes, where:

  • Maze wall is indicated by "1".
  • Open path is indicated by "0".
  • The target in the Mappy game is indicated by "-1". It is treated as "0" in the Pac-man game.

One example is given below, and you may create your own maze following the same format.

mazes[0]
[[0, 0, 0, 1, 1, 0, 0, 0, 1, 1],
 [0, 0, 1, 0, 1, 0, 0, 0, 0, 1],
 [1, 0, 1, 0, 1, 0, 0, 1, 0, 0],
 [1, 0, 0, 0, 1, 0, 0, 1, 0, 0],
 [1, 1, 0, 1, 1, 1, 0, 1, 1, 0],
 [0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
 [0, 1, 0, 1, 1, 0, 1, 1, 1, 0],
 [0, 1, 0, 1, 0, 0, 1, 0, 0, 0],
 [0, 0, 0, 1, 0, 1, 1, 0, 1, 1],
 [1, 1, 0, 0, 0, 1, 0, 0, -1, 1]]

Step 2: create your own function that determines the movement in the maze

The user-defined function must follow the format below.

def maze_move(maze, position, memory):

    """
    Your code here
    """

    return move, memory

The input arguments of the function are:

  • maze as a nested list representing the maze.
  • position is a tuple with two integers: h_index and v_index. The first integer v_index indicates the horizontal index, and the second integer v_index indicates the vertical index, as shown by the picture below.
  • memory can be any data type. Users may use it to keep a track of all previous steps or previous moves. The initial value of memory is given as None.

The outputs are:

  • move is a tuple with two integers: h_move and v_move. It is used to indicate how the character moves in the maze, as shown by the following picture.
  • memory is the updated memory, such as all previous steps or previous moves of the character in the maze.

The function receives the maze information, the current position in the maze, and previous step, moves, or other forms of memory; then decide the next move and update the memory.

Step 3: apply your function to play the game

The game can be played by simply calling the play() function of the mappy or pacman

mappy.play(maze, maze_move)
pacman.play(maze, maze_move)

There are two input arguments for the play() function:

  • maze is the nested list as the maze information.
  • maze_move is the name of the function you created. It can be any valid Python function name.

If your function works correctly, you will find the character moving in the maze.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

magame-0.0.3.tar.gz (78.6 kB view hashes)

Uploaded Source

Built Distribution

magame-0.0.3-py3-none-any.whl (80.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page