Hi!
I've just started playing around in Ruby, and I have started to make a small space-shooter game, andI was hoping someone here could help me with a problem I have.
The way my code is now I have two event2.poll: one for shooting (sensing mouse click) and the other sensing any key to close (will be changed later to just ESC), and for some reason since I added the second even2.poll the first one only works every other mouse click.
Here's my code:
Code:
include Math
SDL.init( SDL::INIT_AUDIO|SDL::INIT_VIDEO )
screen = SDL::setVideoMode(640,480,16,SDL::FULLSCREEN)
head = SDL::Surface.loadBMP("head.bmp")
head.setColorKey( SDL::SRCCOLORKEY , head[0,0])
$head = head.displayFormat
$cMaph = $head.makeCollisionMap
SDL::Mouse.hide
class Ship
def initialize
@ar = []
@m_x = 0
@m_y = 0
end
def move
@ar[0, 1] = SDL::Mouse.state
@m_x = @ar[0]
@m_y = @ar[1]
[@m_x, @m_y]
end
def draw(screen)
SDL.transformBlit($head,screen,270,1,1,$head.w/2,$head.h/2,@ar[0],@ar[1],0 )
end
end
class Shot
def initialize(m_x, m_y)
@s_x = m_x
@s_y = m_y
end
def move
@s_y -= 1
@s_y
end
def draw(screen)
SDL.transformBlit($head,screen,270,1,1,$head.w/2,$head.h/2,@s_x,@s_y,0 )
end
end
class Enemy
def initialize
@e_x = srand(550) + 50
@e_y = 0
end
end
event = SDL::Event.new
ship1 = Ship.new
shot = []
s_count = 0
while true
screen.fillRect(0,0,640,480,0)
m_x, m_y = ship1.move
ship1.draw(screen)
while event2 = SDL::Event2.poll
case event2
when SDL::Event2::MouseButtonDown
for shoot in shot << Shot.new(m_x, m_y)
end
end
end
shot.each {|shoot|
s_y = shoot.move
shoot.draw(screen)
s_count += 1
if s_y < 0 - $head.h/2
shot.delete_at(s_count - 1)
end
}
s_count = 0
screen.updateRect(0,0,0,0)
while event = SDL::Event2.poll
case event
when SDL::Event2::KeyDown, SDL::Event2::Quit
exit
end
end
"if event.poll != 0 then
if event.type==SDL::Event::QUIT then
break
end
if event.type==SDL::Event::KEYDOWN then
exit if event.keySym==SDL::Key::ESCAPE
end
end"
end
Thanks for any help.