Creating a Zerg Rush Bot in Python From Scratch

Mission Briefing:

soO_Bot is down 0-1 in the ProBots Finals. The arena buzzes. Playing it safe is not an option. The only way back into this match? An all-in Zerg Rush.

You are now soO_Bot’s AI Commander. Build fast. Hit hard. Even the score.


Setup Environment

Goal:
Create your own copy of the starter bot and launch a custom game.

Tasks:

  • Go to the VersusAI SC2 Bot Template.
  • Click the “Use this template” button (top right) and create a new repository under your GitHub account.
  • Clone your new repository to your machine:
    git clone <your_git_clone_repo_url_here>
    
  • Move into the bot folder:
    cd <bot_folder_name_here>
    
  • Install the required Python packages:
    pip install -r requirements.txt
    
  • Test that the bot runs:
    python ./run.py
    

Checkpoint:
You should see “Game started” printed in your terminal when the match begins.

Note:
Need help setting up? Check the Python-SC2 installation guide.


Build a Spawning Pool

Goal:
Start building a Spawning Pool after reaching 12 supply.

Checkpoint:
A Spawning Pool should be under construction shortly after you hit 12 supply.

Tip:
Pay attention to your supply count and resource management.

Note:
You may find how to build a structure helpful.

Need a nudge? (click to reveal example):
if self.supply_used >= 12:
    if self.can_afford(UnitTypeId.SPAWNINGPOOL):
        self.workers.random.build(UnitTypeId.SPAWNINGPOOL)

Train Workers to 16 Supply

Goal:
Ensure your economy grows steadily by training Drones until you reach 16 supply.

Checkpoint:
You should have at least 16 workers gathering resources.

Tip:
Monitor both your supply and the number of workers.

Note:
Learn about training units from Larva. You can also distribute workers to optimize gathering.

Need a nudge? (click to reveal example):
if self.can_afford(UnitTypeId.DRONE) and self.supply_workers < 16:
    self.train(UnitTypeId.DRONE)

Build an Extractor

Goal:
Secure gas by building an Extractor on the closest Vespene Geyser.

Checkpoint:
Your Extractor should be built near your Hatchery.

Tip:
You’ll need a Drone to construct it.

Note:
Look into finding the nearest Vespene Geyser to a Drone.

Need a nudge? (click to reveal example):
if self.gas_buildings.amount + self.already_pending(UnitTypeId.EXTRACTOR) == 0:
    if self.can_afford(UnitTypeId.EXTRACTOR):
        drone = self.workers.random
        target = self.vespene_geyser.closest_to(drone)
        drone.build_gas(target)

Build Overlords When Needed

Goal:
Avoid getting supply blocked by producing Overlords when supply is low.

Checkpoint:
Your supply should not cap while training more Drones or units.

Tip:
Build Overlords before you hit the cap.

Note:
Watch your supply_left and manage Overlord production.

Need a nudge? (click to reveal example):
if self.supply_left < 2 and self.already_pending(UnitTypeId.OVERLORD) < 1:
    if self.can_afford(UnitTypeId.OVERLORD):
        self.train(UnitTypeId.OVERLORD)

Expand After 14 Supply

Goal:
Secure a second Hatchery to increase larva production.

Checkpoint:
A second Hatchery should be underway once your Extractor is ready and you have 14+ supply.

Tip:
Timing your expansion is key to maintaining pressure.

Note:
Learn about building structures.

Need a nudge? (click to reveal example):
if self.supply_used >= 14 and self.structures(UnitTypeId.EXTRACTOR).ready:
    if self.structures(UnitTypeId.HATCHERY).amount <= 2 and not self.already_pending(UnitTypeId.HATCHERY):
        if self.can_afford(UnitTypeId.HATCHERY):
            await self.expand()

Spawn Zerglings and Train Queens

Goal:
Start building your army by producing Zerglings and Queens.

Checkpoint:
You should see Zerglings and Queens spawning from your Hatchery.

Tip:
Use your Larva wisely and prioritize what you need.

Note:
You’ll be using Larva to train Zerglings and Hatcheries to train Queens. Check UnitTypeId references if needed.

Need a nudge? (click to reveal example):
if self.structures(UnitTypeId.SPAWNINGPOOL).ready and self.larva:
    if self.can_afford(UnitTypeId.ZERGLING):
        self.train(UnitTypeId.ZERGLING, self.larva.amount)

elif (self.units(UnitTypeId.QUEEN).amount + self.already_pending(UnitTypeId.QUEEN) < self.townhalls.amount
        and self.structures(UnitTypeId.SPAWNINGPOOL).ready):
    if self.can_afford(UnitTypeId.QUEEN):
        self.train(UnitTypeId.QUEEN)

Research Zergling Speed

Goal:
Research Zergling Movement Speed to make your army faster and deadlier.

Checkpoint:
The Zergling Speed upgrade should start researching after your Spawning Pool and Extractor are ready.

Tip:
Ensure you have enough Vespene gas saved up.

Note:
Learn how to research upgrades.

Need a nudge? (click to reveal example):
if self.already_pending_upgrade(UpgradeId.ZERGLINGMOVEMENTSPEED) == 0:
    if self.structures(UnitTypeId.SPAWNINGPOOL).ready:
        if self.can_afford(UpgradeId.ZERGLINGMOVEMENTSPEED):
            self.research(UpgradeId.ZERGLINGMOVEMENTSPEED)

Inject Larvae with Queens

Goal:
Use Queens to inject Larva into your Hatchery and increase production.

Checkpoint:
You should see the Larva injection animation or increased Larva production.

Tip:
A Queen needs enough energy to inject.

Note:
Explore Queen abilities and Hatchery buffs. You may also reference BuffId information.

Need a nudge? (click to reveal example):
main = self.townhalls.first
for queen in self.units(UnitTypeId.QUEEN):
    if queen.energy >= 25 and not main.has_buff(BuffId.QUEENSPAWNLARVATIMER):
        queen(AbilityId.EFFECT_INJECTLARVA, main)

Attack Enemy Base with Zerglings

Goal:
Use your Zerglings to aggressively attack the enemy’s starting location.

Checkpoint:
Zerglings should swarm towards the enemy

Tip:
Speedlings can overrun early defenses if timed correctly.

Note:
Learn how to find enemy start locations and issue attack commands.

Need a nudge? (click to reveal example):
for zergling in self.units(UnitTypeId.ZERGLING):
    zergling.attack(self.enemy_start_locations[0])

Extra:


Share Your Hints

Finished the challenge?
Share a hint, a small tip, or what helped you if you figured out a tricky part. Your input could help others!