banner



How To Change The Background Color For Ruby Gosu

In this tutorial, I am going to show yous how to apply Gosu 2D Ruby Game evolution library. I assume you have some feel with coding in blood-red programming language. If y'all are not comfortable with loops, classes and functions then I suggest you lot to take a few hours to learn carmine programming language. As red is very piece of cake to learn, you lot will pick up the concepts pretty quickly and information technology won't take much time to continue with this tutorial.

What You Can Do With Gosu

Gosu is a 2nd game development library for the Cherry and C++. It is not a complete gaming engine but Information technology gets the job done with well-nigh of information technology's features. There are few other extension libraries that you tin use with Gosu like chipmunk, chingu, texplay and ashton for extended features.

There are few other libraries which are useful for the deployment of the game and for other use.

Installing Gosu and Ruby-red

Before we start building games, you need to install Gosu and Cherry on your calculator. Depending on the type of the operating system you lot have, the instructions will vary. I am going to quickly indicate y'all towards the installation instructions of three pop operating systems.

Windows Instructions

In club to build ruby-red based games on Windows, you demand to utilise Visual Studio C++ Express (or Professional person) edition IDE on your calculator. Past installing visual studio, compiling the C++ and Ruby source files becomes easy.

Commencement by installing Ruby on your computer. Go to ruby lang and download Windows installer. After the installation, you need to install 'rubygems' which makes information technology easier for you to download other development files. In one case you lot install rubygems then installing gosu is very easy.

gem install gosu

If all goes well, You lot're ready to programme.

Mac Os X Instructions

Install latest version of ruby on your Mac. After installing cherry-red, you demand to download the rubygems package. Afterwards that just run the post-obit command.

precious stone install gosu

This will install gosu on your system and y'all're ready to get.

Linux Instructions

Gosu depends on multiple development libraries to work on Linux. Your organisation needs the following files before installing gosu:

build-essential
freeglut3-dev
libfreeimage-dev
libgl1-mesa-dev
libopenal-dev
libpango1.0-dev
libsdl-mixer1.2-dev
libsdl-ttf2.0-dev
libsndfile-dev
libxinerama-dev

Once you lot install these files y'all tin Install ruby i.9.x and ruddy gems packages.

ruby1.nine.1-dev

rubygems

As there are many distros out there, each with their own installation method. You tin download these files from the respective repository using Software Center or download via control line by invoking control line installation tool similar apt-get or yum.

eastward.g.

sudo apt-get install build-essential freeglut3-dev libfreeimage-dev libgl1-mesa-dev libopenal-dev libpango1.0-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsndfile-dev libxinerama-dev

sudo apt-go install ruby1.9.1-dev rubygems

After installation of these deveopment packages y'all can install gosu.

jewel install gosu

Please check the official site of Gosu If there are any updated set of dependencies. Afterwards installation, you are set to execute gosu based ruby programs on your linux organisation.

Additional Libraries

Releasy : If yous wish to distribute game on windows and Mac as native executable and so you need to install releasy. Install the gem using post-obit command:

gem install releasy

Texplay : Texplay lets y'all manipulate images in gosu. Information technology is a C extension that is perfect for fast pixel-perfect collision detection in games, and fast image manipulation. By using this library you tin get the pixel and draw the shapes during the runtime (similar circle, line boxes etc). Install releasy gem using post-obit control:

gem install texplay

Chingu: In one case yous become comfortable with the gosu library then yous tin can employ chingu for additional game logic and other core framework features. Y'all tin install chingu precious stone using this command:

gem install chingu

There is also some other altarnative library rmagick, like shooting fish in a barrel to use, only much harder to distribute compared to texplay. Yous tin download rmagick using following command:

jewel install rmagick

Chipmunk Physics Library: You can extend your gosu game physics using the chipmunk library. This gem can exist installed using the control:

precious stone install chipmunk

ruby-opengl : You can utilise the standard opengl for the 3D furnishings and few other effects with gosu. You can install the gem using following command:

gem install ruby-opengl

There are few other libraries that you tin can install that volition help extend the game you design using gosu. Keep tab on gosu forums for additional updates on the new or existing libraries.

Executing Ruby Programs

Open command prompt (or terminal for linux users), navigate to the directory where you lot have stored the source files and then type the following command.

ruby filename.rb

filename.rb is the source file that you lot want to execute. You can supercede the name of the file with your own file proper name. Make sure yous are typing this control in the directory is stored, otherwise ruby volition flag an fault. As well do notation the file name correctly while typing considering incorrect file proper name also returns the error.

Nosotros are going to cover some of the basics of Gosu library. At the end of this affiliate, you will exist able to write graphical programs using gosu library.

If you want quick Video tutorial, please check out the video beneath.

Let'south outset start by creating an empty window with Gosu.

Code:

require 'rubygems' require 'gosu'  class DemoWindow < Gosu::Window  def initialize     super(640, 400, false)       end end  DemoWindow.new.prove        

Code Explanation:

