Learn How to Code a Bot

When launching in the Code and Combat arena, a battle bot is placed in an online battle arena and will fight against other bots in the same arena. Initially, the bot has no code and will not do anything. However, it can be moved via keyboard commands. Pressing <cursor left> key will make it turn to the left. <cursor right> turns it to the right. <cursor up> will move it forward and <cursor down> will stop any movement. The bot shoots by pressing <space>.
To make the bot score and win in the battle field over a longer time period, it is best to attach code to it instead of commanding it via the keyboard. Once it is running with code, it will keep running in the game, even is the web browser is closed or logged out. Press the (<> Code) button in in the arena screen to start coding. Press the (run) button to deploy your code with the bot. The code can be refined at any point in time. Simply press (stop) and (run) again to deploy the new code.

Code and Combat Bots are written in JavaScript and can be edited via Blockly or a JavaScript editor. Here is an example of a very simple bot.

The bot starts movement in the current direction and then goes into an endless loop to control bot movements over time. Here it changes the direction by 90 degress to the right whenever the bot hits a wall. The resulting JavaScript code is very simple and looks like this:

move();
while (true) {
	if (check_wall()) {
  	turn_right();
  }
}

Bot commands are separated in two groups, Commands and Sensing. Commands change what a bot does and sensing captures data from the environment.

Bots are places in an arena that uses a coordinate system with the origin on the top left at (0,0) and the bottom right at (3200,1920). When degrees are used then it is using degree measures from 0 to 360, with 0 being on top at the 12 o'clock position and 90 to the right, 180 pointing to the bottom, 270 pointing to the left.

Bots can only have one shot in the air at the same time. Shots die when flying for longer than 5 seconds, when they hit a wall or when they hit another bot. A minimal Bot code starts by issuing the "move bot forward" command" and then goes into an endless loop to start other commands as soon as the bot runs into a wall, find enemies, tries to reach flags or similar.

Bots score points by capturing flags and killing other bots. To score higher, the bot example from above can be improved to capture more ground and run into more flags. This bot turns randomly right or left whenever it hits a wall, therefore finding more flags and also avoiding running in circles.

move();
while (true) {
	if (check_wall()) {
		if (Math.floor(Math.random() * 2)) turn_right(); else turn_left();
	}
}

Bots also score higher when they shoot since they can randomly kill other bots. As an example, a bot could shoot all time in the direction it is moving.

move();
while (true) {
	if (check_wall()) {
  	turn_right();
  }
  shoot();
}

Even better, a bot can shoot in a random direction to increase its probability to hit another bot.

move();
while (true) {
	if (check_wall()) {
  	turn_right();
  }
  shoot_to(Math.floor(Math.random() * 360));
}



Here is the complete list of all bot commands

move()

Moves the bot forward. It will keep moving until the stop command is issued. If the bot hits a wall, it will stop but will keep moving as soon as the bot turns into a new direction that is not blocked.

stop()

Stops the bot moving forward. It will be stopped until the move forward command is issued.

turn_left()

Turns the bot left by 90 degrees.

turn_right()

Turns the bot right by 90 degrees.

turn_by(degree)

Turns the bot by a given amount of degrees. This can be positive or neagtive. -90 would turn it by 90 degrees to the left.

turn_to(degree)

Turns the bot to an absolute direction. Input of 0 would turn it to the 12 o'clock position. 90 would turn it facing right.

turn_at(x,y)

Turns the bot towards a coordinate. The degree will be calculated automatically and then applied. The top left coordinate is (0,0), so with that input it will likely somewhere between 270 and 0 degrees, depending on where the bot is in the arena.

shoot()

This will shoot in the direction where the bot is facing. Only one shot can be in the air at the same time. Shooting while another shot is in the air will have no affect.

shoot_to(degree)

Shoots to a given angle/degree. For example, 90 would shoot to the right, no matter where the bot is currently facing.

shoot_at(x,y)

Shoots towards a coordinate. This calculates the right angle and then shoot towards the coordinate.

print(message)

This command lets you see any variable or output. It will be shown in the Bot Console that can be enabled via an on/off button in the arena settings. For example, you can read the flag locations and then print them to see if everything is working. This is mostly used for trouble shooting the bot code.



Here is the complete list of all sensing commands

b = check_wall()

Returns TRUE or FALSE depending on if a wall is hit when the bot was moving in its forward direction.

time = get_time()

Returns the time in milliseconds since the bot has been alive. One second has 1000 milliseconds.

y = get_x()

Returns the current x position of the bot in the arena.

y = get_y()

Returns the current y position of the bot in the arena.

d = get_d()

Returns the degree the bot is currently facing.

b = scan_ready()

Scanning for enemies has a cool down period of 5 seconds. If a scan is ready, it returns TRUE.

slist = scan()

This datapoint triggers a scan and returns a comma separated STRING of enemies that were found. Scanning scans a radius of 800 pixels. The comma separated list has 4 data points for each bot that was found. NAME, X, Y, DISTANCE. For example: "droid1, 100, 100, 75, droid2, 234, 910, 840". You need to use the Blockly function that converts a string to a list before processing these results.

sflags = get_flags()

Return the list of all flags in the arena as a STRING. This data is always available and has the same format as the bot scanner. NAME, X, Y, DISTANCE. For example: "flag1, 100, 100, 75, flag2, 234, 910, 840". You need to use the Blockly function that converts a string to a list before processing these results. The data allows the bot to navigate to all flags to capture the most amount quickly.

b = wall_at(x,y)

Uses the on board laser to check if there is a wall from the current bot position to the x and y coordinate given. Returns TRUE if the given coordinate is blocked by a wall. This can be used to check if a flag is reachable or not. For example, if the first parameter is 0 and the seconds is also 0, then it will check for a reachable path to the top left position in the arena.

b = wall_around(degree, distance)

Uses the on board laser to check if there is a wall from the current bot position by giving a degree and a distance. Returns TRUE is the path is blocked by a wall. For example, if the first parameter is 90 and the second is 50, then it will check for a reachable path for 50 pixels to the right of the bot.



Advanced bots that score high find other bots via radar, shoot at them and deploy strategies to seek the next flag to capture. This example shows how to use radar to directly shoot at an enemy bot.

move();
while (true) {
	if (check_wall()) {
  	turn_right();
  }
  if (scan_ready()) {
  	var enemy_list = scan().split(",");
    if (enemy_list.length > 1) {
    	print("shooting at " + enemy_list[0]);
      shoot_at(enemy_list[1], enemy_list[2]);
    }
  }
}


The following example looks for all flags and uses the laser to see if one of them is reachable. If yes, it will send the bot's direction towards the reachable flag.

move();
while (true) {
	if (check_wall()) {
		turn_right();
	}
	var sflags = get_flags().split(",");
	if (sflags.length > 1) {
		for (var i = 0; i < sflags.length; i+=4) {
			if (!wall_at(sflags[i+1],sflags[i+2])) {
				turn_at(sflags[i+1], sflags[i+2]);
					print("turning to flag " + sflags[i+0]);
 					break;
			}
		}
	}
}