# Ruby/Sdl, beginner problem, making sprites

**URL:** <https://www.ruby-forum.com/t/ruby-sdl-beginner-problem-making-sprites/160561>\
**Category:** Ruby\
**Created:** [February 17, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/ruby-sdl-beginner-problem-making-sprites/160561 "2009-02-17T18:46:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabikku](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a5b964/32.png) [@sabikku](https://www.ruby-forum.com/u/sabikku)\
**Post date:** [February 17, 2009, 6:46pm UTC](https://www.ruby-forum.com/t/ruby-sdl-beginner-problem-making-sprites/160561/1 "2009-02-17T18:46:20Z")

</div>

Hi. I’m beginner in programming, and also in ruby.

I have installed Ruby/Sdl  
([Ruby/SDL Reference Manual](http://www.kmc.gr.jp/~ohai/rubysdl_ref_2.en.html#label-126)) and I have  
a problem.

I tried to make a screen and surface, and it didn’t worked. I’m looking  
for answer for Two days, so now I’m asking here…

My code:

#--------------  
#--------------  
#--------------  
require ‘sdl’  
class Sprite  
attr\_accessor :x  
attr\_accessor :y  
attr\_accessor :alpha  
attr\_accessor :image  
def initialize(path,x=0,y=0,alpha=256)  
# Jeden X odpowiada 1/640 szerokosci ekranu, jakikolwiek szeroki by  
nie byÅ‚.  
@x = ($screen.w/640)\*x  
# Jeden Y odpowiada 1/480 szerokosci ekranu, jakikolwiek wysoki by  
nie byÅ‚.  
@y = ($screen.h/480)\*y  
@alpha = alpha

```
@image = SDL::Surface.load_bmp(path.to_s)
@image.set_alpha(SDL::SRCALPHA,@alpha)
@image.set_color_key( SDL::SRCCOLORKEY ,0)
@image = @image.display_format
$sprites << self

update

```

end

def update  
$screen.put(@image,@x,@y)  
end

end

SDL.init( SDL::INIT\_VIDEO )  
$screen = SDL::Screen.open(640,480,16,SDL::SWSURFACE)  
$sprites = []

$char = Sprite.new(“tilek.bmp”,320,240)

loop do  
while event = SDL::Event.poll  
case event  
when SDL::Event::Quit  
exit  
when SDL::Event::KeyDown  
$char.x += 32  
$char.y += 2  
end  
end  
background\_color = $screen.format.map\_rgb(0,0,0)  
$screen.fillRect(0,0,640,480,background\_color)  
for sprite in $sprites  
sprite.update  
end

# inny update, do obslugi 2-warstwowych ramÃ³w (???) czy czegoÅ› takiego

$screen.flip

# update caÅ‚ego okna

$screen.update\_rect(0,0,$screen.w,$screen.h)  
end

# EnD…

# --------------

# --------------

Ok… When I’m looking at the samples in Ruby/Sdl files, there are  
modules and everything that looks very very similar to that what I did  
above. But Mine is not working ^^".

Can anyone help?
