Summary of Ruby Experience

Hello gals and guys!! Welcome back to the last post of my ruby blog. It has been a wonderful journey to discover and learn a completely new programming language all on my own. Even though it was not of my own accord to the learn a new language, but was a required assignment for my computer science class, I undoubtedly enjoy the whole process from the beginning to the end.

On the first day of class when my professor had told his students to choose a language to learn by themselves, I thought it was a completely insane idea. Learning a programming language from an instructor was already difficult so how would I be able to learn all on my own. As always, the cliche of not judging a book by its cover could be applied to my case. I looked at some blog posts on current popular programming languages and decided that my top three choices would be Python, Ruby or Pascal. Luckily, I was assigned Ruby, a language that I especially enjoy now because it is dynamically typed. I love the idea that a variable can become anything depending on its assignment.

During the first week of research on Ruby, I was actually enjoying the process because I was learning a language at my own pace. I could closely inspect and find what I needed and advance from there. There were so many cool features that I didn’t know about: the ternary operator, hashes, destructuring assignment, method missing, flip-flop operator and many more. Some of these features were especially helpful for my Final Project. I had decided to program a game using the Gosu library that is compatible with the Ruby language. A tough, but wonderful part of the experience.

My proudest achievement would be the post before this because I had completed a game (something that I personally picked and wanted to code). I was glad to accomplished something that I had set out to do. Learning the basics and more had given me the confidence to become a better programmer. Not only can I program in Java and C#, I am now familiar with Ruby and also PHP and Python (the blogs that I had to follow). In the end, all the research, effort, and time put into these blogs were well worth it.

I would like to end with a quote to summarize my whole experience:

“There is no end to education. It is not that you read a book, pass an examination, and finish with education. The whole of life, from the moment you are born to the moment you die, is a process of learning.” – Jiddu Krishnamurti

Thank you to everyone that has read and liked my posts. I appreciate you all for coming along with me on this wonderful journey 🙂

 

Advertisements

“Catch the Rainbows” Part 3

Welcome back gals and guys!! For my last portion of the program, I will need to find a game over image to display when the unicorn runs out of lives. For this particular program I have chosen the image below:

gameover.jpg

I will store the image along with the other images in the hash for organization purpose (Line 16). 

I have also found an angry fire image to act as a distraction obstacle that will randomly spawn throughout the game window to confuse the player. This will also be included in the hash (Line 17). 

fire.png

Next, I added a trumpet.wav file into the hash even though it is not an image, therefore, the syntax will be different. Instead of Gosu:: Image, it has to be Gosu:: Sample (Line 15).

I will need a new instance variable called lives which will be set to 5 at the beginning of the game (Line 24). 

Then, three additional instances: firex, firey, and number are needed to spawn the fire icon. Initially, firex and firey will be randomized according to the game window size (Lines 27-29).

Screen Shot 2018-04-22 at 12.03.03 AM.png

I created a new method called distraction_object that will be called in the update method to randomize the fire icon. Everytime update runs, the number variable will be added by 1. When it reaches 30, firex and firey will be randomized again and the number variable will be reset to 0. This is to guarantee that the randomizing speed isn’t too fast. Remember, the update method is called once every update_interval milliseconds (Lines 56-63).

Refer to Documentation if needed: Gosu

Then, add the created distraction_object method to the update method (Line 66).

Screen Shot 2018-04-22 at 12.13.59 AM.png

Next, I have to add more code to the update method to manipulate the lives variable. Inside the else statement of the update method, I have to ensure that if the rainbowy variable surpasses 550 then the lives variable will be subtracted by one. Once the lives variable is zero  (compare lives == 0), all variables related to the rainbow and unicorn will be set to 0 to end the game (Lines 77-82).

The final step would be to draw the fire icon and the lives text to the game window. Inside the draw method, we add the fire image and assign firex and firey to its x and y location (Line 91).

Then add the lives text to the upper right-hand corner of the window (Line 93).

In order to draw the game over screen, I have included an if statement to compare the lives variable to zero and then I can draw the necessary game over screen and also the score text to the upper-left corner of the game window for the player to see (Lines 94-97).

Screen Shot 2018-04-22 at 12.21.51 AM.png

Test the code: http://rextester.com/NNSDK51707

Game Output:

Screen Shot 2018-04-22 at 12.45.56 AM.png

Screen Shot 2018-04-22 at 12.43.42 AM.png

Screen Shot 2018-04-22 at 12.46.46 AM.png

Screen Shot 2018-04-22 at 12.49.36 AM.png

Screen Shot 2018-04-22 at 12.47.18 AM.png

