Creating a Zerg Rush Bot in Python From Scratch

soO_Bot is down 0-1 in the Finals against Cure_Bot in ProBots.
The crowd is buzzing. soO_Bot needs to pull off something bold to stay in the match. There’s no time to play it safe—the best chance for a comeback is to go all-in with some perfectly executed 12 Pool Zerg Rush. Can you help soO_Bot even the score?

Objective:

Help soO_Bot Spawn Zerglings and Launch a Rush to the Enemy Base after 12 Supply!

This template sets up your bot’s race and libraries. Add your code in the on_step function to execute the zerg rush strategy. The on_step gets called every game loop.

from sc2.bot_ai import BotAI, Race
from sc2.data import Result
from sc2.ids.unit_typeid import UnitTypeId  # References StarCraft II unit types
class CompetitiveBot(BotAI):
    NAME: str = “CompetitiveBot”
    “””This bot’s name.”””
    RACE: Race = Race.Zerg
    “””
    This bot’s StarCraft 2 race.
    Options are:
        Race.Terran
        Race.Zerg
        Race.Protoss
        Race.Random
    “””
    async def on_start(self):
        “””
        This code runs once at the start of the game.
        Use this to set up initial state, build orders, etc.
        “””
        print(“Game started”)
    async def on_step(self, iteration: int):
        # on_step is called every game loop. This is where you collect info,
        # decide actions, and issue commands each iteration.
        # ‘pass’ is a placeholder so Python won’t throw an error for an empty function.
        # Remove or replace it once you add your real logic.
        pass

Stuck?

How can your bot know when it’s at 12 supply to start building a Spawning Pool? Explore the bot’s attributes related to “supply” in the python-sc2 documentation.

Nudge: Look into self.supply_used to check the current supply. Once you reach 12 supply, find out how to use a worker unit (Drone) to construct a building at a specific location. The build() function might help.

How can your bot train Zerglings after the Spawning Pool is complete? What unit is responsible for spawning Zerglings?

Nudge: Use the UnitTypeId.LARVA to find Larvae and command them to train Zerglings. After Zerglings are ready, use self.enemy_start_locations to identify where they should attack.

from sc2.bot_ai import BotAI, Race
from sc2.data import Result
from sc2.ids.unit_typeid import UnitTypeId
class CompetitiveBot(BotAI):
    NAME: str = “CompetitiveBot”
    “””This bot’s name”””
    RACE: Race = Race.Zerg
    “””This bot’s Starcraft 2 race.
    Options are:
        Race.Terran
        Race.Zerg
        Race.Protoss
        Race.Random
    “””
    async def on_start(self):
        “””
        This code runs once at the start of the game
        Do things here before the game starts
        “””
        print(“Game started”)
    async def on_step(self, iteration: int):
        if self.supply_used >= 12:
            if (
                    self.structures(
                        UnitTypeId.SPAWNINGPOOL).amount
                    + self.already_pending(
                        UnitTypeId.SPAWNINGPOOL)
                    == 0
                ):
                if self.can_afford(
                    UnitTypeId.SPAWNINGPOOL
                    ):
                    await self.build(
                        UnitTypeId.SPAWNINGPOOL,
                        near=self.townhalls.first
                    )
        if self.structures(
            UnitTypeId.SPAWNINGPOOL
            ).ready and self.larva:
            if self.can_afford(UnitTypeId.ZERGLING):
                self.train(
                    UnitTypeId.ZERGLING,
                    self.larva.amount)
        if self.units(UnitTypeId.ZERGLING).ready:
            for zergling in self.units(
                UnitTypeId.ZERGLING
                ):
                zergling.attack(
                    self.enemy_start_locations[0]
                    )

Scroll to Top