val s=Staging //> s: net.kogics.kojo.staging.API = net.kogics.kojo.staging.API@3fba0cab
s.clear
for(i<-0 to 10){
s.line(0,i*10,100,i*10)
}
Thread.sleep(2000)
for(i<-0 to 10){
s.line(i*10,0,i*10,100)
}
val s=Staging //> s: net.kogics.kojo.staging.API = net.kogics.kojo.staging.API@3fba0cab
s.clear
for(i<-0 to 10){
for(j<-0 to 10) {
s.circle(i*20,j*20,6)
}
}
val s=Staging s.clear
for(i<-0 to 10){
for(j<-0 to 10) {
if(i%2==1 && j%2==1 ||
i%2==0 && j%2==0 )
s.setFillColor(blue)
else
s.setFillColor(white)
s.rectangle(i*20, j*20, 16, 16)
}
}
val s = Staging
s.clear
for (i <- 0 to 10) {
for (j <- 0 to 10) {
s.setFillColor(if(i % 2 == j % 2) blue else white)
s.rectangle(i * 20, j * 20, 16, 16)
}
}
val r=s.rectangle(i * 20, j * 20, 16, 16)
r.fill(if(i % 2 == j % 2)blue else white)
val s = Staging
s.clear
for (i <- 0 to 10) {
for (j <- 0 to 10) {
s.rectangle(i * 20, j * 20, 16, 16).
fill(if(i % 2 == j % 2)blue else
white)
}
}
val s = Staging
s.clear
val r=s.rectangle(150,0,200,100)
val c=s.circle(200,100,60)
0
val l=s.line(-200,0,300,0)
s.loop {
r.rotate(10)
c.rotate(5)
l.translate(-1,2)
}
scala> 5%2
res0: Int = 1
scala> 6%2
res1: Int = 0
scala> 2*3+4*5
res2: Int = 26
scala> ((2*3)+4)*5
res3: Int = 50
scala> (2*3)+(4*5)
res4: Int = 26
val s = Staging
s.clear
val cs=for(i<-0 to 10)
yield s.circle(i*40,0,25)
s.loop{
for(c<-cs) c.translate(5-random(11),5-random(11))
}
![]() |
乱数の期待値 |
注:このとき translate に与える乱数は、
期待値が0になるように調節しておかないと、
時間経過とともに図形群が原点から離れていってしまうことに注意。
val s = Staging
def rancol=color(random(256),random(256),random(256))
s.clear
val cs=for(i<-0 to 10)
yield s.circle(i*40,0,25)
var n=0
s.loop{
n+=1
if(n%20<10)
for(c<-cs)c.hide
else {
for(c<-cs)c.show()
for(c<-cs)c.translate(10-random(21),10-random(21))
for(c<-cs)c.fill(rancol)
}
}
その配列の要素を取り出す時には、<-
の左側(ループ変数を書いていた場所)に、
タプルの各要素を受け取る変数名を()で包んで書ける。
val s = Staging;s.clear
def rancol=color(random(256),random(256),random(256))
val cs=for(i<-0 to 100)
yield (
s.circle(random(300)-150,random(200)-100,random(25-10))
,random(10)-5,random(10)-5)
s.loop{
Thread.sleep(100)
for((c,x,y)<-cs) c.translate(x,y)
}
なお、最後の (translate を含む)行は、以下のようにも書ける。
for(t<-cs) t._1.translate(t._2,t._3)