Skip to content

lmu-cmsi1010-fall2021/python-game-demos

Repository files navigation

CMSI 1010 Computer Programming & Laboratory, Fall 2021

Python Game Demos

This repository contains a collection of code and resources to play with for creating games with Python. The originals for these files are taken from various public locations across the web, and are collated here in one place for your convenience.

Overview

The files in this repository represent different ways for implementing games in Python. They vary based on the Python library that they use:

Base library Examples
Tkinter tkinter_demo.py, all_user_input_widgets.py
graphics.py tetris_sol.py
Pillow whack_a_mole.py
PyGame chimp, test_pygame.py

Tkinter serves as a foundation for any Python program that has graphics—you have already been using this via the graphics.py module that has come with many of your psets! The included Tkinter-only demos aren’t really games; they show you how to open a window and how to code up basic widgets like buttons, lists, text fields, etc.

The trusty graphics.py module has come with many of your psets so far. As you might recall, this module adds a number of basic computer graphics operations. If you put these together in the right way, you can implement games! However, graphics.py does not include support for music or other sound 😔

Another area where Tkinter can use a boost is support for image files. The Pillow library helps here, making it easier to implement game graphics based on image files that you can create separately.

Finally, the most broad-based basic game support can be found by installing the PyGame Python library. Learning this library can give you a path toward creating Python games with the least possible boilerplate code, with support for images, music, and sound built-in.

These four are all independent alternatives to creating game-like programs in Python. If you prefer to stay with something you have used before and don’t mind not having music or sound, staying with graphics.py will suffice. If you want to learn just enough additional code to handle images, you can use Pillow. However if you want all of the above plus music/sound, and are willing to learn some new library code, PyGame would be the way to go.

Set Yourself Up for Experimentation

If you want to modify and push your modifications to GitHub, you will need to fork this repository. This will create your own personal copy which you can then clone as usual. Click on Fork button near the top-right of https://github.com/lmu-cmsi1010-fall2021/python-game-demos in order to create a forked copy for yourself.

Tkinter Demos

If you have been able to run the graphics.py exercises from the problem sets, then you will be able to run the Tkinter demos without further setup:

python3 tkinter_demo.py
python3 all_user_input_widgets.py

As mentioned in the Overview, these are not actually games but they show the basic graphics and user interface features that Tkinter provides.

graphics.py Demo: tetris_sol.py

Silent games are possible with just graphics.py. For convenience, this file is included with this repository just as it has been with past problem sets. To demonstrate this, we have a simple version of the Tetris game:

python3 tetris_sol.py

Full documentation on graphics.py can be found here: https://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf

As long as graphics.py is in the same folder as tetris_sol.py, the game should work. Use the ⬅️/➡️ keys to move a falling piece; the ⬆️ key rotates it; the space bar drops it.

Note, this is for demo purposes only: you may encounter bugs! (which may be just as educational as the working code itself 😁)

Pillow Demo: whack_a_mole.py

This is an implementation of Whack-a-Mole, using images to represent hidden and uncovered moles via the Pillow library. This makes it easy to swap out the graphics without having to change the code!

Required Library for Whack-a-Mole Demo: Pillow

Pillow is not included with most standard installations of Python. As such, it needs to be installed separately. There are many ways to install a Python library so if you know of others and prefer those, feel free to use them. If you are new to installing third-party libraries, pip with the user option is a safe way to do so: the user option ensures that the library is saved as part of your user account’s files, avoiding administrator passwords and possible interference with operating system files.

Windows

py –m pip install –U Pillow -user

macOS

python3 –m pip install –U Pillow --user

If the exact command doesn’t appear to work, try to replace py or python3 with the Python command you’ve been using.

Hammer Time!

With the Pillow library installed, the game should be fairly self-explanatory once you run it:

python3 whack_a_mole.py

More information on this implementation (with different image files) can be found here: https://runestone.academy/runestone/books/published/thinkcspy/GUIandEventDrivenProgramming/11_gui_program_example.html

PyGame Demos: chimp and test_pygame.py

For full game support including audio/music, PyGame is a viable option. Some self-teaching is involved since it’s optional for the class, but the time investment may be well worth it if games are your jam.

Required Library: PyGame

PyGame is not included with most standard installations of Python. As such, it needs to be installed separately. There are many ways to install a Python library so if you know of others and prefer those, feel free to use them. If you are new to installing third-party libraries, pip with the user option is a safe way to do so: the user option ensures that the library is saved as part of your user account’s files, avoiding administrator passwords and possible interference with operating system files.

Windows

py -m pip install -U pygame -user

macOS

python3 -m pip install -U pygame --user

If the exact command doesn’t appear to work, try to replace py or python3 with the Python command you’ve been using.

Quick Start: chimp

If you want to try something out right away, the PyGame library comes with a built-in example called chimp. In fact, you don’t need any extra code to run it; it’s already in PyGame! Do this:

python3 -m pygame.examples.chimp

(substitute python3 with your Python command if it’s different)

An article on how chimp works can be found here: https://www.pygame.org/docs/tut/ChimpLineByLine.html

The code for chimp can be seen here: https://www.pygame.org/docs/tut/chimp.py.html

Code Included: test_pygame.py

The included PyGame test code implements a racecar-like game (so it’s just “A bit Racey”) with music and sound effects! How many barriers can you dodge before crashing?

python3 test_pygame.py

A tutorial, with videos, that walks you through this program can be found here: https://pythonprogramming.net/pygame-python-3-part-1-intro

(this is a multipage tutorial so don’t hesitate to click to the next one at the bottom if you want to keep learning more)

Note how these libraries all make use of classes and objects, which is why we couldn’t really introduce these libraries until we got to this point. Make sure to review classes, objects, methods, and inheritance if you’re having trouble with any of these libraries.

About

A collection of code and resources to play with while exploring PyGame.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages