Variables are like storage boxes where we keep data. Here's how to create one:
character_name = "Sir Python"
Here, character_name
is the variable, and "Sir Python"
is the value it's storing. The =
is the assignment operator, it's used to assign the value to the variable.
In Python, variables don't need a predefined data type, like in some other languages. There are several data types that Python recognizes, and it will identify them automatically:
- String (str): Text data enclosed in quotes.
- Integer (int): Whole numbers.
- Float (float): Decimal numbers.
- Boolean (bool): True or False values.
Try assigning different types of variables:
character_level = 1 # Integer
character_alive = True # Boolean
character_hp = 100.0 # Float
Arithmetic operations are the math operations you know and love:
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Floor division (
//
- it rounds the result down to the nearest whole number) - Modulus (
%
- it gives the remainder of a division) - Exponentiation (
**
)
Let's try to make a basic combat calculation:
enemy_hp = 80
damage = 30
# attack!
enemy_hp = enemy_hp - damage # you can also write this as: enemy_hp -= damage
After running this, enemy_hp
will now be 50.
The print
function is used to output data. It's the most basic way to see what your program is doing. Here's how to use it:
print(character_name)
If you run this code, Python will output: Sir Python
A list is a collection of items, in a specific order. You can make a list of any objects, including numbers, strings, and even other lists.
inventory = ["sword", "shield", "health potion"]
You can access elements in the list by their index. Indices start from 0, so inventory[0]
will give you "sword".
If statements let your program make decisions. They work with boolean expressions (something that's either True
or False
).
if character_alive:
print("You are still adventuring.")
else:
print("Game Over.")
Here, if character_alive
is True
, Python will execute the first block of code. If character_alive
is False
, Python will execute the block under else
.
A while
loop runs as long as a certain condition is True
. Let's create a basic game loop:
while character_alive:
print("Adventuring...")
# here's where we'd put game logic, like enemy encounters
This loop will keep printing "Adventuring..." as long as character_alive
is True
.
Functions are pieces of reusable code. They execute a block of code when they're called by their name. Functions are defined using the def
keyword:
def drink_potion():
global character_hp # we use 'global' to let the function modify this variable
print("You drink a health potion.")
character_hp += 50
You can "call" or use this function like this: drink_potion()
Dictionaries are like lists, but they use keys to access values, instead of an index. Keys can be of any immutable data type.
character_stats = {
"Strength": 10,
"Defense": 5,
"HP": 100
}
Here, "Strength", "Defense", and "HP" are keys, and 10, 5, and 100 are their values. You can access values like this: character_stats["Strength"]
Now let's put everything together into a simple RPG game. We'll also introduce two more concepts: the input
function, which asks the user for input, and the random
library, which allows us to generate random numbers.
Before we start, we need to import the random
library:
import random
Let's initialize our game characters:
# Player
# Player
player = {
"Name": "Sir Python",
"HP": 100,
"Damage": (10, 20), # Damage will be a random number between these two
"Potions": 3 # The player starts with three potions
}
# Enemy
enemy = {
"Name": "Code Ogre",
"HP": 150, # The enemy is stronger now
"Damage": (20, 40) # The enemy deals more damage
}
Next, let's define a function that will simulate one character attacking another:
def attack(attacker, defender):
# Calculate random damage
damage = random.randint(attacker["Damage"][0], attacker["Damage"][1])
defender["HP"] -= damage
print(f"{attacker['Name']} attacks! {defender['Name']} takes {damage} damage.")
print(f"{defender['Name']}'s HP: {defender['HP']}\n")
Let's also define the drink_potion
function:
def drink_potion(character):
if character["Potions"] > 0:
character["HP"] += 50 # Restore 50 HP
character["Potions"] -= 1 # Use up one potion
print(f"{character['Name']} drinks a potion and recovers 50 HP.")
print(f"{character['Name']}'s HP: {character['HP']}\n")
else:
print(f"{character['Name']} has no potions left!\n")
Now, let's set up our game loop:
while True:
# Player's turn
print(f"{player['Name']} HP: {player['HP']}, Potions: {player['Potions']}")
action = input("Enter 'a' to attack or 'p' to drink a potion: ")
if action.lower() == 'a':
attack(player, enemy)
if enemy["HP"] <= 0:
print("You have defeated the Code Ogre. Congratulations!")
break
elif action.lower() == 'p':
drink_potion(player)
# Enemy's turn
if enemy["HP"] > 0:
attack(enemy, player)
if player["HP"] <= 0:
print("You have been defeated by the Code Ogre. Game Over.")
break
This game loop will run until either the player or the enemy has 0 or less HP.
Go ahead, run the program and fight the Code Ogre!
The Code Ogre might be a tough opponent for Sir Python to handle on his own. Feel free to expand this game by adding items, spells, more complex battle mechanics, or even more characters. Remember, the key to learning programming is practice and creativity. Good luck, and happy coding!