Skip to main content

Python clone of the famous 2048 game.

Project description

py2048 is a clone of the famous game 2048, by Gabriele Cirulli. It’s probably not very interesting to people looking for playing 2048, but may be useful for people looking for learning Python (and some popular Python packages, such as PyQt). Written in Python 3.8 (and nothing more), the package is divided in 2 parts: the backend and the frontend (or interface).

This package provides no concrete user interface for the game, but contains a class that must be inherited by every actual frontend (more details below).

Features

  • Customizable rules: you can configure the size of the game grid and the winning condition (wanna go beyond 2048?);

  • Customizable looks: by subclassing the basic frontend and overriding its methods, you can make the game look any way you want, without touching the basic game logic;

  • Automatic mode: you can replace human input with a function that picks a random direction! More useful for testing than actual gameplay, though.

How to use

Subclass py2048.basefrontend.Base2048Frontend and override the following methods:

  • choose_direction

  • player_quit

  • player_victory

  • player_loss

choose_direction must return exactly one py2048.Directions object. The other methods shouldn’t return anything. player_quit is called when the player exits the game before winning or losing.

Wishlist

In the future, I’d like to create an AI to play the game!

Backend

The backend is a pure-Python implementation of the original game logic; it explains how equal numbers merge into their sum, “moving” from one tile into another, and so on. The main components are the Cell and the Grid.

The Cell class is a wrapper over an int that represents a tile in the game grid. It has a locked property, which is simply a bool that determines whether it can change its stored int. This prevents, for example, a 2 merging into a 2 and the resulting 4 merging into another 4 all in a single game-turn.

The Grid is wrapper over a dict that maps points into Cells (and points are named tuples that store x and y coordinates). This class implements much of the game logic, such as determining how and when one Cell can merge into another.

Frontend

The backend deals only with plain Python data, such as int and dict; it is up to each frontend to actually collect user input and respond by displaying the game data as a (hopefully) pretty interface. Each frontend is a Python class that inherits from Base2048Frontend, the only frontend-related implementation in this package. This base class contains the main game loop that runs until a tile numbered with the winning condition has formed or the player has no valid movements available.

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

py2048base-0.18.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py2048base-0.18-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

Details for the file py2048base-0.18.tar.gz.

File metadata

  • Download URL: py2048base-0.18.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.23.0 setuptools/53.0.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.6

File hashes

Hashes for py2048base-0.18.tar.gz
Algorithm Hash digest
SHA256 cb518266240a115f35978bcc4c01cca96dd640e57a3d93ed694e3a73ac25e966
MD5 4e3f62aba47f454d4123654d5935f032
BLAKE2b-256 985a0941e2500e9c1ae73cb614208a77cc24653b09e04e229569f16c42f079a8

See more details on using hashes here.

File details

Details for the file py2048base-0.18-py3-none-any.whl.

File metadata

  • Download URL: py2048base-0.18-py3-none-any.whl
  • Upload date:
  • Size: 29.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.4.2 requests/2.23.0 setuptools/53.0.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.6

File hashes

Hashes for py2048base-0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 c0e4b9a3544c6f2e2d39b4f3b9b0dcc32ebcbbb51d43ebaf961e534c28d2d4d5
MD5 bd33175770bc19d86644a66fc0af04f5
BLAKE2b-256 9d08d5e404c11b147154aad7161f642e1091cbdb00e453fa1f6ed2485744fd56

See more details on using hashes here.

Supported by

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