That will be the wrap up of our Final Project!! Thank you, everyone, for tuning in to the completion of “Catch the Rainbows” 🙂

“Catch the Rainbows” Part 2

Hello gals and guys!!! Welcome back to the developing process of “Catch the Rainbows”. The next step in the game is to add a falling rainbow from the sky in random x-locations.

First, I would simply change the static location of the rainbowx variable to a random location within the width of the game window. I have decided that the range would be 100 to 800 (line 17). 

Next, I have added four new variables: y_velocity, change, score, and font. y_velocity will be the falling speed of the rainbow. The change variable will keep track of how fast the rainbow will fall. The score will keep track of the number of rainbows the unicorn catches.  The font is necessary to draw text on the game window (Lines 19-21). 

I created a velocity_rainbow method that can be called later in the update method to have the rainbow fall from the set position 0 (rainbowy variable). Interval is a local variable, that takes a default float of Gosu called update_interval (16.66666 – the interval between frames in milliseconds) divided by 1000. Then y_velocity will keep on adding change (which is set to 200) multiply by the interval. Lastly, the rainbowy variable will keep on adding y_velocity multiply by the interval. The calculations are done based on my preference. (Lines 33-37).

Screen Shot 2018-04-14 at 11.31.47 PM.png

Then, I added an update method (According to Gosu, “this method is called once every update_interval milliseconds while the window is being shown”). I then called the velocity_rainbow method in the update to have the rainbow move. An if condition is needed to have points added to the score. If the distance between the four parameters, the x and y locations of both objects: rainbow and unicorn are less than 30 then score will add 1 and the rainbow is reset to fall from the sky again. The change variable will add 10 to itself every time the if statement occurs in order to speed up the rainbow as more points are obtained. Else, I call the spawn_more method if the rainbow and unicorn are not in the proximity-indicated by the if statement. (Lines 49-60).

Screen Shot 2018-04-15 at 12.19.21 AM.png

Next, I created a spawn_more method if the rainbowy location is greater than 558 (the height of the game window). The rainbowy variable will be reset to -50 to give the player more time to react. y_velocity will also be reset to 0 and rainbowx variable will pick another random location from 100 to 800 (Lines 41-47).  

The final step would be to draw the text onto the game window. We call the font variable and assign the word: Score and the score variable. Twenty and twenty will be the x and y coordinates. Finally, we assign the color black to the font. (text, x, y, z, rel_x, rel_y, color) (Line 67). 

Parameters according to Gosu:

  • rel_x (Float) — the horizontal alignment.
  • rel_y (Float) — the vertical alignment.
  • text (String)
  • x (Number) — the X coordinate
  • y (Number) — the Y coordinate
  • z (Number) — the Z-order

Screen Shot 2018-04-14 at 11.43.04 PM.png

Test the code: http://rextester.com/DEK9523

Screen Shot 2018-04-15 at 12.22.25 AM.png

Screen Shot 2018-04-15 at 12.20.07 AM.png

Screen Shot 2018-04-15 at 12.23.21 AM.png

Screen Shot 2018-04-15 at 12.24.25 AM.png

I tried to add a video of the recorded game, but it was not possible because I do not have the premium plan to support it 😦 That’s the end of part 2 of “Catch the Rainbows.” Tune in to next week’s completion of the game!!!!!!!

 

 

 

“Catch the Rainbows” Part 1

Welcome back gals and guys to my final project!!!! First of all, all images were found on google and edited using the Magic Wand Tool:

Unicorn

unic.png

Rainbow

rainbow.png

Background

clouds.png

The Atom Editor has a plugin tool called atom-image-resize package. Images above were resized to my preference.

Next, I created a GameWindow, a subclass of the Gosu::Window in order to call other events: button_down, draw, etc (Line 6).

The initialize constructor can take any number of arguments and runs when GameWindow.new is created (Line 7, 36).

Super is the default behavior of Gosu::Window (Line 8).

Images can be stored separately in a variable, but I currently have a hash to keep all the images organized. Create an instance hash called @images then add in the background, uni and rainbow. Each of the images is being overloaded with (window, source, tileable) parameters (Lines 11-13).

The self.caption allows for a title to appear on the title bar. Any name can be used, but since I have called the game, “Catch the Rainbows”, that is what I will be using (Line 9). 

I have created x and y instance variables for both the unicorn and rainbow images to manipulate later on (Lines 15-17).

Button down is a method that is called before the update method when a button is pressed (Line 20).

