Skip to main content
Engineering LibreTexts

4.9: Making the Source Code Look Pretty

  • Page ID
    14442
  • # Memory Puzzle
    # By Al Sweigart al@inventwithpython.com
    # http://inventwithpython.com/pygame
    # Released under a "Simplified BSD" license
    
    import random, pygame, sys
    from pygame.locals import *
    
    FPS = 30 # frames per second, the general speed of the program
    WINDOWWIDTH = 640 # size of window's width in pixels
    WINDOWHEIGHT = 480 # size of windows' height in pixels
    REVEALSPEED = 8 # speed boxes' sliding reveals and covers
    BOXSIZE = 40 # size of box height & width in pixels
    GAPSIZE = 10 # size of gap between boxes in pixels
    BOARDWIDTH = 10 # number of columns of icons
    BOARDHEIGHT = 7 # number of rows of icons
    assert (BOARDWIDTH * BOARDHEIGHT) % 2 == 0, 'Board needs to have an even number of boxes for pairs of matches.'
    XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * (BOXSIZE + GAPSIZE))) / 2)
    YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * (BOXSIZE + GAPSIZE))) / 2)
    
    #            R    G    B
    GRAY     = (100, 100, 100)
    NAVYBLUE = ( 60,  60, 100)
    WHITE    = (255, 255, 255)
    RED      = (255,   0,   0)
    GREEN    = (  0, 255,   0)
    BLUE     = (  0,   0, 255)
    YELLOW   = (255, 255,   0)
    ORANGE   = (255, 128,   0)
    PURPLE   = (255,   0, 255)
    CYAN     = (  0, 255, 255)
    
    BGCOLOR = NAVYBLUE
    LIGHTBGCOLOR = GRAY
    BOXCOLOR = WHITE
    HIGHLIGHTCOLOR = BLUE
    

    Remember that colors in Pygame are represented by a tuple of three integers from 0 to 255. These three integers represent the amount of red, green, and blue in the color which is why these tuples are called RGB values. Notice the spacing of the tuples on lines 22 to 31 are such that the R, G, and B integers line up. In Python the indentation (that is, the space at the beginning of the line) is needs to be exact, but the spacing in the rest of the line is not so strict. By spacing the integers in the tuple out, we can clearly see how the RGB values compare to each other. (More info on spacing and indentation is as http://invpy.com/whitespace.)

    It is a nice thing to make your code more readable this way, but don’t bother spending too much time doing it. Code doesn’t have to be pretty to work. At a certain point, you’ll just be spending more time typing spaces than you would have saved by having readable tuple values.

    • Was this article helpful?