![]() As we start the game, the score is set to 0, the iteration of the frames is set to 0, and we place a new food. The initial direction is set to Right, the axis of the head is calculated, and the snake is created as a list. The reset function starts or restarts the game. Self.head = Point(self.w / 2, self.h / 2) The reset function will restart the game after each collision. Three dots at the end shows where the next part of code will be. Now we begin to create our game with the SnakeGameAI class. We set the colors, the size of the blocks or the spaces this window will contain, and the speed which can be changed as preferred. Using namedtuple, we create the dictionary to define x and y axis of an object. We initialize the pygame, set the font varibale and the directions. The font file is a separate file, it can be downloaded from the GitHub link in the reference. We are using Pygame library for this project. First, we import some necessary libraries and define some constants. We are also adding functions for placing the food, detecting collisions, taking the next step, and moving into that direction. In this file, we are using one class to define all the functions needed for the graphics. ![]() Run the agent.py file to run the whole project. Finally, we present the progress in a chart with the plotResult file. The model file contains the Q-learning model we are using for this project. The agent file trains the snake to perform and connects the functions of other files. It includes the snake, the food, the window, and the boundaries. The main file contains the creation of th graphics. The project is divided into four scripts. That is how the snake learns to play the game on its own using RL. The snake gets punished every time it hits any boundaries or itself, which means if any collision occurs. The reward is received every time the snake eats the food. Our snake will get a +10 as a reward and a -10 as a punishment. The goal is to receive the maximum reward. This equation calculates the next maximum reward based on a certain action at each state. This algorithm is a value based algorithm and it updates the value based on the Bellman equation. Using different algorithms, an agent is trained to complete a task without any human interaction.įor this project, we are using Q-learning algorithm. Reinforcement learning is a machine learning method where an agent learns to act in an environment depending on the rewards and punishments it gets. It means the agent/snake learns to move on it's own, avoid the collisions, and eat the food. In this article, we will explain a codebase where the classic Snake Game is developed using Reinforcement Learning.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |