Home > RGSS Script Reference > Window_PlayTime
Window_PlayTime
Inherits from: Window_Base
Description: This window displays the player's play time in the main menu.
class Window_PlayTime < Window_Base
# ------------------------------------
def initialize
super(0, 0, 160, 96)
self.contents = Bitmap.new(width - 32, height - 32)
refresh
end
# ------------------------------------
def refresh
self.contents.clear
self.contents.font.color = system_color
self.contents.draw_text(4, 0, 120, 32, "Play Time")
@total_sec = Graphics.frame_count / Graphics.frame_rate
hour = @total_sec / 60 / 60
min = @total_sec / 60 % 60
sec = @total_sec % 60
text = sprintf("%02d:%02d:%02d", hour, min, sec)
self.contents.font.color = normal_color
self.contents.draw_text(4, 32, 120, 32, text, 2)
end
# ------------------------------------
def update
super
if Graphics.frame_count / Graphics.frame_rate != @total_sec
refresh
end
end
end
|
Total_Sec: The total amount of seconds the player has been playing.
Initialize
Arguments: None
Local Variables: None
How it Works: This method initializes the window by setting the width of the window to 160, and the height of the window to 96. Note that the y coordinates of 0 is a dummy value. The y coordinate is changed by the Scene_Menu object. The window is then refreshed.
Refresh
Arguments: None
Local Variables: None
How it Works: This method draws the window's contents. The first thing the method does is clear the window's contents. The words "Play Time" are drawn in the system color at x coordinate 4 and y coordinate 0. Next, the value of @total_sec is set to the value of Graphics.frame_count (the total number of frames shown since the beginning of the game) divided by the current frame rate. The next three statements divide the value of @total_sec into hours, minutes, and seconds. The number of hours played is the total number of seconds divided by 3600. The number of minutes played is the total number of seconds divided by 60, then modded by 60. Finally, the seconds to be shown is the total number of seconds modded by 60. Once these three values are computed, the text = sprintf("%02d:%02d:%02d", hour, min, sec) statement formats the three values into a string, with each of the three values seperated by the ":" character. The next two statements print the formatted string at x coordinate 4 and y coordinate 32.
Update
Arguments: None
Local Variables: None
How it Works: This method updates the window. It first calls the superclass method in order to perform updating common to all windows. If the value of Graphics.frame_count (the total number of frames shown since the beginning of the game) divided by the number of frames per second is not equal to the value of @total_sec, then the window is refreshed to reflect the proper play time. Note that because of this, drastic changes in the frame rate will cause the play time shown to be incorrect.
|
|