Learning LibraryCore Coding LibraryTeens

Classes and Objects in Python: blueprints and the things they make

The Ship and Enemy you use in the game course are classes — blueprints. Every ship on screen is an object built from that blueprint, with its own position and its own draw().

The big idea

A class is a blueprint that bundles data and behavior; an object is one instance made from it, with its own copy of the data.

See it in code

1The basics

A class is a blueprint. __init__ sets up each new object's data, and self refers to the specific object being built. Here Hero stores one piece of data — a name:

python
class Hero:
    def __init__(self, name):
        self.name = name

h = Hero("Nova")
print(h.name)
Run it — one object built from the blueprint:
Nova

Hero("Nova") builds one object from the blueprint. self.name = name saved its data; h.name reads it back.

2A step further

Add a method and make more than one object. Each Hero gets its own health, and status is an action it can perform. Changing one object leaves the other untouched:

python
class Hero:
    def __init__(self, name):
        self.name = name
        self.health = 100

    def status(self):
        return self.name + " has " + str(self.health) + " HP"

a = Hero("Nova")
b = Hero("Vex")
b.health = 60

print(a.status())
print(b.status())
Run it — two objects, each with its own data:
Nova has 100 HP
Vex has 60 HP

Two objects from one blueprint — changing b.health left a alone. That independence is the whole point of objects.

3In our world

Same pattern, now a game object: a Coin. __init__ sets up each coin's data (x, y, value) — self is the specific coin being made. describe is a method it performs. We stamp out two independent coins:

python
class Coin:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.value = 10

    def describe(self):
        return f"Coin worth {self.value} at ({self.x}, {self.y})"

a = Coin(100, 200)
b = Coin(300, 50)
b.value = 25

print(a.describe())
print(b.describe())
Run it — two coins from one blueprint, each with its own data:
Coin worth 10 at (100, 200)
Coin worth 25 at (300, 50)

One class, two objects — and changing b.value left a untouched, because each object holds its own data. That's exactly how Enemy(240, 300) and Enemy(60, 90) are two independent enemies from the same Enemy class.

The same idea, everywhere

Classes model any 'thing' with state and actions: a Player, an Account, a Sprite, a Matrix. They keep related data and the functions that act on it together, so large programs stay organized. Every object you've used — a list, a string, a game entity — is an instance of some class.

Try it yourself

Add a collect() method that sets self.value = 0 and returns the old value. Then make three coins in a list and loop over them printing each describe().

The common mistake

Forgetting self, or confusing the class with an object. Every method's first parameter must be self, and you read data as self.x inside the class. Coin is the blueprint; Coin(100, 200) is an actual coin — you draw and move objects, not the class.

What it unlocks

Classes formalize methods and attributes, and explain how the sprites and entities in the game course carry their own position and behavior.