Git Product home page Git Product logo

first-snake's Introduction

A simple Battlesnake written in Python.

This is a basic implementation of the Battlesnake API that moves randomly around the board. It's a great starting point for anyone wanting to program their first Battlesnake using Python. It comes ready to deploy to Heroku, and you can use other cloud providers if you'd like.

Technologies

This Battlesnake uses Python 3.7, CherryPy, and Heroku.

Prerequisites

Deploying Your First Battlesnake

  1. Fork this repo into your own GitHub Account.

  2. Clone your forked repo to your local environment.

    git clone [email protected]:[YOUR-GITHUB-USERNAME]/starter-snake-python.git
  3. Create a new Heroku app.

    heroku create [YOUR-APP-NAME]
  4. Deploy your code to Heroku.

    git push heroku master
  5. Open your new Heroku app in your browser.

    heroku open

    You should see the following text:

    Your Battlesnake is alive!
    

Optionally, you can view your server logs using the Heroku logs command.

heroku logs --tail

(the --tail option will show a live feed of your logs in real-time)

At this point your Battlesnake is live and ready to enter games!

Registering Your Battlesnake and Creating Your First Game

  1. Log in to play.battlesnake.com.

  2. Create a new Battlesnake.

    Give your Battlesnake a name, and complete the form using the URL for your Heroku app.

  3. Once your Battlesnake has been created, you can create a new game and add your Battlesnake to it.

    Type your Battlesnake's name into the search field and click 'Add' to add it to the game. Then click 'Create Game' to start the game running.

  4. You should now see a brand new Battlesnake game with your Battlesnake in it! Yay! Press Play to start the game and watch how your Battlesnake does.

Repeat step 3 & 4 every time you want to start a new game and see how your snake behaves. It's common for Battlesnake developers to repeat these steps often as they make their Battlesnake smarter.

By default, your Battlesnake will move randomly around the board. See server.py for details on how it achieves that.

Optionally, open your Heroku logs while the game is running to see your Battlesnake receiving API calls and responding with its moves. shell heroku logs --tail

At this point you should have a registered Battlesnake and be able to create games!

Customizing Your Battlesnake

Now you're ready to start making your Battlesnake yours.

Changing Appearance

When the game engine tells your Battlesnake that a new game is starting, you can optionally reply with information about how you want your Battlesnake to look.

Locate the start function inside server.py. You should see a line that looks like this:

return {"color": "#888888", "headType": "regular", "tailType": "regular"}

See Customizing Your Battlesnake for how to customize your Battlesnake's appearance using these values.

Changing Behavior

On every turn of each game your Battlesnake receives information about the game board and has to respond with its next move.

Locate the move function inside server.py. You should see code that looks like this:

data = cherrypy.request.json
# Choose a random direction to move in
possible_moves = ["up", "down", "left", "right"]
move = random.choice(possible_moves)
return {"move": move}

Your Battlesnake can move one of four directions: "up", "down", "left", or "right". By default your Battlesnake chooses a direction randomly. Your goal is to read board information from data and make an intelligent decision on where your Battlesnake should move next.

See the Battlesnake Rules for more information on playing the game and moving around the board and developing your Battlesnake.

Updating Your Battlesnake

After making changes, commit them using git and deploy your changes to Heroku.

git add .
git commit -m "update my battlesnake's appearance"
git push heroku master

Once heroku has updated you can create a new game with your Battlesnake to view your latest changes in action.

At this point you should feel comfortable making changes to your code and deploying those changes to Heroku!

Developing Your Battlesnake Further

Now you have everything you need to start making your Battlesnake smarter. Here are a few more helpful tips...

  • Keeping your logs open in a second window (using heroku logs --tail) is helpful for watching activity and debugging any problems with your Battlesnake.

  • You can use the Python print() function to output information to your server logs. This is very useful for viewing what information is available in each command, and debugging any logic in your code during Battlesnake games.

  • Review the Battlesnake API Docs to learn what information is provided with each command. You can also output the data to your logs:

    def move(self):
        data = cherrypy.request.json
        print(data)
        return {"move": "up"}
  • When viewing a Battlesnake game, you can pause playback and move forward/backward one frame at a time. If you review your logs at the same time, you can see what decision your Battlesnake made on each turn.

Joining a Battlesnake Arena

Once you've made your Battlesnake behave and survive on its own, you can enter it into the Global Battlesnake Arena to see how it performs against other Battlesnakes.

Arenas will regularly create new games and rank Battlesnakes based on their results. They're a good way to get regular feedback on how well your Battlesnake is performing, and a fun way to track your progress as you develop your algorithm.

(Optional) Running Your Battlesnake Locally

Eventually you might want to run your Battlesnake server locally for faster debugging and code testing. You can do this by installing Python 3.7 and running server.py from the command line: shell python server.py

Note: You cannot create games on play.battlesnake.com using a locally running Battlesnake unless you install a tunnelling tool like ngrok.


Questions?

All documentation is available at docs.battlesnake.com, including detailed Guides, API References, and Tips.

You can also join the Battlesnake Developer Community on Slack. We have a growing community of Battlesnake Developers of all skill levels wanting to help everyone succeed and have fun with Battlesnake :)

first-snake's People

Contributors

chyggen avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.