First two lines initialize rubygems and gosu library in guild. In third line, we accept a class called DemoWindow form that inherits from the Gosu::Window class. It creates a new window of size 640 x 400, and runs the main loop. That is all information technology does for now.

Once you run this lawmaking, It should testify you lot the window of 640×400 without title.

gosu ruby line draw

We are going to add the caption to the window so that you can run into the name of the game in window title.

Let's add the caption in our program.

crave 'rubygems' crave 'gosu'  class DemoWindow < Gosu::Window  def initialize     super(640, 400, false)     self.caption = "Gosu Demo Window"  end   def update   end      def draw   cease  cease  DemoWindow.new.show        

Cheque the output. It should evidence you Window caption of "Gosu Demo Window".

Lawmaking explanation:

In this program we take added update() and draw() methods. These 2 methods are overrides of Gosu::Window's member functions. These two methods are very important considering update() method should incorporate your game logic and move. The update() method is called threescore times per 2nd in social club to process the game logic.

In draw() method, you should go along the code that redraws the objects and background. Information technology should non contain the logic of your game. Whenever the program requires to redraw the background, information technology calls for describe() method. So the depict() method is e'er in the loop to either fetch the input or to redraw the game background, unless program calls for close().

When the program is executed, it invokes the show() method which triggers the input events and then moves to update() method and processes the game logic. After that information technology goes for the draw() method and then exits through close() method if the call is fabricated for that.

Make changes to this program. Write your own caption. You can also alter the window size. Make sure yous cheque display size combination, If you want to experiment the size of the game window.

Bones Shapes

Now that we have managed to create a simple window, let'southward meet how to describe some basic shapes. Nosotros have three methods to draw the basic shapes.

draw_quad()
draw_triangle()
draw_line()

Permit's accept a look at these methods one past one. But kickoff a small note on the co-ordinate values. We are creating shapes with the help of 10, Y and Z co-ordinate values. For example, when it comes to cartoon the line, we fill the colour of line starting from X to X1 and Y to Y1, which makes upwards a line on our screen. In case of triangle, we need 3 X values for the position of triangle and 3 values for Y. And similarly we have to take iv values of X and Y for quadrangle.

In gild to make this more than clear, we will write a simple lawmaking for each shape.

draw_quad() method

Code:

require 'rubygems' require 'gosu'  class DemoWindow < Gosu::Window     def initialize     super(640, 400, faux)     self.caption = "Demo of draw_quad()"   end    def draw     x = 300     y = 200     size = 120     draw_quad(x-size, y-size, 0xffff8888, 10+size, y-size, 0xffffffff, ten-size, y+size, 0xffffffff, 10+size, y+size, 0xffffffff, 0)   end   end  DemoWindow.new.show        

gosu draw_quad() ruby

Code Caption: Equally yous tin see, we have written the draw_quad() method inside the depict() method. We accept specified the X and Y co-ordinate values and substracted and added information technology with the size. This way we are giving the method the values it needs to draw the quadrangle. We have also specified the color values to make full the quadrangle. You can specify the Z=0 or exit that part out. I accept kept it purposefully in club to explicate you about the Z value.

draw_triangle() Method

require 'rubygems' require 'gosu'  class DemoWindow < Gosu::Window     def initialize     super(640, 400, false)   end    def draw     x = 300     y = 200     size = 120     draw_triangle(ten-size, y-size, 0xff00ff00, x+size, y-size, 0xff00ff00, x-size, y+size, 0xff00ff00)   terminate   end  DemoWindow.new.prove        

Code Caption: The draw_triangle() method is fetching the values for X and Y, using the ten-size and y-size and ten+size. These 3 values makes up the triangle. Just pass the specific values for X and Y and you can get the triangle every bit per your values.

draw_line() Method:

require 'rubygems' require 'gosu'  class DemoWindow < Gosu::Window     def initialize     super(640, 400, fake)   end    def draw     x = 300     y = 200     size = 120     draw_line(x-size, y-size, 0xff00ffff, x+size, y+size, 0xff00ffff)   end   stop  DemoWindow.new.show        

Code Caption: In this code we have passed two 10 and ii Y co-ordinate values, along with color code. That is all this method needs to depict the line.

Background Color

Equally per the current update of the libgosu, there seems to exist no congenital-in method for the background and you lot take the only 2 options in this case.

Apply epitome to fill up the screen (This applies to almost of the games which relies on graphic images for the background).
Utilise quadrangle to fill the screen and pass -Bladder::INFINITY for z to make certain it is drawn below everything.

That's all for at present. From here you tin can become ahead and learn more virtually making games with Gosu. I accept tried to cover some basic concepts, I hope you have some practiced foundation from which you can develop further games. If you accept whatsoever questions or suggestions, feel free to let me know about it in comments. You can also shout @maheshkale on twitter.

Source: https://onecore.net/gosu-ruby-2d-game-development.htm

Posted by: stephensgoolifter.blogspot.com

0 Response to "How To Change The Background Color For Ruby Gosu"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel