Hi everyone.
I just discovered Gosu (ruby), and the Tutorial was really what gave me will to try it. With the Tutorial done, I was wondering where to go from there : the exchanges & showcases forums are full of information and projects, but I did find the step between the tutorial and those a bit steep.
I thought that some other "beginner level" info should come handy for me, and probably for others too, so (after asking Julian), I decided to open a topic on this subject : feel free to contribute here any code snippet, basic tutorial extension, simple project which could be used as learning material for others, as long as you can provide some kind of explanation (and not only raw code - the tutorial was, again, a good example for me, allowing me the instant gratification of something that works, while actually making me learn some basic skills which could be useful as I delve more into Gosu).
To try to contribute something myself, I did extends the Tutorial to include a (computer controlled) second player, making some kind of "adversary" to the player, as both race to be the first to score 500 points.
The modified Ruby source are attached to this post, I'll comment them a bit here :
Star class
- not modified
GameWindows class
- Modified to include a second player. For easier code, the two players are stored in an array @players, so that the update and draw methods can simply call the needed methods on @players.each. To be able to differentiate the players visually, they start at different position and are colored one in red, the other in blue.
- A method get_winner returns the actual winner (a player scoring 500 point or more), if any. The method will be check with each loop to determinate if the game end. If it happens, the game notify each player that the game is finished, preventing further move (and scoring).
Player class
- Has been spitted in one base class (Player), with the behavior common to both human and computer players. The subclass differs only by their input (the player uses the keyboard, the computer some -very- basic 'AI').
- HumanPlayer class has the code for keyboard input, moved from the initial GameWindow class, in a method get_action, giving to the system the players "choices". They are then applied in the move method.
- ComputerPlayer class use a very basic logic : it identify the closest star (get_closest_star), using an accessor on the GameWindow, then turn on its direction (adjusting its angle to face it), and accelerate, except if very close.
Disclaimer :
- I have used Gosu less than 3 hours and Ruby less than 30 so the code is probably wildly suboptimal. Feel free to convey advices or remarks, I'm here to learn
- English is not my first language, so please forgive any misuse.
Thanks to Julian & Gosu team for the framework, I hope that this could help someone, and that it will encourage other to post their own examples !
Martin
Good job!
Just had a quick look and made a few minor changes.
ComputerPlayer
There isn't much point assigning the variable then doing a check if the array is empty, you get this for free in Ruby. So I would add
'return if @window.stars.empty?'
def get_closest_star
return if @window.stars.empty?
closest_star = @window.stars.first
@window.stars.each do |star|
if(Gosu::distance(@x,@y,star.x,star.y) < Gosu::distance(@x,@y,closest_star.x,closest_star.y))
closest_star = star
end
end
closest_star
end
I would also suggest that it really isn't the ComputerPlayer classes job to find the closest star. I would put this method into the star class and give the players coordinates and have it return the closest.
I would also return early with the get_actions method as above.
I also made a little change the GameWindow get_winner and rather then checking 'if winner != nil then', I changed it to 'unless winner.nil? then'. This is more of a rubyish preference but I don't tend to add return on the last line, as ruby does this anyway.
Hope this helps in some way.
It did, thanks ! All those "ruby-isms" are good thing to learn (I got a java background, but I want to code in a manner appropriate to the language).
Martin
Loading...