Computer Science
Grade 8
20 min
Creating Characters: Defining Attributes and Actions
Students will learn to define character attributes (e.g., name, health) and basic actions in Python.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what attributes and actions are in the context of a game character.
Use a Python class to model a character.
Create attributes for a character using the `__init__` method.
Create methods for a character to represent its actions.
Instantiate (create) multiple, unique character objects from a single class.
Write code where one object's action affects another object's attributes.
Ever wonder how your favorite video game character knows its health, items, and special moves? 🎮 Let's build the blueprint for one ourselves!
In this lesson, we'll learn how to use Python classes to create digital characters for our interactive stories. We'll give them properties (attributes) like a name and health, and abilities (actions) like atta...
2
Key Concepts & Vocabulary
TermDefinitionExample
ClassA blueprint or template for creating objects. A class defines the attributes and methods that all objects of that type will have.A `Character` class is the blueprint. It says every character will have a name, health, and an attack action.
Object (or Instance)A specific item created from a class. It has its own unique set of attribute values.If `Character` is the class, then `wizard` and `dragon` are two separate objects (instances) created from that blueprint.
AttributeA variable that belongs to an object and stores a piece of information about it. It's like a noun or adjective describing the object.`name = 'Dragon'`, `health = 200`, `inventory = ['gold', 'gem']` are all attributes.
MethodA function that belongs to an object an...
3
Core Syntax & Patterns
Class Definition Syntax
class ClassName:
# Methods and attributes go here
Use the `class` keyword followed by a name (in CamelCase) and a colon. All code for the class must be indented underneath.
Constructor and Attribute Syntax
def __init__(self, param1, param2):
self.attribute_name = param1
self.other_attribute = param2
The `__init__` method is where you define and assign the initial values for an object's attributes. Use `self.` to attach a variable to the object.
Method Definition Syntax
def method_name(self, other_parameters):
# Code that performs an action
self.attribute_name = new_value
Define methods just like regular functions, but always include `self` as the first parameter. This allows the method to access and change the object&#...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You have a `Hero` class and a `Monster` class. The `Hero` class needs an `attack` method that reduces the `Monster`'s health. Which code snippet correctly implements this interaction?
`# Setup
class Monster:
def __init__(self):
self.health = 50
class Hero:
def __init__(self):
self.damage = 10
# ... attack method here ...
hero = Hero()
monster = Monster()`
A.def attack(self, target):
target.health -= self.damage
B.def attack(self, target):
self.health -= target.damage
C.def attack(self):
monster.health -= self.damage
D.def attack(target):
target.health -= 10
Challenging
What is the final value of `monster.health` after this code is executed?
`class Hero:
def __init__(self, name, power):
self.name = name
self.attack_power = power
def attack(self, enemy):
print(f'{self.name} attacks {enemy.name}!')
enemy.health -= self.attack_power
class Monster:
def __init__(self, name, health):
self.name = name
self.health = health
knight = Hero('Sir John', 15)
goblin = Monster('Gruk', 40)
dragon = Monster('Smaug', 100)
knight.attack(goblin)
knight.attack(dragon)
knight.attack(goblin)`
A.10
B.25
C.85
D.40
Challenging
A game needs a `Chest` class. When a `Player` object with a `has_key` attribute (True/False) uses an `open` method on a `Chest` object, the chest's `is_locked` attribute should become False, but only if the player has the key. Which implementation is the most robust and logical?
A.In the Player class: `def open_chest(self, chest):
if self.has_key:
chest.is_locked = False`
B.In the Chest class: `def be_opened(self, player):
if player.has_key:
self.is_locked = False`
C.In the Chest class: `def open(self):
self.is_locked = False`
D.In the Player class: `def open_chest(self, chest):
chest.is_locked = not self.has_key`
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free