Factors to Consider before choosing a Matrimonial site for Marriage

Remember that your spouse will be your companion through every stage of your life — ups and downs, joys and sorrows, sickness, success and failure. So, if you don’t want to spend the rest of your…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The Classic Snake Game Recreated With Ruby

Being a 90s kid and experiencing the recent launch of Disney Plus has left me feeling nothing short of nostalgic. Thinking back on the times, I quickly remembered the best phone game of my childhood: the Classic Snake Game.

I found a YouTube tutorial that recreates the Classic Snake Game using Ruby. The original game is played by maneuvering a snake around the screen. Food in the form of a ball appears randomly on the screen. As the snake collects the food, the snake grows in length. This increase in length makes it more difficult to control the snake because the snake is not allowed to touch its body. The game is over when the snake crashes into itself. The goal is to make the snake as long as possible before the game is over. For this Ruby version, the snake can go through the walls. The arrow keys on the keyboard control the snake.

Ruby 2D is the gem required for this tutorial. Ruby 2D is a gem that makes 2D applications in Ruby. You can create applications, games, and visualizations easily with just a few lines of code. When you require ‘ruby2d’, it adds the Ruby 2D domain-specific language and classes. Then a new window object is created for you by calling Ruby2D::Window.new. The show method called at the end of the program tells Ruby 2D to show the window.

You can reference the window class directly, to see its attributes:

We set the grid size to be 20. Now every time we draw something, we will have 20 pixels. Our grid is 32 by 24:

So this results in:

Width: 640 / 20 = 32

Height: 480 / 20 = 24

The snake is going to be created as a few boxes on the screen. First, we must create a Snake Class. The snake will be initialized with 4 starting positions corresponding to where we want the boxes to appear when we start the program. For this, we create an array for @positions. The positions will hold each of the squares that the snake currently occupies. Inside the @positions array, there are 4 sets of arrays with x and y coordinates for each of the 4 starting positions. The starting direction is also specified as “down”. Our snake is a straight line consisting of 4 boxes, going from the top to the bottom.

Next, we will draw a square for each of these positions by making a method called “draw”. We will iterate through each position, and for each position our program will draw a new square. The x will be the value of the element at index 0 multiplied by the grid size, which is 20. The y will be the value of the element at index 1 multiplied by the grid size. Then, we set the size of each square equal to the grid size minus 1. The reason we minus 1 pixel is so that a line appears before each new box. Otherwise, the snake would be solid all throughout. We then set the color to white.

The “move” method will update the initial positions. To make the snake move, we must add a new square at the head of the snake, and remove the square at the tail of the snake.

For the tail:

@positions.shift will remove the first element in the positions array, which is the first set of coordinates. We must also use the update loop here.

The update loop: The update loop is an infinite loop that runs 60 times per second. The Window manages the update loop. We can enter this loop by using the update method. We must put “clear” in the update method so that the tail of the snake clears. Otherwise it will just draw on top of the boxes that are there already.

For the head:

First, make a method to return the head of the snake:

Then, @positions.push will add the new coordinates of the head to the @positions array.

The next problem is that we want to prevent the snake going backwards in any direction. For example, when going down, the snake should not be able to go up. The way to fix this is:

In our case, it would look like this:

The next step is to generate the food ball. We will make a Game Class for this. The game class will be initialized with a score of 0, randomly generated x and y coordinates for the ball, and a variable to check if the game is finished.

Then, we create a “draw” method that generates a square for the ball, and a message to display texts to the user.

Now the score is in the top left, and when the game is over, the “Game Over” message will be displayed.

The next step is to detect when the snake reaches the ball. Then we must increment the score by 1 point and make our snake grow.

In the Snake Class, add these:

We want to compare the position of the head of snake with the position of the randomly generated ball. In the game class, add this:

Then, in the update loop, add:

To make the snake grow, change the “move” method of the snake class to this:

The snake was initialized with @growing = false. @positions.shift will remove the first element in the positions array, which is the first set of coordinates. That is the tail. Here, we are telling the program to not remove the tail if we are currently growing. Then, at the end of the “move” method, we make @growing equal to false each time the snake moves.

Now we need to detect when the snake crashes into itself. To do this, we can use the ruby .uniq method to compare the length of the @positions array of the snake body with the length of calling the .uniq method on it. If they are equal, then the snake did not crash into itself. If they are not equal, then the snake is occupying a set of coordinates twice. This means the snake crashed into itself, and the game is over.

In the snake class, add:

Change the update loop to look like this:

In the game class, add:

The Snake Game is now complete! A possible additional feature would be to increase the speed of the snake as the snake collects the food. Currently, the speed is the same speed throughout the game.

If you’re looking for a more detailed guide to recreating the Snake Game and reliving all the glory of the late 90s/early 2000s era, check out this youtube tutorial that helped me get started:

Add a comment

Related posts:

MY ENTERPRENAURSHIP PROJECT

In my training of foster flagship training programe my fourth project was enterprenaurship task.So we made a whatsapp group in which we planned our enterprenaurship task.we decided that we sell…

What influences you to use a technology?

I personally think being labelled as a .NET developer your entire career is really limiting and speaks volumes about your unwillingness to experiment and learn new things. Don’t get me wrong .NET is…

Impacts of Social Media on Youth

Social Media is one of the major things in everyone's life. Especially There are a lot of impacts of social media on youth. There are many factors affecting the young stars. Like social media is one…