In my case, I have decided to use the case when statements when different keys are pressed. For the escape button, the window gets close. For the left arrow, the unicorn’s x position will be subtracted by 35. For the right arrow, the unicorn’s x position will be added by 35. Ternary operators are used to stop the unicorn once it reaches the edge of the GameWindow (Refresher on the ternary operator can be found on a previous blog). (Lines 21-24).

The draw method is necessary for the images to appear in the game window and gets called after every update (gets called 60 frames per second). Take a key in the hash, then add .draw and the dimensions (x, y, z). For the background, (0,0,0) is sufficient. For the unicorn, pass in its @x variable for movement and a static y location. For the rainbow, pass in its @rainbowx and @rainbowy variables. (Lines 28-32).

To get the window to appear. After the dimensions are set, window.show is needed (Lines 36-37).

Screen Shot 2018-04-08 at 2.14.32 AM.png

The prerequisites for the last post is needed before the file can be run in the Terminal.

Code File: http://rextester.com/HEYZIJ96723

Screen Shot 2018-04-08 at 3.30.49 AM.png

Output: GameWindow 

Screen Shot 2018-04-08 at 2.13.17 AM.png

 

That is all for part 1 of “Catch the Rainbows”!! Tune in to the next blog to see more changes 🙂

2D Game with Ruby and Gosu

Welcome back to the Ruby blog!!! From this point onward, I will begin a final project to showcase the Ruby skills that I have learned throughout the past weeks. I will be implementing a game using Ruby and Gosu (a game development library that is compatible with both Ruby and C++). Since I have never worked with Gosu before, it will be a challenging experience to create my first game using Ruby, but that is the fun part. Helpful information for Gosu is included below:

Documentation: http://www.rubydoc.info/github/gosu/gosu/frames


Description of the Game:

The game will be called “Catch the Rainbows.” There will be a unicorn that the player can move back and forth horizontally and the goal is to catch the falling rainbows from the sky. When the unicorn comes into contact with a rainbow, a point will be added to the score. The game is over when the unicorn misses a rainbow. I will be implementing the game on a MacOS using the Atom Editor and grabbing the Gosu library (or gem) to work with.


Prerequisite Steps To Implementation:

For MacOS

1.) Visit: https://www.libgosu.org/ruby.html

2.) Install Homebrew in Terminal: https://brew.sh

3.) Type in: brew install sdl2 

Screen Shot 2018-04-03 at 10.54.40 PM.png

4.) Install the Gosu gem in Terminal (type the words): gem install gosu

Screen Shot 2018-04-03 at 10.55.25 PM.png

5.) Open Atom Text Editor, create a Gemfile,  type in: source ‘https://rubygems.org’ and gem ‘gosu’ and then bundle install into project’s root inside the Terminal.

(Note: to get to the project’s root, use the “cd /” command and to exit use “cd -“)

Screen Shot 2018-04-03 at 10.57.52 PM.png

Atom Editor:

Screen Shot 2018-04-03 at 10.46.14 PM.png

Terminal:

Screen Shot 2018-04-03 at 10.48.26 PM.png

6.) Create a game folder, add in a file called “unicorn.rb” or whatever name the programmer prefers.

7.) In the first line of “unicorn.rb” file, type in: require ‘Gosu’ and the setup is complete!!!!

Screen Shot 2018-04-03 at 10.50.46 PM.png

For Windows

1.) Visit: https://www.libgosu.org/ruby.html

2.) Install Gosu through RubyGems and type in: gem install gosu and the setup is complete!!


Game Plan: 

The game will be completed in the next 3 blogs, following the general outline below:

First blog: Find and edit all the images related to the game, implement these images into the game window, and finally allow the unicorn to move back and forth horizontally. Furthermore, pressing the escape button will exit the game regardless of the state the game is in (A shortcut button to exit).

Second blog: Randomize the rainbows that are falling from the top of the screen. Add a score tracker at the upper left-hand corner of the game screen. The score will initially be zero until the unicorn catches a rainbow. A point is added each time the unicorn comes into contact with the rainbow.

Third blog: The velocity of the rainbows will increase slowly as the score increases. There will be 5 lives added to the upper right-hand corner, a life will be subtracted if the player does not catch a falling rainbow.  Once lives reach 0, a ‘Game over’ screen with the total score will appear. Each time the unicorn catches a rainbow, a trumpet sound will be played. Furthermore, there will be a randomized angry fire icon that will spawn throughout the game window to confuse the player.

That will be the general approach. Let us meet again in the next blog!!!

Interesting Ruby Features Part 2

Welcome back gals and guys to the Ruby blog. We will be exploring more interesting features of our language!!!

