Learn CODECOMBAT with Real Code Examples
Updated Nov 26, 2025
Code Sample Descriptions
1
Hello World in CodeCombat (Python)
hero.say("Hello World")
In CodeCombat, instead of printing, learners issue commands to characters. This simple Python snippet makes the hero say 'Hello World'.
3
Attack Nearest Enemy
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
Commands the hero to attack the nearest enemy.
4
Collect Coin Example
coin = hero.findNearestItem()
if coin:
hero.move(coin.pos)
Makes the hero collect the nearest coin.
5
Move and Attack Sequence
hero.moveForward()
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
Moves the hero forward, then attacks an enemy.
6
Using While Loop
while True:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
else:
break
Keeps attacking enemies until none remain.
7
Move to Specific Position
hero.move({'x': 10, 'y': 20})
Moves the hero to a specified x,y coordinate.
8
Say a Message Multiple Times
for i in range(3):
hero.say("Hello")
Makes the hero say 'Hello' 3 times using a loop.
9
Attack if Health is Good
if hero.health > 50:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
Hero attacks only if health is above 50.
10
Collect All Items in Range
items = hero.findItems()
for item in items:
hero.move(item.pos)
Loops through all items and collects them.