| 赞 | 0  | 
 
| VIP | 8 | 
 
| 好人卡 | 27 | 
 
| 积分 | 66 | 
 
| 经验 | 41413 | 
 
| 最后登录 | 2012-10-21 | 
 
| 在线时间 | 833 小时 | 
 
 
 
 
 
Lv4.逐梦者 弓箭手?剑兰 
	- 梦石
 - 0 
 
        - 星屑
 - 6559 
 
        - 在线时间
 - 833 小时
 
        - 注册时间
 - 2010-11-17
 
        - 帖子
 - 1140
 
 
 
 | 
	
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员  
 
x
 
 本帖最后由 一箭烂YiJL 于 2010-12-11 15:21 编辑  
 
课序号:4 
作者:一箭烂YiJL 
出版社:VX教程出版社 
售价:完全免费 
VX教程出版社的其它教程 
 
上一课: 
新手教程——函数外的常量(理论篇) 
 
 
再看这一课之前请看: 
新手教程——函数外的常量(理论篇) 
新手教程——计算累计时间Window 
 
 
序: 
这一课时联系着上一课的函数外的常量, 
此课是实战。我们要开刀的是上一次的新手教程——计算累计时间Window 
 
 
正式开始: 
还记得上一次我介绍了两种大方法吗? 
这一次我会两种都用上的。 
 
 
先看看脚本:- class WindowPlayTime < Window_Base
 
 -   
 
 -   def initialize(x, y, w = 160, h = 90)
 
 -     super(x, y, w, h)
 
 -     update
 
 -   end
 
  
-   def update
 
 -     self.contents.clear
 
 -     @total_sec = Graphics.frame_count / Graphics.frame_rate
 
 -     hrs = @total_sec / 60 / 60
 
 -     min = @total_sec / 60 % 60
 
 -     sec = @total_sec % 60
 
 -     playtime = sprintf("%02d:%02d:%02d", hrs, min, sec)
 
 -     self.contents.font.color = normal_color
 
 -     self.contents.draw_text(x, 30, 126, WLH, playtime, 2)
 
 -     time_text = "游戏时间:"
 
 -     self.contents.draw_text(x, -30, 160, 90, time_text,0)
 
 -   end
 
 - end
 
  复制代码 先想一想有什么我们可以抽出来的: 
w、h、time_text、时分秒之间的冒号":" 
有人会问:怎么不把xy一并抽出来? 
xy是待留用@a = WindowPlayTime.new(1, 2)    # xy坐标 
 
 
 
A.类内常量的方法: 
 
先把wh的160和90抽出来,名字统一为WH一个常量 
并且把initialize的那里的w和h删掉:- class WindowPlayTime < Window_Base
 
 -   WH = [160, 90]
 
 -   def initialize(x, y)
 
 -     super(x, y, WL[0], WL[1])    #记得数组是从0开始数起
 
 -     update
 
 -   end
 
 
  复制代码 再把time_text的"游戏时间:"抽出来:- class WindowPlayTime < Window_Base
 
 -   WH = [160, 90]     # 这里用的是字串式
 
 -   Text = "游戏时间:"
 
 
  复制代码 然后在原版的17行做手脚,变成:最后,把时分秒之间的冒号改变, 
在类中(以及函数外)添加这种哈希式: 
- Colon = {时=>":", "分"=>":", "秒"=>":"}
 
 
  复制代码 再在原版的14行动手脚,变成: 
- playtime = sprintf("%02d#{Colon["时"]}%02d#{Colon["分"]}%02d#{Colon["秒"]}", hrs, min, sec)
 
 
  复制代码 等等!为什么要用#{}括着Colon["时"]? 
因为在""里用变量是要用#{}来括住。 
 
若有看过上两课的同学则会知道上边在做什么。 
 
制成品最后那里有 
 
 
 
B.module模块类的方法: 
 
开一个module,把上面的所有常量塞进去: 
- module TimeSet
 
 -   WH = [160, 90]
 
 -   Text = "游戏时间:"
 
 -   Colon = {"时"=>"时", "分"=>"分", "秒"=>"秒"}
 
 - end
 
 
  复制代码 然后在class的第一行加进:这样TimeSet的函数否会全部加进来。 
 
修改initialize成为: 
- def initialize(x, y)
 
 -   super(x, y, WL[0], WL[1])
 
 -   update
 
 - end
 
 
  复制代码 之后下一步就是update了, 
update我们用"TimeSet::常量"这种方法, 
修改原版的17行,成为:- time_text = TimeSet::Text
 
  复制代码 再修改原版的14行,成为: 
- playtime = sprintf("%02d#{TimeSet::Colon["时"]}%02d#{TimeSet::Colon["分"]}%02d#{TimeSet::Colon["秒"]}", hrs, min, sec)
 
 
  复制代码 还有大功告成! 
 
 
C.完成作品在这儿: 
 
 
一.类内常量的方法:- class WindowPlayTime < Window_Base
 
 -   WH = [160, 90]
 
 -   Text = "游戏时间:"
 
 -   Colon = { "时" => "时", "分" => "分", "秒" => "秒"}
 
 -   def initialize(x, y)
 
 -     super(x, y, WH[0], WH[1])
 
 -     update
 
 -   end
 
  
-   def update
 
 -     self.contents.clear
 
 -     @total_sec = Graphics.frame_count / Graphics.frame_rate
 
 -     hrs = @total_sec / 60 / 60
 
 -     min = @total_sec / 60 % 60
 
 -     sec = @total_sec % 60
 
 -     playtime = sprintf("%02d#{Colon["时"]}%02d#{Colon["分"]}%02d#{Colon["秒"]}", hrs, min, sec)
 
 -     self.contents.font.color = normal_color
 
 -     self.contents.draw_text(x, 30, 126, WLH, playtime, 2)
 
 -     time_text = Text
 
 -     self.contents.draw_text(x, -30, 160, 90, time_text,0)
 
 -   end
 
 - end
 
 
  复制代码 二.module模块类的方法:- module TimeSet
 
 -   WH = [160, 90]
 
 -   Text = "游戏时间:"
 
 -   Colon = { "时" => "时", "分" => "分", "秒" => "秒"}
 
 - end
 
  
- class WindowPlayTime < Window_Base
 
 -   include  TimeSet
 
 -   def initialize(x, y)
 
 -     super(x, y, WH[0], WH[1])
 
 -     update
 
 -   end
 
  
-   def update
 
 -     self.contents.clear
 
 -     @total_sec = Graphics.frame_count / Graphics.frame_rate
 
 -     hrs = @total_sec / 60 / 60
 
 -     min = @total_sec / 60 % 60
 
 -     sec = @total_sec % 60
 
 -     playtime = sprintf("%02d#{TimeSet::Colon["时"]}%02d#{TimeSet::Colon["分"]}%02d#{TimeSet::Colon["秒"]}", hrs, min, sec)
 
 -     self.contents.font.color = normal_color
 
 -     self.contents.draw_text(x, 30, 126, WLH, playtime, 2)
 
 -     time_text = TimeSet::Text
 
 -     self.contents.draw_text(x, -30, 160, 90, time_text,0)
 
 -   end
 
 - end
 
 
  复制代码 总结,我们实战了: 
1.使用类外的常量 
2.module模块类 
3.调用module里的常量 
4.数组(Array) 
5.哈希(Hash) 
 
总结,我们学到了: 
1.""中的变量#{} 
 
 
 
 
看完教程请回复, 
我们需要的是支持!! 
 
好了,这堂实战课上完了,谢谢大家。 
 
下一课: 
新手教程——细说数组与哈希 
 |   
 
 
 
 |