04-21-2011, 02:33 PM
Bitmaps and Sprites
Learn how to make your game more visually appealing and exciting with scripted Sprites.
Creating a Sprite
Sprite is a class built into RGSS. It displays a bitmap on the screen. This is how you create a sprite that is displayed on the screen:
What I did here is to create the sprite. I then create a bitmap for the sprite specifying that it is 640 pixels wide and 480 pixels high. Alternatively, I can make the bitmap load a picture instead.
It will learn the dimensions of the picture so this makes your job easier.
Position of the sprite
The position of the sprite is handled using x and y as well as a z variable. The z variables determines where in the queue of visual elements it will be drawn. Far behind or in front. We set position this way:
If you leave them unassigned they will all be assigned the value of 0 making the sprite appear on the top left of the screen and down. This is usually a wanted position to have it in.
Some additional properties for the Sprite class are:
Color
Color is important in bitmaps. Every pixel in a bitmap is made up of four different values. Red, Green, Blue and alpha. The alpha channel is opacity. The rest is obvious.
RGSS has a built in Color class. You create a new Color object like this.
255 is the max value, while 0 is the lowest possible.
Setting pixels
In order to draw a pixel on a bitmap you use the .set_pixel method of the Bitmap object.
The above will make the top left pixel of the bitmap black.
Drawing rectangles
Sometimes we would rather want to draw a rectangle spanning over many pixels than just single pixels. It can be done very easily:
The above will make the entire screen black.
Using loops to create dynamic effects
You can read about for loops in my other tutorial. They can be very handy if you want to create gradients for example:
This will draw a quite nice gradient on the bitmap. The rect starts one pixel lower for each iteration as the i will go up one every time. The color changes as all the channels change to the value of i times two every iteration.
More may be coming soon. :) Hope it helped. I appreciate replies if you liked it. :)
Learn how to make your game more visually appealing and exciting with scripted Sprites.
Creating a Sprite
Sprite is a class built into RGSS. It displays a bitmap on the screen. This is how you create a sprite that is displayed on the screen:
Code:
@sprite = Sprite.new
@sprite.bitmap = Bitmap.new(640, 480)
Code:
@sprite = Sprite.new
@sprite.bitmap = Bitmap.new("Graphics/Pictures/picture.png")
Position of the sprite
The position of the sprite is handled using x and y as well as a z variable. The z variables determines where in the queue of visual elements it will be drawn. Far behind or in front. We set position this way:
Code:
@sprite.x = 50
@sprite.y = 60
@sprite.z = 250
Some additional properties for the Sprite class are:
Code:
@sprite.angle = 360
@sprite.opacity = 255
@sprite.visible = true
@sprite.zoom_x = 50
@sprite.zoom_y = 40
@sprite.mirror = false
@sprite.tone = Tone.new(0, 0, 0, 20)
Color
Color is important in bitmaps. Every pixel in a bitmap is made up of four different values. Red, Green, Blue and alpha. The alpha channel is opacity. The rest is obvious.
RGSS has a built in Color class. You create a new Color object like this.
Code:
@black = Color.new(0, 0, 0, 255)
# Or you can leave out the alpha channel which will make it 0% transparent
@black = Color.new(0, 0, 0)
@white = Color.new(255,255,255)
@red = Color.new(255, 0, 0)
Setting pixels
In order to draw a pixel on a bitmap you use the .set_pixel method of the Bitmap object.
Code:
@sprite.bitmap.set_pixel(x, y, color)
# Or if we insert the values
@sprite.bitmap.set_pixel(0, 0, Color.new(0, 0, 0))
Drawing rectangles
Sometimes we would rather want to draw a rectangle spanning over many pixels than just single pixels. It can be done very easily:
Code:
@sprite.bitmap.fill_rect(x, y, width, height, color)
# Or with inserted values
@sprite.bitmap.fill_rect(0, 0, 640, 480, Color.new(0, 0, 0))
Using loops to create dynamic effects
You can read about for loops in my other tutorial. They can be very handy if you want to create gradients for example:
Code:
for i in 1..100
c = Color.new(i*2, i*2, i*2)
@sprite.bitmap.fill_rect(0, i, 640, 1, c)
end
More may be coming soon. :) Hope it helped. I appreciate replies if you liked it. :)
Valdred
Tech Administrator of Save-Point
Tech Administrator of Save-Point