Not logged inGosu Forums
Forum back to Help Search Register Login
Up Topic Gosu / Gosu Showcase / Space shooter (sample game for Gosu)
- By benko Date 2009-05-05 13:58

I've made a simple shooter games that shows how to handle sprites and game states (ie, screens) with Ruby/Gosu. I read in these forums a long time agao one message asking for help regarding game states, so I think this could be useful for beginners at game development.

The source code is stored at GitHub:

Some snapshots:

- By jlnr (dev) Date 2009-05-07 10:43
One thing I wonder, is it possible that Array#each will die because you are deleting objects in its iteration block, or is there a protection? Not that it happened while playing, but I'm curious because I remember having this problem. Oh, and you can shoot while dead ;)

The game states & transitions are very clean, I think I'm going to link this from the end of the tutorial. State management is really underdocumented right now :)
- By benko Date 2009-05-07 17:49

You were right about both bugs. I totally forgot that the array "shifts" its elements when you do a delete and that messes the array.each loop. Now the code for updating the sprite lists looks like this:

# Updates all the sprites in the collection
def update
  @sprites.each_value do |list|
        list.each {|x| x.update}
        list.reject!{|x| x.die?} # delete all doomed sprites

I think it should work now properly, since the sprites don't delete themselves from their own sprite lists until the full sprite list (ie array) has been looped. And now you cannot kill bad guys while you're dead ;)

PS: Thanks a lot for your hard work with Gosu, it's really an amazing lib!
- By banister Date 2009-07-21 02:16
not sure if this is relevant but when i want to delete while i iterate i use the delete_if iterator
Up Topic Gosu / Gosu Showcase / Space shooter (sample game for Gosu)

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill