Gamedev on Ruby? Why not! Snake Game just in few steps!

 
RIGA, Latvia - Sept. 19, 2016 - PRLog -- This article isn't about big games with sophisticated graphics, nor will I be creating something like MMORPG (even though, I think it's possible). In this article, I will be creating a terminal Snake Game.

I know a lot of developers; including myself, who got started in programming, because they were crazy about games. They wanted to create something similar to what they had played before, but in the end, for some reason they are doing programming that has nothing to do with gamedev. So in this article I will be going though the steps to create a simple game in Ruby language, which  most of us use for various web-applications.

Preparations
I prefer to use ruby 2.3.1 with structured code and folders. For the Snake Game, I recommend the following folder structure:
ruby_snake/
 -lib/
   |- Files that will create game mechanics
 -spec/
   |- spec_helper.rb
   |- Test files
 Gemfile
 start.rb

The Gemfile contains a few gems: RSpec (http://rspec.info/) for tests and Pry (https://github.com/pry/pry) for debugging our application. Gemfile:
source 'https://rubygems.org'
gem 'rspec'
gem 'pry'

For rspec, setup a spec_helper.rb. This file will contain each lib file and configure RSpec for our needs. spec/spec_helper.rb should be included in each test.
Dir[File.expand_path('../lib/*.rb', File.dirname(__FILE__))].each do |file|
 require file
end
Dir[File.expand_path('../lib/errors/*.rb', File.dirname(__FILE__))].each do |file|
 require file
end
RSpec.configure do |config|
 # Use color in STDOUT
 config.color = true

 # Use color not only in STDOUT but also in pagers and files
 config.tty = true

 # Use the specified formatter
 config.formatter = :documentation # :progress, :html, :textmate
end

The structure of the game
Can you imagine all the game components? It should look something like this:
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . x x x x . o .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .

Here you can see the Board, the Snake and the Food.
In addition, you will need something that will connect all these things together - The Game.
Let's look closely at these components.

The Board
The Board has a width and a length, in our sample the width is 9 and the length is 9.
  1 2 3 4 5 6 7 8 9
1 . . . . . . . . .
2 . . . . . . . . .
3 . . . . . . . . .
4 . . . . . . . . .
5 . . . . . . . . .
6 . . . . . . . . .
7 . . . . . . . . .
8 . . . . . . . . .
9 . . . . . . . . .

So, let's write some tests that will cover our Board.
require 'spec_helper'
describe Board do
 describe "#new" do
   it "initializes gameboard with board" do
     expect(Board.new(40,40).board).not_to be_nil
   end
 end
 describe "#create_board" do
   let(:gameboard){Board.new(40,40)}
   it "returns an array" do
     expect(gameboard.board).to be_instance_of(Array)
   end
   it "returns an array with given size" do
     expect(gameboard.board.size).to be_eql(40)
     expect(gameboard.board[0].size).to be_eql(40)
   end
   it "returns board full of . symbols" do
     expect(gameboard.board.first.first).to eql('.')
   end
 end
end

And specifically this will create our Board and its methods:
class Board
 attr_reader :length, :width, :board

 def initialize(width, length)
   @length = length
   @width = width
   create_board
 end

 def center
   [board.length/2, board.first.length/2]
 end

 def print_text(text)
   char_center = text.length/2
   i = 0
   text.chars.each do |char|
     board[center.first][center.last - char_center + i] = char
     i+=1
   end
 end

 def create_board
   @board = Array.new(length){ Array.new(width, '.') }
 end

end

Please read the continuation of the article in blog: http://blog.diatomenterprises.com/ruby-snake/

Contact
Aleksandr Kovtunov, Diatom Enterprises
***@diatomenterprises.com
End
Source: » Follow
Email:***@diatomenterprises.com Email Verified
Tags:IT, Software, Application
Industry:Technology
Location:riga - riga - Latvia
Subject:Surveys
Account Email Address Verified     Account Phone Number Verified     Disclaimer     Report Abuse
Diatom Enterprises News
Trending
Most Viewed
Daily News



Like PRLog?
9K2K1K
Click to Share