1. StarCraft II Python Bot: Gathering resources

 # Command Center

The Command Center is the main building you need. On it you build your workers. You start with one the match and can expand your territory with it.

# Building the workers

async def buildWorkers(self):
for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue:
if self.can_afford(UnitTypeId.SCV) and self.workers.amount < self.units(UnitTypeId.COMMANDCENTER).amount * 14 + 4:
await self.do(commandcenter.train(UnitTypeId.SCV))
This is simply a function to tell the Terran Command Center to create SCVs. We need to use it in the class, so let’s put it in the class and call it in the on_sept() function. The final script of this part should look like this:
import sc2
from sc2 import run_game, maps, Race, Difficulty
from sc2.player import Bot, Computer
from sc2.ids.unit_typeid import *
class AlanBot(sc2.BotAI):
async def on_step(self, iteration):
await self.buildWorkers()
async def buildWorkers(self):
for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue:
if self.can_afford(UnitTypeId.SCV) and self.workers.amount < self.units(UnitTypeId.COMMANDCENTER).amount * 14 + 4:
await self.do(commandcenter.train(UnitTypeId.SCV))
run_game(maps.get("AbyssalReefLE"), [
Bot(Race.Terran, AlanBot()),
Computer(Race.Zerg, Difficulty.Hard)
], realtime=True)

# Gatering minerals

The developers of this library have already implemented a function to distribute_workers(), it will gather the minerals and Vespene Gas, but it won’t create create the Vespene Refinery (in Terran case). We need to create the Refineries too.
import sc2
from sc2 import run_game, maps, Race, Difficulty
from sc2.player import Bot, Computer
from sc2.ids.unit_typeid import *
class AlanBot(sc2.BotAI):
async def on_step(self, iteration):
await self.buildWorkers()
await self.distribute_workers()
async def buildWorkers(self):
for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue:
if self.can_afford(UnitTypeId.SCV) and self.workers.amount < self.units(UnitTypeId.COMMANDCENTER).amount * 14 + 4:
await self.do(commandcenter.train(UnitTypeId.SCV))
run_game(maps.get("AbyssalReefLE"), [
Bot(Race.Terran, AlanBot()),
Computer(Race.Zerg, Difficulty.Hard)
], realtime=True)

# Gathering vesper gas

In order to collect the vespene gas, we need refineries (or Extractors or Assimlators for other Races) for the Terran SVCs.
async def buildRefineries(self):
for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready:
vespenes = self.state.vespene_geyser.closer_than(18.0, commandcenter)
for vespene in vespenes:
if not self.can_afford(UnitTypeId.REFINERY):
break
worker = self.select_build_worker(vespene.position)
if worker is None:
break
if not self.units(UnitTypeId.REFINERY).closer_than(1.0, vespene).exists:
await self.do(worker.build(UnitTypeId.REFINERY, vespene))

Write this as a class function of our bot and call it on the on_step() function:
import sc2
from sc2 import run_game, maps, Race, Difficulty
from sc2.player import Bot, Computer
from sc2.ids.unit_typeid import *
class AlanBot(sc2.BotAI):
async def on_step(self, iteration):
await self.buildWorkers()
await self.distribute_workers()
await self.buildRefineries()
async def buildWorkers(self):
for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue:
if self.can_afford(UnitTypeId.SCV) and self.workers.amount < self.units(UnitTypeId.COMMANDCENTER).amount * 14 + 4:
await self.do(commandcenter.train(UnitTypeId.SCV))
async def buildRefineries(self):
for commandcenter in self.units(UnitTypeId.COMMANDCENTER).ready:
vespenes = self.state.vespene_geyser.closer_than(18.0, commandcenter)
for vespene in vespenes:
if not self.can_afford(UnitTypeId.REFINERY):
break
worker = self.select_build_worker(vespene.position)
if worker is None:
break
if not self.units(UnitTypeId.REFINERY).closer_than(1.0, vespene).exists:
await self.do(worker.build(UnitTypeId.REFINERY, vespene))
run_game(maps.get("AbyssalReefLE"), [
Bot(Race.Terran, AlanBot()),
Computer(Race.Zerg, Difficulty.Hard)
], realtime=True)

You should now have workers being created and gathering minerals and vespene gas for you.