Skip to main content
Engineering LibreTexts

9.4: The Usual Setup Code

  • Page ID
    13614
    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    # Squirrel Eat Squirrel (a 2D Katamari Damacy clone)
    # By Al Sweigart al@inventwithpython.com
    # http://inventwithpython.com/pygame
    # Released under a "Simplified BSD" license
    
    import random, sys, time, math, pygame
    from pygame.locals import *
    
    FPS = 30 # frames per second to update the screen
    WINWIDTH = 640 # width of the program's window, in pixels
    WINHEIGHT = 480 # height in pixels
    HALF_WINWIDTH = int(WINWIDTH / 2)
    HALF_WINHEIGHT = int(WINHEIGHT / 2)
    
    GRASSCOLOR = (24, 255, 0)
    WHITE = (255, 255, 255)
    RED = (255, 0, 0)
    
    CAMERASLACK = 90     # how far from the center the squirrel moves before moving the camera
    MOVERATE = 9         # how fast the player moves
    BOUNCERATE = 6       # how fast the player bounces (large is slower)
    BOUNCEHEIGHT = 30    # how high the player bounces
    STARTSIZE = 25       # how big the player starts off
    WINSIZE = 300        # how big the player needs to be to win
    INVULNTIME = 2       # how long the player is invulnerable after being hit in seconds
    GAMEOVERTIME = 4     # how long the "game over" text stays on the screen in seconds
    MAXHEALTH = 3        # how much health the player starts with
    
    NUMGRASS = 80        # number of grass objects in the active area
    NUMSQUIRRELS = 30    # number of squirrels in the active area
    SQUIRRELMINSPEED = 3 # slowest squirrel speed
    SQUIRRELMAXSPEED = 7 # fastest squirrel speed
    DIRCHANGEFREQ = 2    # % chance of direction change per frame
    LEFT = 'left'
    RIGHT = 'right'
    

    The start of the program assigns several constant variables. This program frequently makes use of the half length of the width and height of the window so much that the HALF_WINWIDTH and HALF_WINHEIGHT variables store these numbers.

    The "camera slack" is described later. Basically, it means that the camera will begin following the player squirrel when it moves 90 pixels away from the center of the window.

    The comments next to these constants explains what the constant variable is used for.


    This page titled 9.4: The Usual Setup Code is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Al Sweigart via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.