Take your Python skills to the next level with Battlesnake

So you want to learn Python? In fact if you want to learn any kind of coding, one of the best things to do, is work on a project.

I came across Battlesnake through a friend. I can definitely say this has been a super fun journey. Not only have I met some really cool people through live streaming this project, I've had a lot of fun upping my Python skills, and seeing the results of my coding.

What is Battlesnake?

Battlesnake is a coding competition where you program a snake (remember the old Nokia phone game) and compete against others. Your snake is hosted on a server and requests are sent between the Battlesnake server and your server. This means you don't need to sit down and code against someone. Program your snake, upload the code, and watch your snake battle against others. Battling happens constantly and you can go back and watch your games whenever you want.

A Battlesnake game consists of four snakes on a grid. The last snake standing wins. You must ensure your snake doesn't run out of health, so you need to eat the food on the board. Your snake also can't run off the board or run into other snakes, including yourself. How you program your snake is up to you.

Programming your Battlesnake

One of the coolest things about Battlesnake is how easy it is to get going. The Battlesnake Docs explain how to get started with the click of a button. If you really want to dive into the code, you can go straight to the GitHub repo and clone or fork the starter snake.

GitHub logo BattlesnakeOfficial / starter-snake-python

Getting started with Battlesnake and Python

Getting started with Battlesnake and Python

Battlesnake Logo

This is a basic implementation of the Battlesnake API in Python. It's a great starting point for anyone wanting to program their first Battlesnake using Python, and comes ready to deploy with Replit and Heroku, or you can use any other cloud provider you'd like.

Technologies Used

Quickstart

The Quick Start Coding Guide provides the full set of instructions to customize, register, and create your first games with your Battlesnake! While the guide uses Repl.it as an example host, the instructions can be modified to work with any hosting provider. You can also find advice on other hosting providers on our Hosting Suggestions page.

Prerequisites


Customizing Your Battlesnake

Locate the handle_info function inside server.py. At the end of that function you should see a line that looks like this:

return {
    "apiversion": "1"
    "author"
Enter fullscreen mode Exit fullscreen mode

Once you have your repo cloned or forked, you can start coding. The Docs has a tonne of information about how to code your snake and what kind of requests the Battlesnake server will ask your server.

How you program your snake is up to you. What problems do you want to focus on and how? Do you want to avoid the walls first? Do you want to avoid other snakes second? What strategy do you want to win? Will you check your snake's games and refine the code? What about using machine learning?

When you have your snake 'ready' you can enter it into a league. The leagues are really fun for seeing just how well your snake performs. It's awesome going back to the league's ladder board every few days and checking on where your snake sits on the ladder. You can also rewatch games to see how your snake wins or loses. This is useful for deciding how to refine your code and what strategies to build next.

If you like Python, this is a great project for upping your Python skills.

Using languages other than Python

What if you want to do something other than Python? Battlesnake has you covered. You can write your Battlesnake in any language. There are starter snakes starting code for a bunch of popular languages:

  • Python
  • Go
  • JavaScript
  • TypeScript

Those are the official starter snakes made by the Battlesnake team. There are also lots of Community Starter projects for languages including:

  • TypeScript
  • Node.js
  • Java
  • Ruby
  • C#
  • Scala
  • PHP
  • Rust
  • C++
  • Node

So choose your weapon - I mean, code - and get started. It's such a great community to be a part of and there's even live streamed league finals on Twitch. Check it out and enter your snake now. Plus there's lots of challenges to compete in if you don't want to get too competitive against others.

This one above is a challenge to avoid yourself for 100+ turns.

If you'd like to program along with me, check out my beginner friendly stream where I setup my snake. Or you can join me for a live session when I'm next on Twitch.

17