If you load a clock sprite in, then load a minute hand and an hour hand in as 3 different Bitmap/Sprite combos, like so:
Spoiler
class Spriteset_Clock def initialize @back_sprite = Sprite.new @min_hand = Sprite.new @hour_hand = Sprite.new @back_sprite.bitmap = Cache.picture("Clock.png") @hour_hand.bitmap = Cache.picture("ClockHourHand.png") @min_hand.bitmap = Cache.picture("ClockMinuteHand.png")
Then you have them on screen. All you have to figure out now is how to rotate them. You can do this with their Sprite#angle property. The #angle property is an integer from 0 to 360, or a full circle in integer-based degrees. Thus, we need a way to calculate our angle from the time of day. I think it'd be something like:
Spoiler
calc_xy anchor_to_center end def calc_xy @sprite_hour.x = @sprite_back.x + (@sprite_back.width/2) @sprite_hour.y = @sprite_back.y + (@sprite_back.height/2) @sprite_min.x = @sprite_hour.x @sprite_min.y = @sprite_hour.y end def anchor_to_center @sprite_hour.oy = @sprite_hour.height/2 @sprite_min.oy = @sprite_min.height/2 end
After that you gotta figure out the updating of the clock. Because you want to supply your own time standard and not the default one, and to make this system much more dynamic, we will allow the user to supply hour and minutes to the update function.
We also have to calculate the angle of the hands:
Spoiler
def update(hour, min) @sprite_hour.angle = hour_angle(hour, min).floor @sprite_min.angle = minute_angle(hour, min).floor end def hour_angle(hour, min) 180 - ((0.5 * (60.0 * hour + min)) + 90) end def minute_angle(hour, min) 180 - ((6 * min) + 90) end
The hour_angle method is simply calculating an angle from the hour and min variables, then adding 90 (because my hands are at a 90 degree angle already, aka pointing right). Then to get it to rotate clockwise instead of counter clockwise, you must subtract your calculated angle from 180. Same with minute_angle.
Finally, we define the rest of our class, then initialize it:
Spoiler
def dispose @sprite_back.dispose @sprite_hour.dispose @sprite_min.dispose endendtest = Spriteset_Clock.newhour = 12min = 1count = 60loop do if (count -= 1) <= 0 count = 60 min += 1 if min >= 60 min = 1 hour += 1 if hour == 13 hour = 1 end end end Graphics.update Input.update test.update(hour, min) break if Input.trigger?🙂C)endtest.dispose
The final code is in
this rar file. Just unzip it, and copy the two folders into a new project. (Images are in there, so you can see how it works.)