Follow on Google News News By Tag Industry News News By Location Country(s) Industry News
Follow on Google News | Gamedev on Ruby? Why not! Snake Game just in few steps!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/ 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_ require file end Dir[File.expand_ 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( end end describe "#create_board" let(:gameboard){ it "returns an array" do expect( end it "returns an array with given size" do expect( expect( end it "returns board full of . symbols" do expect( 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_ end def center [board.length/ end def print_text(text) char_center = text.length/ i = 0 text.chars.each do |char| board[ i+=1 end end def create_board @board = Array.new(length){ end end Please read the continuation of the article in blog: http://blog.diatomenterprises.com/ End
Account Email Address Account Phone Number Disclaimer Report Abuse
|
|