DEV Community

Play Button Pause Button
Michelle Mannering
Michelle Mannering

Posted on • Updated on

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.

Battlesnake game

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

Get started with Battlesnake and Python

Battlesnake Python Starter Project

An official Battlesnake template written in Python. Get started at play.battlesnake.com.

Battlesnake Logo

This project is a great starting point for anyone wanting to program their first Battlesnake in Python. It can be run locally or easily deployed to a cloud provider of your choosing. See the Battlesnake API Docs for more detail.

Run on Replit

Technologies Used

This project uses Python 3 and Flask. It also comes with an optional Dockerfile to help with deployment.

Run Your Battlesnake

Install dependencies using pip

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

Start your Battlesnake

python main.py
Enter fullscreen mode Exit fullscreen mode

You should see the following output once it is running

Running your Battlesnake at http://0.0.0.0:8000
 * Serving Flask app 'My Battlesnake'
 * Debug mode: off
Enter fullscreen mode Exit fullscreen mode

Open localhost:8000 in your browser and you should see

{"apiversion":"1","author":"","color":"#888888","head":"
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.

Sneaking into Gold

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.

Avoid self win

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.

Top comments (2)

Collapse
 
star_trooper profile image
Atharva Shirdhankar

Hey I m at 142 rank in BattleSnake summer league.
But how can I move ahead?
I need help!!

Collapse
 
kingsszx profile image
KINGSSZX

do you know any thing about react
i really need help. with it now i am having a serious challenge with it now