This is the final week of the two-part course Introduction to Interactive Programming in Python. This week's lectures introduce a very important data type in Python: sets, which I briefly cover in this blog post.
This week's mini project is building RiceRocks, a modern recreation of the classic arcade game Asteroids. The name RiceRocks is a reference to Rice University, where this course's instructors teach computer science.
- Sets: This week introduces the set data type, which we will use in our implementation of this week's mini project, RiceRocks. A set is an unordered collection without duplicates. In previous weeks we learned about the list data type, which is a mutable sequence of objects. Sets are similar to lists in that they are both collections of objects, but unlike lists, sets contain no duplicate items. It's important to note that most of the operations performed on sets in Python (e.g., union, intersection, etc.) simply apply the mathematical principles of set theory, the branch of mathematics that deals with sets. To create a new set or convert an iteratble into a set in Python, we use the following syntax:
set()
or set(iterable)
. Below are some common set operations:
- Set Union:
a.union(b)
The union of sets a and b is the set of elements which are in set a, set b, or in both sets a and b.
- Set Intersection:
a.intersection(b)
The intersection of sets a and b is the set that only contains all elements of a that also belong to b.
- Set Difference:
a.difference(b)
The difference of sets a and b is the set that includes all elements in a that are not included in b.
- Add Element:
a.add(x)
Adds element x to set a.
- Remove Specific Element::
a.remove(x)
Removes element x from set a.
- Remove Arbitrary Element:
a.pop()
Removes and returns an arbitrary element from set a.
Mini Project 9
RiceRocks is the final mini project of Introduction to Interactive Programming in Python. It's a challenging and extremely fun and rewarding project to build for new programmers because it brings together so many fundamental programming concepts that this course covers, such as global/local variables, functions, classes and object-oriented programming, event-driven programming, sets, lists, and so on. In fact, it does such a great job of tying together the basic building blocks of programming that it was one of the first projects I built in JavaScript when I began learning front-end development. The explanation below of my implementation of RiceRocks in Python isn't a step-by-step tutorial of how to build this game. Rather, it shows how building the game brings together all of the programming concepts from the first eight weeks of the course.
- Global/Local Variables: Variables point to a physical location in a computer's memory that stores a value. Variables that are not defined within a function have a global scope and can be accessed and modified by other objects in the program, such as functions. Variables that are defined within a function have a local scope and can only be accessed and modified within the scope of that particular function. It's best practice to declare global variables that shouldn't be modified in uppercase letters to distinguish them between global variables that may be modified in a program. For example, the
WIDTH
and HEIGHT
global variables should not be modified by any functions, but the score
, lives
, time
, started
, and counter
global variables will be updated to keep track of the state of the game.
- Classes and Object-Oriented Programming: One of the core concepts central to object-oriented programming (OOP) is the abstract notion of encapsulation, which bundles data, properties and methods (i.e., functions) of an object to distinguish that object as its own entity separate from the rest of the program. Classes are like a blueprint for creating objects. They can be re-used to create multiple objects that have a similar data format and properties. An object, then, is simply an instance of a class. My implementation of RiceRocks includes a ship class and a sprite class. Note that the ship class is not the ship itself; rather, it's a blueprint for instantiating a ship. Similarly, the sprite class is simply a blueprint for creating sprite objects, which include the asteroids, missiles, and explosion objects. Shown below is the code snippets for the ship class, as well as the code for creating the ship object.
- Helper Functions: A function is a piece of code that you define and execute later by calling it. In general, I try to write functions that only do one thing, and do that thing perfectly. This makes functions easier to test, debug, and, most importantly, read (Note: unit tests and the importance of testing are introduced in Principles of Computing). In other words, rather than writing one function that performs many computations, I prefer isolating each computation in a separete helper function, and then calling the helper functions in other parts of my program when necessary. The code snippet below shows the helper functions I wrote for RiceRocks. Note that each helper function aims to perform just one task. For example, the simplest helper function below,
dist(p, q)
, just returns the distance between two points, p and q.
- Event-Driven Programming: Most of the mini projects in Introduction to Interactive Programming in Python are interactive, as the name of the course suggests, and RiceRocks is by far the most interactive. Event-driven programs wait for pre-defined events to occur, such as specific inputs from a keyboard or mouse, or a certain amount of elapsed time. Once an event occurs, a special type of function called an event handler is called, which tells the program how to handle that type of event. Event-driven programming allows users to interact with applications such as games. My implementation of RiceRocks includes event handlers for clicking events, keydown and keyup events, and timing events. The code snippet below shows the
click(pos)
event handler that is called when a player clicks their left mouse button on the canvas.
Introduction to Interactive Programming in Python was the first massively-open online course (MOOC) I took, and it was a fantastic experience that made me sincerely appreciate the efforts of the course creators and experience first-hand the incredible benefits of self-directed education in general. I've had excellent teachers and professors throughout high school, college, and graduate school, and I can honestly say that this course and the entire specialization has been one of the best educational experiences of my life. This course provides an excellent introduction to programming, and I highly recommend it as well as the subsequent courses which delve into the principles, mathematics, and algorithmic thinking of computer science.
See my complete implementation of Mini Project 9 on GitHub.