一个很有意思的小东西。t为闹铃运行时间,单位是秒,我们可以写一个简单的转换程序来使得t变为输入格式为hour:minute:second。
原帖地址:http://bbs.pinggu.org/thread-2266932-1-1.html
简单修改
clock<-function(hour,minute,second,back,circle,hou,sec,min,method){
#请事先安装sound包!
#back为闹铃背景色
#circle为闹钟外框颜色
#hou为时针颜色
#sec秒针颜色
#min分针颜色
#hou时针颜色
#method参数为选择提醒方式,取值为1,2,3,1为响铃,2为弹出窗口,其余数值都进行
t<-hour*3600+minute*60+second
#定义缺省值
if(missing(back)){
back="gray";
if(missing(circle)){
circle="green";
}
}
if(missing(hou)){
hou<-"red";
if(missing(min)){
min<-"orange";
if(missing(sec)){
sec<-"blue";
}
else{next}
}
else{next}
}
else{next}
if(missing(method)){
method=3;
}
library(sound);#sound用于发声
#绘制闹钟
par(bg=back,lwd=3,bty="n",xaxt="n", yaxt="n")
x=seq(0,2*pi,0.01);
plot(cos(x),sin(x),type="l",xlab="",ylab="",col=circle);
lines(1.05*sin(x),1.05*cos(x),col=circle);
title(main="clock",font=2,cex=2);
#标记时刻
for(k in 1:12){
second<-c("1","2","3","4","5","6","7",
"8","9","10","11","12");
text(0.93*cos(2*pi/12*(3-k)),0.93*sin(2*pi/12*(3-k)),
labels = second[k],adj=c(0.5,0.5),col="yellow",
font=2,cex=1.7);
}
#获取系统时间
A<-date();
h0<-as.numeric(substr(A,12,13));
m0<-as.numeric(substr(A,15,16));
n0<-as.numeric(substr(A,18,19));
if(h0>12){
h0=h0-12;
}
#设置时钟,三针已齐全
n=60;m=60;h=12;
#设置分针,时针进位判别函数
#分针
f1<-function(ii){
if((16-n0-i)<=-45){
arrows(0,0,0.7*cos(2*pi/n*(16-m0-j)),
0.7*sin(2*pi/n*(16-m0-j)),col=back);
arrows(0,0,0.7*cos(2*pi/n*(15-m0-j)),
0.7*sin(2*pi/n*(15-m0-j)),col=min);
}else{
arrows(0,0,0.7*cos(2*pi/n*(16-m0-j)),
0.7*sin(2*pi/n*(16-m0-j)),col=min);
}
}
f2<-function(jj){
if((16-m0-j)<=-44&(16-n0-i)<=-45){
arrows(0,0,0.5*cos(2*pi/h*(4-h0-l)),
0.5*sin(2*pi/h*(4-h0-l)),col=back);
arrows(0,0,0.5*cos(2*pi/h*(3-h0-l)),
0.5*sin(2*pi/h*(3-h0-l)),col=hou);
}else{
arrows(0,0,0.5*cos(2*pi/h*(4-h0-l)),
0.5*sin(2*pi/h*(4-h0-l)),col=hou);
}
}
#时钟部分循环主体
repeat{
for(l in 1:h){
for(j in 1:m){
for(i in 1:n){
points(0,0,cex=2);
arrows(0,0,0.85*cos(2*pi/n*(16-n0-i)),
0.85*sin(2*pi/n*(16-n0-i)),col=back);
f1(i);
f2(j);
arrows(0,0,0.85*cos(2*pi/n*(15-n0-i)),
0.85*sin(2*pi/n*(15-n0-i)),col=sec);
Sys.sleep(1);
arrows(0,0,0.85*cos(2*pi/n*(15-n0-i)),
0.85*sin(2*pi/n*(15-n0-i)),col=back);
f1(i);
f2(j);
lines(sin(x),cos(x),col=circle);
#编辑闹铃功能
t=t-1;
if(t==0){
break;
}else{next}
}
#
f2(j);
#
if(t==0){
arrows(0,0,0.7*cos(2*pi/n*(15-m0-j)),
0.7*sin(2*pi/n*(15-m0-j)),col=back);
points(0,0,cex=2);
break;
}else{next}
}
if(t==0){
arrows(0,0,0.5*cos(2*pi/h*(3-h0-l)),
0.5*sin(2*pi/h*(3-h0-l)),col=back);
points(0,0,cex=2);
break;
}
}
if(t==0){break}
else{next}
}
#闹铃提醒
#编制闹铃
#定义音符
s0 <- Sine(0,0.25);
s1 <- Sine(523.25,0.25);
s2 <- Sine(587.33,0.25);
s3 <- Sine(659.26,0.25);
s4 <- Sine(698.46,0.25);
s5 <- Sine(784,0.25);
s6 <- Sine(880,0.25);
s7 <- Sine(987.77,0.25);
s11 <- Sine(1046.5,0.25);
sound<-appendSample(s5,s6,s5,s3,s4,s5,s4,s2,s1,s3,s5,s11);
s<-mirror(sound);
if(method==1){
play(s);#播放铃声
}
else if(method==2){
winDialog(,"时间到!!!");
}
else{
play(s);#播放铃声
winDialog(,"时间到!!!");
}
}