Coder Social home page Coder Social logo

snake-1's Introduction

Snake

This is an implementation of 2D Snake using pygame. The game is being used to to implement an artificial neural network for AI learning.

Gameplay

The player controls a "snake", indicated by a colored square, with the arrow keys, moving around a grid. Colliding with itself or a wall results in a game over, while colliding with "food", yellow circles, picks it up.

The objective of the game is to collect "food" items that respawn randomly in the grid when the previous one is collected. Player score will increase by a constant (default: 10) number for each "food" item collected. When the snake collects food, its length will increase by one, making movement progressively more difficult.

Implementation Details

The game is laid out in a very OOP fashion.

  • constants.py, effectively a configuration file, stored constants and values derived from them.

  • A lightweight main class, Snake.py, implemented the animation cycle using a finite state machine to delegate behavior to additional state classes.

  • A separate class, States.py, defined the behavior of those different states that would be processed by the main class.

  • Finally, Player, Food, and Board classes defined relevant variables and the necessary methods to operate on them. Board also contained static methods to perform basic grid operations, such as collision checking and equality checks.

constants.py

This file holds game constants and values derived from them that determine layout and behavior details.

This allows for easy tweaking of parameters and avoids using "magic numbers". This also allows for potential implementation of dynamic resizing.

Snake.py

This file contains the runner main() method and the StateManager class.

The StateManager class points to the current state and also assigns itself as a field to that state. This reference allows for the current state to be changed by the state classes themselves.

main() follows the animation cycle and delegates behavior to one of several States, which encapsulate all the information for the current state of the game.

States.py

This file holds the states that determine actual game behavior. An interface, State, contains update(), render(), and handle_event() methods.

At each tick, update() updates all variables and game internals. render() displays necessary information to the screen, and handle_event() listens for keypresses that would change the state.

Artificial Neural Network

PyBrain is an existing Python library that provides the ANN functionality. I will interface my game to PyBrain functions and also attempt to code my own simple implementation for comparison.

In Progress

Menu with player color selection
Generalize game to Tron
Dynamic resizing and maximizing window

snake-1's People

Contributors

aeonstasis avatar

Watchers

James Cloos avatar  avatar  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.