Python is one of the world's most popular programming languages, and with good reason: it is extremely flexible, easy to learn, and runs on a wide variety of devices and platforms. In fact, Python recently moved to first place in the TIOBE programming community index, which is a remarkable achievement.
Python is one of the world's most popular programming languages, and with good reason: it is extremely flexible, easy to learn, and runs on a wide variety of devices and platforms. In fact, Python recently moved to first place in the TIOBE programming community index, which is a remarkable achievement.
This course is an introduction to Python, version 3.10. Unlike many introductory courses, we will not spend hours surveying the basics of the language; personally, I see no point in doing that, since there are already many, many free resources online that do precisely that. Instead, we'll learn the language in the way that I have long preferred: we'll actually build something.
The bulk of this course will be focused on building a simple text adventure game, where the player explores a labyrinth, encounters monsters, engages in combat, finds items, and that sort of thing. It's a bit of a throwback to the early days of computer games, but the important thing here is not the quality of the game that we'll build, but rather that we'll cover all of the things you need to get started programming in Python, including:
Python's primitive data types: integer, float, string, and boolean
Python's aggregate data types: tuple, set, dictionary and list
How to make decisions in Python: if/elif/else
How to loop in Python: for and while loops
How to write functions
How to import from the standard library, and third party packages
How to structure a Python program
How to perform mathematical operations in Python
How to manipulate strings in Python
How to work with classes in Python
And, as the saying goes, much more. Periodically, I'll include "aside" lectures, which focus on a particular aspect of Python in more detail, with simple code examples that are not part of the main project.
This course requires no previous programming experience.
Let's figure out how to capture user input.
Just an introduction, and an overview of what we are going to be covering in this course.
A brief note about my background.
Macs come with Python installed by default, and you can use that, but you can install the latest version if you wish.
Windows does not include a python distribution by default. Let's install one.
For the most part, programming languages either compile source code to a binary, or run the source code through an interpreter in order to execute commands. But some languages, like Python, fall in between these two categories. Here is a brief explanation.
Let's write the customary "Hello, world!" program in Python. It's only a single line of code, but it's a start.
Let's get started with the basic structure of a Python program.
Strings, integers, booleans and floats.
You can put variables almost anywhere you want in a Python file, but where you put them matters. Let's talk about scope.
Let's get started on making decisions based on what a user types in, and give the player the option of showing a help screen and quitting the game (or playing again).
Let's have a look at Python's comparison operators and logical operators. We'll be using them a lot, so a brief overview seems appropriate.
Let's make sure that we sanitize whatever the user types, and let's finish up printing our help information.
Let's spice things up a bit by adding colour to our application, and figuring out how to clear the screen before the game starts.
Classes are incredibly useful, and we'll be using them throughout the remainder of this course. Let's explore them a bit.
We need something to keep track of the player as he or she progresses through the game, so let's create a Player class to take care of this for us.
We need somewhere to store information about every room in the game, so let's create a Room class to take care of this.
Let's define a Game class that will hold the current player, the room that the player is currently in, and all other important game related information.
Let's get started using our Player, Room, and Game classes in our adventure game.
Tuples are like lists: they are one of Python's four data structures which allow us to store more than one thing in a variable. Let's use tuples to help provide more realistic descriptions for our game.
Let's get started with allowing players to navigate around the dungeon.
Like lists and tuples, dictionaries are data structures in Python that let us store more than one thing in a variable. Unlike lists and tuples, though, dictionaries allow us to store things using key-value pairs. Let's see how they work.
We have a lot of items in our armory.py file, so let's put some in our rooms, to give the player something to find. Let's also decide if a room should have a monster or not.
An overview of the Python list data type, and adding an attribute to the Player class to keep track of inventory.
Let's get started with the logic of allowing players to pick items up.
Let's finish up the code for picking an item up, and removing it from the room once we've done so.
Let's write a simple function that allows the player to print out the contents of the current inventory.
Right now, room descriptions are displayed every time we enter a command. That's no good, so let's clean our code up a bit.
Let's implement the functionality that allows the player to drop an item, and while we're at it, let's learn about the "try/except" functionality in Python, which is similar to "try/catch" in other languages.
We can now let players find items, drop items, and show the current inventory, so let's allow them to equip, or use, weapons, armor, and shields.
Let's show the player what items from their inventory that they have equipped, and let's consider how to solve one problem that we have in the game. Plus, I give you a bit of a challenge.
Here's how I solved the challenge.
Players can now find items, pick up items, use items, and drop items, and view the inventory. The last step is to let them "unequip," or stop using items. Let's take care of that.
Let's implement the "status" command, so players can see how well they are doing in the game.
A brief overview of how combat is going to work in our adventure game.
Let's get started with combat by letting the player run away when they encounter a monster.
Let's begin with the combat logic by determining who gets to attack first.
Let's get the initial code in place for the player's turn during combat.
Now it's the monster's turn, so let's implement the logic for this part of the fight.
If the player is doing badly in the fight, they should have the chance to run away. Let's write that logic.
Once a fight is over, the player should get feedback, one way or the other, and if the monster lost, we need to make sure that the monster is taken out of the game. Let's take care of that.
Combat should be harder (or easier) depending on what armour, weapons, or shield the player has, and depending how strong the monster is. Let's adjust our math accordingly.
Let's allow the player to rest in order to regain health.
Let's have a look at what the final map will look like.
We need to make some changes to the Game class to tell it about the map. Let's do that.
Player's should not be allowed to move "outside" of the map. In order to prevent that, we need to keep track of where the player is, and check that against the current map's dimensions. Let's do that.
Syntax errors are easy to spot, since your editor typically points them out to you. Logic errors are tougher. Here is a challenge that focuses on logic errors.
Let's try out our code and modify it so that it keeps track of the player's current position on the map.
Let's take advantage of the attribute "visited" that we added to the Player class in order to keep track of where the player has been on the map.
Let's get started implementing the "map" command to show a map to the player.
Let's make the map show us useful information.
Let's finish up our map command by adding some lines at the top and bottom, and adding a legend.
Let's give our player an easy way to see how things are going by using the blessings package to add a status bar at the top of the screen.
There are only a few things left to take care of in the game; let's write the last bit of code.
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.