battlesnake-builder
Easily build a battlesnake in python
Installation
$ pip install battlesnake_builder
Requirements
- python >= 3.10
- Flask >= 2.0.2
Examples
Basic snake that always moves up
from battlesnake_builder import BattleSnake
my_snake = BattleSnake()
@my_snake.on_move
def move(data, _store):
return "up"
my_snake.run()
More advanced snake that looks for food
from battlesnake_builder import BattleSnake
my_snake = BattleSnake()
@my_snake.on_move
def move(data, _store):
me = data.you
closest_food = data.board.closest_food(me)
if closest_food.y > me.head.y:
return "up"
elif closest_food.y < me.head.y:
return "down"
if closest_food.x > me.head.x:
return "right"
return "left"
my_snake.run()
Snake with static config
from battlesnake_builder import BattleSnake, Config
my_snake = BattleSnake(Config(apiversion="0.1"))
Or
from battlesnake_builder import BattleSnake
my_snake = BattleSnake({
"apiversion": "0.1"
})
Snake with dynamic config
from battlesnake_builder import BattleSnake
import random
my_snake = BattleSnake()
@my_snake.on_config
def config():
tails = ["default","curled"]
return {
"tail": random.choice(tails)
}
my_snake.run()
You can also mix a static and dynamic config. If you overwrite an existing value, the newer one is getting adapted.
Shout something
@my_snake.on_move
def move(_data, _store):
return {
"move": "up",
"shout": "WHY ARE WE SHOUTING"
}
Snake that stores data across events
from battlesnake_builder import BattleSnake
my_snake = BattleSnake()
@my_snake.on_start
def start(data, store):
store["snake_amount"] = len(data.board.snakes)
@my_snake.on_move
def end(data, store):
shout = "Everyone is alive"
if len(data.board.snakes) < store["snake_amount"]:
shout = "Some snake died!"
return {
"move": "up",
"shout": shout
}
my_snake.run()
All features
from battlesnake_builder import BattleSnake, Config, Data
my_snake = BattleSnake(Config(tail="curly"))
@my_snake.on_config
def config():
return {
"head": "default"
}
@my_snake.on_start
def start(data: Data, store: dict):
store["start_snake_size"] = len(data.board.snakes)
@my_snake.on_move
def move(data: Data, store: dict):
shout = "Everything is fine"
if store["start_snake_size"] != len(data.board.snakes):
shout = "Oh no! Someone lost :("
for m in ["up", "down", "left", "right"]:
if data.board.is_move_safe(data.you, m):
return {
"move": m,
"shout": shout
}
return {
"move": "up",
"shout": shout
}
@my_snake.on_end
def end(data: Data, store: dict):
end_snake_size = len(data.board.snakes)
print(f"Out of {store['start_snake_size']} Snakes, only {end_snake_size} survived")
my_snake.run()
Metadata
Release files for battlesnake-builder 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| battlesnake_builder-0.1.2.tar.gz | 6.6 kB | Without provenance | Details for battlesnake_builder-0.1.2.tar.gz |
Release files / battlesnake_builder-0.1.2.tar.gz
| Download URL | battlesnake_builder-0.1.2.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f6178ac6c51db1d6e159842034651e744076d96fcba5678bdfaf8acb1ee98e1b
|
|
BLAKE2b-256 checksum How to use checksums |
8e25b7382799d3ef55ac4e7800f2e3d67d04af721c86e522167ba9609b990a40
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.0 CPython/3.9.12
|
Security for battlesnake-builder 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.