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.21.tar.gz (15.6 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.21-py3-none-any.whl (29.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py2048base-0.21.tar.gz
  • Upload date:
  • Size: 15.6 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.21.tar.gz
Algorithm Hash digest
SHA256 8c66331e63f2ca5c82b12973d3a9f28682672116a1c5f08dfe2d5b55225eabfd
MD5 deeb4ac61f6de1516a5d3fb0adefaa87
BLAKE2b-256 448290acdd972e2905a92ceb68cfd4666fa1a887b07f32c3c34d83d3d0efe093

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py2048base-0.21-py3-none-any.whl
  • Upload date:
  • Size: 29.3 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.21-py3-none-any.whl
Algorithm Hash digest
SHA256 fc7e4fa0691a20a103957bbdfc008160be63878358632a03edc8958c3cef55f1
MD5 bc8f59d9d64bdeee9539268ae3f7d8e5
BLAKE2b-256 3c502366d51e483c10b03324f0c35caff62daf7ac4b169e72e2bd39aaca523a7

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