Today I worked on a Python version of the classic 2048 game using Pygame. The whole thing runs in an 800×800 window, split into a 4×4 grid. Tiles slide around, merge when they match, and new tiles spawn after every move just like the real game.
class Tile:
^This class handles everything about the individual tiles. Each tile knows its value, its position on the grid, and its pixel coordinates. It also picks a color based on its value and draws itself with centered text. The tile can move smoothly across the board and update its row/column once it lands.
def draw_grid(window):
^This draws the thick grid lines that separate the 16 squares. It’s just visual structure no gameplay logic here.
def draw(window, tiles):
^This function clears the screen, draws every tile, draws the grid, and updates the display. It’s the main “render everything” function that gets called every frame.
def get_random_pos(tiles):
^This picks a random empty spot on the board. It keeps generating row/column pairs until it finds one that isn’t already occupied by a tile.

Leave a Reply