Your First Tank
Basics
Clone the template repository of your preferred language.
Language |
---|
Python |
Rust |
Dart |
Java |
Javascript |
You will edit the file within tanks/
to create your tank. You may create as many tanks as you wish, however each tank's code must be in its own file and follow some basic requirements. These are usually the imports at the top of the file, the tank class, the run() and onEvent() tank methods, and the createTank() method that returns a instance of your tank.
- Python
- Dart
- Rust
spin_bot.py
from codetanks import BaseTank, commands
class MyTank(BaseTank):
"""Simple Python spinning tank
"""
def __init__(self):
super().__init__()
self.direction = 0
print('Running my spinning tank!')
def run(self):
if self.direction % 2 == 0:
self.commands.append(commands.MOVE_BACKWARD | commands.ROTATE_TANK_COUNTER_CLOCKWISE | commands.FIRE)
else:
self.commands.append(commands.MOVE_FORWARD | commands.ROTATE_TANK_CLOCKWISE | commands.FIRE)
def on_event(self, event):
self.direction = self.direction + 1
def create_tank():
"""Return an instance of your tank
"""
return MyTank()
spin_bot.dart
import 'package:codetanks/codetanks.dart';
class MyTank extends BaseTank {
int i = 0;
MyTank() {
print("Created my spin tank!");
}
void run() {
if (i % 2 == 0) {
commands.add(Command.MOVE_FORWARD | Command.ROTATE_TANK_CLOCKWISE | Command.FIRE);
} else {
commands.add(Command.MOVE_BACKWARD | Command.ROTATE_TANK_COUNTER_CLOCKWISE | Command.FIRE);
}
print(commands);
}
void onEvent(Map event) {
print(event);
i = i + 1;
print(i);
}
}
BaseTank createTank() => MyTank();
spin_bot.rs
use ct_api::*;
use serde_json::Value;
pub struct MyTank {
i: u32
}
impl Tank for MyTank {
fn run(&mut self, commands: &mut Vec<Command>) {
if self.i % 2 == 0 {
commands.push(Commands::MOVE_FORWARD | Commands::ROTATE_TANK_CLOCKWISE | Commands::FIRE);
} else {
commands.push(Commands::MOVE_BACKWARD | Commands::ROTATE_TANK_COUNTER_CLOCKWISE | Commands::FIRE);
}
}
fn on_event(&mut self, commands: &mut Vec<Command>, event: &Value) {
self.i += 1;
}
}
pub fn create_tank() -> MyTank {
MyTank { i: 0 }
}
This a basic tank that spins and fires. Upon any event, it will spin in the opposite direction. For more details, check out Anatomy, Physics & World, Commands, Debugging, Events, and Game Engine Logic.
Building
To build your code
build.sh my_tank.py
Running
ctdesktop TANK_HASH ANOTHER_TANK_HASH ETC_TANK_HASHES
You may enter multiple tank hashes as well as duplicate tank hashes.