By jlnr (dev)
Date 2014-01-09 23:04
I am not sure if this line is always true or always false, but it is certainly not doing what it's supposed to :)
if @y >= @y -= 20 then
I wish I had time to explain the CptnRuby example, I think it's a pretty good example of simple collision detection. I have written a whole unpublished game with a friend in its style :)
Not exactly collision related, but somewhat on-topic...
If you ever want to graduate to proper gravity simulation (for jumping) without learning a physics engine (like Chipmunk), you can't beat Hugo Elias' page about modeling physical phenomena:
http://freespace.virgin.net/hugo.elias/models/m_main.htmIt's not much more involved than your method, but the results are far more realistic. Here's a quick'n'dirty implementation to get you started:
https://gist.github.com/runnerpack/8344513Note the @maxjumps variable. Set it to something greater than one to allow jumping in mid-air (once you collect the right power-up, of course ;)
@jlnr: it's always true, and it always subtracts 20 from @y :)
Ok, I'm gonna give you some tips on code organization and style. I'll try to come back to the actual content of your code tomorrow, as it's pretty late where I am right now, and I should be getting to bed soon. Just some stuff I wish I had learned sooner. Maybe you already know this stuff, and you're just rushing the code a bit to prototype fast.
As an organizational tip, I personally recommend copying Gosu's convention and giving each object you create #update and #draw methods. This will make it clear what phase of the game calculation you are in. I recommend never getting mixed up between what is rendering / display logic and what is world state logic. You seem to be confusing the two a lot in your code, which can be dangerous if you're not careful.
In a similar train of thought, separate files for separate classes can be helpful, as you can easily jump to different classes by switching files.
Keeping everything in one file is sometimes really great for prototyping though.
You're creating a bunch of variables in Window#initialize that all start with @star. That's a pretty good indication that you should be creating a new class called Star.
For one-liners with if statements, I think that
run_this if condition
makes for cleaner code, as opposed to the
if condition then run_this
style you're currently using. It's just a matter of taste though, as both do the same thing.
But the style I propose only works for one-liners, while the style you're currently using works for multi-liners as well, so it's more versatile.
You don't seem to understand what attr_reader / attr_accessor does, as you used it in one place, and not in others. I think that's a question that has already been answered better in other places on the internet, so I'll let you figure that out yourself.
One last note:
Don't be afraid to run your code after each small step of implementing something new. That will allow you to see what works and what doesn't. It looks like you wrote this code all in one go, without running it. I say this because you've defined a method #button_down(id) inside of Window#update. I'm not sure if that's even valid ruby syntax.
Hey,
thank you very much for your answers :) They are very detailed and I really appreciate it, that you really take your time to write that ;)
What do you mean by giving each object update and draw methods? Do you mean
class Player
def initialize
end
def draw
end
def update
end
Do you have any good Tutorial or example that I can find how I can use rendering / display logic and world state logic better?
I will try to use more files, but the problem is, that I recently found out how to do so (because load 'script.rb' doesen't really work).
Yea, I got very confused. I mixed up some variables, because I meant to make some Stars but I decided to stop that and I started making Traps ... But I forgot to rename the variables
In my oppinion using if condition then run_this is good. But I will try to make one line if statements in the format run_this if condition (Because I didn't know that that works) ;)
I know what they are used for but I think I didn't really get what they do... I will try to learn that ;)
I always run my code to assure that it runs correctly. I know that a method within a method isn't really good, but where should I put the def button_down(id); end method then? I thought that it has to get updated all the time?
Misc:
The problem with my programs is, that I always to things much harder than they should be and that often ends up in a wrong syntax. I also don't always have the time to learn Ruby / Gosu and that's why I often try things out. The problem about that is, that I may get a working program but it probably is really complicated and has wrong syntax. But I cannot learn all the time, because I have to go to school. But I'm going to search some ruby programs and take a look at them so that I know how to use the syntax etc :)
Marvin
By lol_o2
Date 2014-01-10 20:35
Your Player class structure is good, but maybe update should be before draw as it is called before.
Put the
def button_down(id)
into your Window class.
You can learn some Ruby/Gosu by looking at the games on Showcase forum. But their syntax isn't always perfect.
I'm making some 'framework', which manages game logic in Gosu and provides various useful features. You can find it
<here>. But it lacks some things, like examples and I'm going to make a major update soon.