Tag Archives: cpsc 386
Python || Aku Aku Snake Game Using Pygame

The following is another homework assignment which was presented in an Introduction to Game Design and Production class. This project is an implementation of the classic “Snake” game using Python 3.2 and Pygame 1.9.2a0.
REQUIRED KNOWLEDGE FOR THIS PROGRAM
Pygame - How To Install
Pygame - Download Here
How To Use Pygame
How To Create Executable Python Programs
Aku Aku Snake! Source Code - Click Here To Download
Aku Aku Snake! Executable File - Click Here To Download
==== 1. DESCRIPTION ====
Aku Aku Snake is a new take on the classic “snake” game. It features ten levels, with characters and sounds taken from the PlayStation hit: “Crash Bandicoot 2: Cortex Strikes Back.” The purpose of this game is to successfully complete all ten levels in the fewest amount of retries possible.
To advance in the game, each round the player must consume a specified number of fruit items. Once the required score is obtained during each round, a new one is started. This process is repeated until the player finishes all ten levels. But watch out! The game gets harder as the game progresses.
==== 2. USAGE ====
This game utilizes the python “pygame” module. To play this game, it is assumed that the user already has python 3 and pygame installed on their computer. If that is not the case, here are documents explaining how to obtain the necessary resources for play.
How to install Python:
How to install Pygame:
After the required resources are obtained, to start the game, the easiest way this to do would be to extract the entire .zip file into the directory of your choice, and to simply run the “main.py” source file through the python interpreter. Once the “main.py” source file is ran through the python interpreter, the game should automatically start.
NOTE: Python and Pygame are not needed to play the executable file!
==== 3. EXTERNAL DEPENDENCIES ====
Other than the pygame module and the “main.py” file mentioned above, there are eight other source files this program depends on. The additional source files should be saved within the “libs” folder, which is located in the directory: “data -> libs”
The following is a brief description of each additional source file.
• “gameBoard.py” sets up the game board and background images for display. This class also reads files (images/fonts) from the “img” & “fnt” directory for use within this program.
• “gameMusic.py” sets up the game sounds and music. This class also reads files (.ogg) from the “snd” directory for use within this program.
• “gameSave.py” sets up the game to have the ability to save its current status. This class uses the “pickle” module to save the players current progress to a file.
• “snake.py” sets up the snake for display on the game board. This class also reads files (images) from the “img” directory for use within this program.
• “fruit.py” sets up the fruit for display on the game board. This class also reads files (images) from the “img” directory for use within this program.
• “enemy.py” sets up the enemies for display on the game board. This class also reads files (images) from the “img” directory, and uses the “spriteStripAnim” class for use within this program.
• “spriteSheet.py” handles sprite sheet animations.
• “spriteStripAnim.py” extends the spriteSheet.py module, providing an iterator (iter() and next() methods), and a __add__() method for joining strips, which comes in handy when a strip wraps to the next row.
These additional files are located within the directory: “data -> libs” folder.
==== 4. FEATURES ====
This game features various colorful images and background music which are taken from the original Crash Bandicoot game. Each level in this game features background music that are unique to each level.
The most useful feature implemented in this game is the “save game” element, which allows a player to save their current progress at any moment during gameplay. This allows a player to save their current status and continue playing the game at a later date if they desire.
To save the game, during gameplay simply click on the boss image in the upper right corner. Once the save is complete, a sound jingle will play confirming the successful save.
This game also features two modes of operation: “Easy” and “Hard” mode. When easy mode is selected, the game saves the user’s current game progress every time they die, but when hard mode is selected, that feature is turned off and the user HAS to save their current progress manually, or else it will be lost after each death.
Here are screenshots of the game during play. (on all images, click to enlarge)
==== 5. DOWNLOAD AND PLAY ====
• Aku Aku Snake! Source Code - Click Here To Download
• Aku Aku Snake! Executable File - Click Here To Download
NOTE: Python and Pygame are not needed to play the executable file!
Python || Brainy Memory Game Using Pygame