Destructuring Assignment

A way to break down an array or hash into individual items.

“Destructuring allows you to bind a set of variables to a corresponding set of values anywhere that you can normally bind a value to a single variable” – Lisp 

Simple examples of destructuring in destruct.rb file:

1.) Obtain the first three items in the numbers array (Lines 4, 6).

2.) Obtain the first item, in-between elements, and last item in numbers array (Lines 9, 11-13).

3.) Ignore certain elements in the array (Lines 16, 18-20).

Screen Shot 2018-03-25 at 8.49.14 PM.png

Output:

Screen Shot 2018-03-25 at 8.49.28 PM.png

Test the code: http://rextester.com/ZUHSB5083

Splat Operator

Splat Operator has two functions depending on its assignment. The first is known as “slurp” or “collect”. This function takes a variable number of arguments and adds them into an array. The operator is usually used in methods that take a range of inputs for a parameter. The second function is known as “split”. A way to convert an array into a list of arguments. This function is used to call a method with the arguments of an array.

Destruct Block Assignment

A Ruby programmer can destruct inside a block argument. Anything inside an array [ ] can be destructed. Thus, one can obtain the first element in the 3 subarrays of the triples array (Lines 17-18).

Examples of splat operator and destructuring of a block in splat.rb file:

Screen Shot 2018-03-25 at 9.03.31 PM.png

Output:

Screen Shot 2018-03-25 at 9.03.38 PM.png

Test the code: http://rextester.com/UTKF42380

Destruct Hashes

We have explored ways to destruct an array, but hashes can be destructed too.

A Ruby Hash contains a method known as values_at. The method takes a list of keys and returns the values for the keys in the same order (Line 2).

Destruct of destructuring is also possible (Lines 11-13).

Splat operator will work with hashes (Lines 16-17).

Example of destructuring of hashes in hashes.rb file:

Screen Shot 2018-03-25 at 9.46.50 PM.png

Output:

Screen Shot 2018-03-25 at 9.46.59 PM.png

Test the code: http://rextester.com/VDU87979

 

More destructuring examples for Ruby: https://github.com/LendingHome/destruct

 

That’s the end of Ruby’s unique features!!!

Interesting Ruby Features Part 1

Hello gals and guys, welcome back to the weekly Ruby blog. We will be exploring interesting features or hidden gems in the Ruby language!!!

1.) Method Missing

An object can call a method by searching through the method directory (methods that were written) for the same name. Once the object cannot find a particular method, there will be a NoMethodError exception. Though a programmer in Ruby is able to provide a “method missing” to prevent such an error. This method (method_missing) will pass a variable for a non-existent method, an array of arguments that were initially passed, and a block that was initially passed to the missing method. The second and third parameter could be empty if a method was called without any arguments, but are there if they were to be used or passed to other methods.

Note: The * is the splat operator. In a method, it specifies a variable length argument list. All arguments passed to a method will be placed into an array called args.

A more complex example of method missing can be found here:

https://code.tutsplus.com/tutorials/ruby-for-newbies-missing-methods–net-20924

Example of Method Missing in methodMissing.rb file:

Screen Shot 2018-03-17 at 11.50.28 PM.png

Output:

Screen Shot 2018-03-17 at 11.40.41 PM.png

Test the code: http://rextester.com/GVERX43863

2.) Flip Flop Operator

A way to execute a portion of a loop. The flip-flop range operator compares two conditions inside of a loop. Starting with the first condition, everything will be true until the second condition. Before the first condition or after the second condition, everything else will be false.

Note: << operator apprehends values to end of an array. In our case, we apprehend the value that is true in the colors array to the empty array called myColors.

Example of flip-flop operator in flipflop.rb file:

Screen Shot 2018-03-18 at 12.30.26 AM.png

Output:

Screen Shot 2018-03-18 at 12.30.34 AM.png

Test the code: http://rextester.com/RQYAKJ22223

 

3.) Almost everything is an object

Classes, instances, and primitive types (fixnum) are objects. Examples of objects include number, string, array, hash, etc.  While methods, operators, control structures (conditionals and loops) and blocks are not objects but can be wrapped into objects, which is called Proc. Ruby can take a block of code, wrap it up in an object, then store it in a variable or pass it into a method, and finally run the code as many times as the programmer desire.

Example of objects and proc in object.rb file:

Screen Shot 2018-03-18 at 2.02.25 AM.png

Output:

Screen Shot 2018-03-18 at 1.51.55 AM.png

Test the code: http://rextester.com/FCZA62083

 

That’s the end of Ruby’s unique feature!!!