Basics

Whether you're a complete beginner or an experienced programmer, this guide will help you understand the fundamental concepts of Python that you'll use to control Gizmo in the game.

What is Python?

Python is a high-level, interpreted programming language known for its simplicity and readability. In Droning On, you'll use Python to write scripts that control Gizmo's actions in the game world.

Variables

Variables are used to store data in your program. In Python, you don't need to declare the type of a variable explicitly.

# Assigning values to variables
gizmo_health = 100
gizmo_name = "Gizmo"
is_moving = True

# Using variables
print(f"{gizmo_name}'s health is {gizmo_health}")

Data Types

Python has several built-in data types. The most common ones you'll use in Droning On are:

  • Integers (int): Whole numbers, e.g., 5, -3, 0
  • Floating-point numbers (float): Decimal numbers, e.g., 3.14, -0.5
  • Strings (str): Text, e.g., "Hello", 'Gizmo'
  • Booleans (bool): True or False
# Examples of different data types
distance = 5  # int
energy_level = 75.5  # float
message = "Move forward"  # str
is_hazard_detected = False  # bool

Functions

Functions are reusable blocks of code that perform specific tasks. In Droning On, you'll use both built-in functions (provided by the game) and create your own functions.

# Using a built-in Droning On function
current_position = get_position()
print(f"Gizmo is at position: X={current_position.x}, Y={current_position.y}, Z={current_position.z}")

# Defining your own function
def move_to_coordinates(x, y, z):
    current_pos = get_position()
    while current_pos.x != x or current_pos.y != y or current_pos.z != z:
        # Implement movement logic here
        current_pos = get_position()
    print("Reached the target coordinates!")

# Calling your custom function
move_to_coordinates(10, 0, 5)

Conditional Statements

Conditional statements allow your code to make decisions based on certain conditions. The main constructs are if, elif (else if), and else.

# Example of conditional statements
energy = get_energy().current_energy

if energy > 75:
    print("Energy levels are high. Let's explore!")
elif energy > 25:
    print("Energy levels are moderate. Proceed with caution.")
else:
    print("Energy levels are low. Find a charging station!")

Loops

Loops allow you to repeat a block of code multiple times. The two main types of loops in Python are for loops and while loops.

# For loop example
for i in range(4):
    turn(TurnDirectionEnum.Right)
    move(MoveDirectionEnum.Forward)

# While loop example
while not detect_station().detected:
    move(MoveDirectionEnum.Forward)
    if detect_hazard().detected:
        turn(TurnDirectionEnum.Right)

Lists

Lists are ordered collections of items. They can contain elements of different types.

# Creating and using a list
directions = [MoveDirectionEnum.Forward, MoveDirectionEnum.Right, MoveDirectionEnum.Backward, MoveDirectionEnum.Left]

for direction in directions:
    if move(direction):
        print(f"Moved {direction}")
    else:
        print(f"Couldn't move {direction}")

Dictionaries

Dictionaries are collections of key-value pairs. They're useful for storing and retrieving data with meaningful keys.

# Creating and using a dictionary
station_types = {
    "health": "Repair Station",
    "energy": "Charging Station"
}

detected_station = detect_station()
if detected_station.detected:
    station_purpose = "unknown"
    for key, value in station_types.items():
        if key in detected_station.name.lower():
            station_purpose = value
    print(f"Found a {station_purpose}!")

Best Practices

  1. Indentation: Python uses indentation to define code blocks. Consistent indentation is crucial for your code to work correctly.

  2. Comments: Use # for single-line comments and """ for multi-line comments. Good comments explain why, not what.

  3. Naming Conventions:

    • Use lowercase with underscores for function and variable names (e.g., move_forward, energy_level).
    • Use CamelCase for class names (e.g., MoveDirectionEnum).
  4. Error Handling: Use try-except blocks to handle potential errors gracefully.

    try:
       move(MoveDirectionEnum.Forward)
    except Exception as e:
       print(f"An error occurred: {e}")
  5. Code Reusability: Define functions for tasks you perform repeatedly.

  6. Keep it Simple: Write clear, simple code. It's easier to understand and debug.

Remember, practice is key to improving your Python skills. Experiment with different code structures and functions in Droning On to become a more proficient programmer and master the game!