Giter Club home page Giter Club logo

tetrisai.py's Introduction

TetrisAI.py

Disclaimer:

The Project is still not finished. It is work in progress

How to use:

  • Install requirements: pip install -r requirements. txt
  • Run main.py: python3 main.py

This Python script is designed to train a Convolutional Neural Network (CNN) to play the game of Tetris on a NES emulator. The script uses the TensorFlow library to build and train the model, and the PyAutoGUI library to interact with the game.

The script begins by importing the necessary libraries and creating an empty list for the training data (X_train) and the labels (y_train).

The model is then defined using TensorFlow's Keras API. It's a sequential model with two convolutional layers (each followed by a max pooling layer), a flattening layer, and two dense layers. The input shape of the first layer is set to match the dimensions of the game screen (240x256 pixels, with 3 color channels), and the output layer has 7 neurons, corresponding to the 7 possible actions (up, down, left, right, and three buttons on the NES controller).

The script then enters a loop where it captures screenshots of the game screen and processes them into a format suitable for the neural network. It also checks which key is currently being pressed and assigns a label to the current game state based on this. This data is then added to the training set.

Once the training data has been collected, it's converted into a numpy array and used to train the model. The model is trained using the Adam optimizer and the sparse categorical crossentropy loss function, which is suitable for multi-class classification problems like this one.

After training, the model's weights are saved to a file so that they can be loaded later without having to retrain the model.

The script then starts the game and enters a loop where it continuously captures the current game state, uses the trained model to predict the best action to take, and then performs this action. This loop continues until the game is over.

The range class provided is a built-in Python class and is not directly used in the main script. It's a sequence type that represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.

Screenshot

tetrisai.py's People

Contributors

leonw1112 avatar

Watchers

 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.