The following is another homework assignment which was presented in an Introduction to Game Design and Production class. This project is an implementation of the classic “Memory” game using Python 3.2 and Pygame 1.9.2a0.
REQUIRED KNOWLEDGE FOR THIS PROGRAM
Pygame - How To Install
Pygame - Download Here
How To Use Pygame
How To Create Executable Python Programs
Brainy Memory Game Source Code - Click Here To Download
Brainy Memory Game Executable File - Click Here To Download
==== 1. DESCRIPTION ====
Brainy Memory Game is a mind stimulating educational card game in which an assortment of cards are laid face down on a surface, and the player tries to find and uncover two identical pairs. If the two selected cards match, they are removed from gameplay. If the two selected cards do not match, both of the cards are turned back over, and the process repeats until all identical matching cards have been uncovered! The object of this game is to find identical pairs of two matching cards in the fewest number of turns possible. This game can be played alone or with multiple players, and is especially challenging for children and adults alike.
==== 2. USAGE ====
This game utilizes the python “pygame” module. To play this game, it is assumed that the user already has python and pygame installed on their computer. If that is not the case, here are documents explaining how to obtain the necessary resources for play.
How to install Python:
How to install Pygame:
After the required resources are obtained, to start the game, the easiest way to do this would be to extract the entire .zip file into the directory of your choice, and to simply run the “memory.py” source file through the python interpreter. Once the “memory.py” source file is ran through the python interpreter, the game should automatically start.
NOTE: Python and Pygame are not needed to play the executable file!
==== 3. EXTERNAL DEPENDENCIES ====
Other than the pygame module and the memory.py file mentioned above, there are two other source files this program depends on. They are named “gameBoard.py” and “gameMusic.py.”
• “gameBoard.py” sets up the game board and playing cards for display. It also reads files (images/fonts) from the “img” & “fnt “directory for use within this program.
• “gameMusic.py” sets up the game sounds and music. It also reads files (.ogg/.wav) from the “snd” directory for use within this program.
Both of these source files should be located in the same directory as “memory.py”
==== 4. FEATURES ====
This game features various colorful images and sounds for a pleasant user experience. By default, there are 27 playing cards (image size: 80×80) for use located in the “img” folder, and more can be added by the player in the future if they desire.
There are four attainable memory ranks available in this game, with two of them being displayed below. Play the full game to discover the rest!
Here are screenshots of the game during play. (on all images, click to enlarge)
==== 5. DOWNLOAD AND PLAY ====
• Brainy Memory Game Source Code - Click Here To Download
• Brainy Memory Game Executable File - Click Here To Download
NOTE: Python and Pygame are not needed to play the executable file!
Python || Random Number Guessing Game Using Random MyRNG & While Loop

Here is another homework assignment which was presented in introduction class. The following is a simple guessing game using commandline arguments, which demonstrates the use of generating random numbers.
REQUIRED KNOWLEDGE FOR THIS PROGRAM
How To Get User Input
Getting Commandline Arguments
While Loops
MyRNG.py - Random Number Class
==== 1. DESCRIPTION ====
The following program is a simple guessing game which demonstrates how to generate random numbers using python. This program will seed the random number generator (located in the file MyRNG.py), select a number at random, and then ask the user for a guess. Using a while loop, the user will keep attempting to guess the selected random number until the correct guess is obtained, afterwhich the user will have the option of continuing play or exiting.
==== 2. USAGE ====
The user enters various options into the program via the commandline. An example of how the commandline can be used is given below.
python3 guess.py [-h] [-v] -s [seed] -m 2 -M 353
Where the brackets are meant to represent features which are optional, meaning the user does not have to specify them at run time.
The -m and -M options are mandatory.
• -M is best picked as a large prime integer
• -m is best picked as an integer in the range of 2,3,..,M-1
NOTE: The use of commandline arguments is not mandatory. If any of the mandatory options are not selected, the program uses its own logic to generate random numbers.
==== 3. FEATURES ====
The following lists and explains the command line argument options.
• -s (seed): Seed takes an integer as a parameter and is used to seed the random number generator. When omitted, the program uses its own logic to seed the generator
• -v (verbose): Turn on debugging messages.
• -h (help): Print out a help message which tells the user how to run the program and a brief description of the program.
• -m (minimum): Set the minimum of the range of numbers the program will select its numbers from.
• -M (maximum): Set the maximum of the range of numbers the program will select its numbers from.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# ============================================================================= # Name: K Perkins # Date: Aug 6, 2013 # Taken From: http://programmingnotes.org/ # File: guess.py # Description: This is the guess.py module which uses a random number # generator to simulate a simple guessing game. This program will seed the # random number generator (located in the file MyRNG.py), select a number at # random, and then ask the user for a guess. The user will keep attempting # to guess the selected random number until the correct guess is obtained, # afterwhich the user will have the option of continuing play or exiting # ============================================================================= # Sample commandline: guess.py -s 3454 -m 1 -M 1000 import sys, pdb from MyRNG import * def Usage(status, msg = ""): # Function prints out usage directions aswell as a simple # message if a string is sent as a 2nd parameter if(msg): print(msg) print("nUSAGE:n ", sys.argv[0],"[-h help] [-v] [-s seed] [-m minimum number]", "[-M maximum number]") sys.exit(status) def IsWarmer(userGuess, numGuess, randomNumber): # Function determines if the current number the user guesses is closer # to the random number than the previous guess. # Function returns TRUE if current user guess is 'warmer' # than previous, otherwise returns FALSE currGuessDifference = userGuess[numGuess] - randomNumber currGuessDifference = math.fabs(currGuessDifference) prevGuessDifference = userGuess[numGuess-1] - randomNumber prevGuessDifference = math.fabs(prevGuessDifference) if(currGuessDifference < prevGuessDifference): return True else: return False def main(): # This is the 'main' function which first takes a string via the commandline # and parses it to determine various modes of operation, namely being # 'seed,' 'minimum,' and 'maximum.' After the commandline options # are obtained, that information is sent to the MyRNG class in order # to generate a random number. After a random number is found, using # a while loop, the user is prompted to try and guess that specified # number, repeatedly doing so until a correct guess is found # declare variables index = 0 # counter which is used to index the sys.argv string verbose = False choices = (("y"),("yes"),("n"),("no")) # This is to be used for the while loop seed = 806189064 # saves the seed from the command line minimum = 1 # saves the minimum num from the command line maximum = 1000 # saves the maximum num from the command line loop = True # this controls the while loop randomNumber = 0 # this saves the random number from the generator userGuess = [] # this saves the user guesses numGuess = 0 # this is the counter wgich keeps track of the num of usr guesses newGame = True # bool to determine if the user is playing a new game programDescr = """nDESCRIPTION: The following is a simple guessing game in which the user is prompted to guess a number and the computer determines if the guess is above, below or exactly the random number which was selected.""" # == determine if the user entered enough args via commandline == # if(len(sys.argv) < 1): #if not enough args, stop the program and print an error message Usage(1, "n** Must provide atleast 1 argument") # == parse thru the argv string to find the appropriate tokens == # for currentArg in sys.argv: if(currentArg == "-h"): # print out the usage message and exit. Usage(2, programDescr) elif(currentArg == "-v"): # set verbose mode to be true for debugging messages verbose = True elif(currentArg == "-s"): # set the seed here # if the user doesnt specify a seed, the # default is my CWID if((index+1 < len(sys.argv)) and (sys.argv[index+1].isdigit())): seed = int(sys.argv[index+1]) else: seed = 806189064 elif currentArg == "-m": # set the minimum here # if the user doesnt specify a min, the default is 1 if((index+1 < len(sys.argv)) and (sys.argv[index+1].isdigit())): minimum = int(sys.argv[index+1]) else: minimum = 1 elif currentArg == "-M": # set the maximum here # if the user doesnt specify a max, the default is 1000 if((index+1 < len(sys.argv)) and (sys.argv[index+1].isdigit())): maximum = int(sys.argv[index+1]) else: maximum = 1000 # increment the index counter index += 1 # print debugging messages if debug mode is on if(verbose): print ("nThis message only appears if verbose mode is turned on.") print (""" ** To continue seeing debugging messages, press the "n" button when prompted. To print the current value of variables, press the "p" button followed by the name of the variable you wish to print. Press the "l" button to visually see where you are in the programs souce code. To quit debugging, press the "c" button.n""") pdb.set_trace() print("nSeed = %d, Minimum = %d, Maximum = %d" %(seed, minimum, maximum)) # == declare the class object == # random = MyRNG(minimum, maximum) random.Seed(seed) randomNumber = random.Next() print("nI'm thinking of a number between %d and %d" ". Go ahead and make your first guess. " %(minimum, maximum)) # * this is the while loop which simulates a guessing game. # * the loop gets a number from the user and determines if the # user input is a "winner, warmer, or colder" in relation to the # random number which was generated. # * once the user guesses correctly, they have a choice of continuing # play or exiting the game. If they choose to play again, a new # random number is generated while(loop): userGuess.append(int(input(">> "))) if(newGame): newGame = False if((userGuess[numGuess] > randomNumber) or (userGuess[numGuess] < randomNumber)): print("nSorry that was not correct, please try again...n") elif((userGuess[numGuess] > randomNumber) or (userGuess[numGuess] < randomNumber)): if(IsWarmer(userGuess, numGuess, randomNumber)): print("nWARMERn") else: print("nCOLDERn") if(userGuess[numGuess] == randomNumber): print("nWINNER! You have guessed correctly!") print("It took you %d attempt(s) to find the answer!" %(numGuess+1)) del userGuess[:] numGuess = -1 answer = input("nWould you like to play again? (Yes or No): ") answer = answer.lower() if(answer in choices[:2]): randomNumber = random.Next() print("nMake a guess between %d and %dn" %(minimum, maximum)) newGame = True elif((answer in choices[2:]) or (answer not in choices[2:])): loop = False numGuess += 1 print("nThanks for playing!!") if __name__ == "__main__": main() # http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output:
Seed = 806189064, Minimum = 1, Maximum = 1000
I'm thinking of a number between 1 and 1000. Go ahead and make your first guess.
>> 500Sorry that was not correct, please try again...
>> 400
WARMER
>> 600
COLDER
>> 300
WARMER
>> 150
WARMER
>> 100
COLDER
>> 180
COLDER
>> 190
COLDER
>> 130
WARMER
>> 128
WINNER! You have guessed correctly!
It took you 10 attempt(s) to find the answer!Would you like to play again? (Yes or No): y
------------------------------------------------------------
Make a guess between 1 and 1000
>> 500
Sorry that was not correct, please try again...
>> 600
COLDER
>> 400
WARMER
>> 300
WARMER
>> 280
WARMER
>> 260
WARMER
>> 250
COLDER
>> 256
WINNER! You have guessed correctly!
It took you 8 attempt(s) to find the answer!Would you like to play again? (Yes or No): n
Thanks for playing!!