|
楼主 |
发表于 2012-7-15 15:43:27
|
显示全部楼层
-- 程序版权所有: 广东中山 高建彪 QQ: 76456245 E-mail:[email protected] 2012.07.15
platform.apiLevel = '1.0'
sz1 = "" -- 第1颗骰子点数
sz2 = "" -- 第2颗骰子点数
sz3 = "" -- 第3颗骰子点数
dsh = {0,0,0,0,0,0} --点数和初始值
syzs = 0 --实验总次数
anxz = 0 --按钮选择
andj = 0 --按钮点击
kszt = 2 --开始按钮状态, 2准备开始,1准备暂停,0准备继续
szks = 1 --骰子颗数
sdbl = 3 --速度变量
--主窗口程序
function on.paint(gc)
if szks == 1 then
shaizi1 = shaizi(40,110,sz1,70)
shaizi1:paint(gc)
elseif szks == 2 then
shaizi1 = shaizi(20,90,sz1,45)
shaizi1:paint(gc)
shaizi2 = shaizi(80,130,sz2,45)
shaizi2:paint(gc)
elseif szks == 3 then
shaizi1 = shaizi(150,20,sz1,30)
shaizi1:paint(gc)
shaizi2 = shaizi(200,20,sz2,30)
shaizi2:paint(gc)
shaizi3 = shaizi(250,20,sz2,30)
shaizi3:paint(gc)
end
if szks == 3 then
dsfb = dsfbt(30,80,260,80,ds)
else
dsfb = dsfbt(155,50,140,120,ds)
end
dsfb:paint(gc)
local kstxt = ""
if kszt == 2 then
kstxt = "开始(R)"
elseif kszt == 1 then
kstxt = "暂停(S)"
elseif kszt == 0 then
kstxt = "继续(J)"
end
anks = anniu(70,5,60,20,kstxt)
anks:paint(gc)
antz = anniu(70,30,60,20,"停止(T)")
antz:paint(gc)
gc:drawString("骰子数",5,25)
gc:drawString(szks,25,50)
anjs = anniu(5,30,15,20,"-")
anjs:paint(gc)
anzj = anniu(40,30,15,20,"+")
anzj:paint(gc)
if anxz > 0 then
gc:setColorRGB(255,0,0)
gc:setPen("medium","smooth")
end
if anxz == 1 then
gc:drawRect(70,5,60,20)
elseif anxz == 2 then
gc:drawRect(70,30,60,20)
elseif anxz == 3 then
gc:drawRect(5,30,15,20)
elseif anxz == 4 then
gc:drawRect(40,30,15,20)
end
end
function on.timer()
sz1 = math.random(1,6)
if szks == 1 then
dsh[sz1] = dsh[sz1] + 1
elseif szks == 2 then
sz2 = math.random(1,6)
dsh[sz1+sz2-1] = dsh[sz1+sz2-1] + 1
elseif szks == 3 then
sz2 = math.random(1,6)
sz3 = math.random(1,6)
dsh[sz1+sz2+sz3-2] = dsh[sz1+sz2+sz3-2] + 1
end
syzs = syzs + 1
platform.window:invalidate()
end
-- 骰子模型
shaizi = class()
function shaizi:init(x,y,s,l)
self.x = x --骰子左上角x坐标
self.y = y --骰子左上角y坐标
self.s = s --骰子点数
self.l = l --骰子边长
end
function shaizi:paint(gc)
local x, y, s, l = self.x, self.y, self.s, self.l
gc:setPen("medium","smooth")
gc:setColorRGB(0,0,0)
gc:drawRect(x,y,l,l)
local bh = l/2 * math.sqrt(2) / 2
gc:drawLine(x,y,x+bh,y-bh)
gc:drawLine(x+l,y,x+l+bh,y-bh)
gc:drawLine(x+l,y+l,x+l+bh,y+l-bh)
gc:drawLine(x+bh,y-bh,x+l+bh,y-bh)
gc:drawLine(x+l+bh,y-bh,x+l+bh,y+l-bh)
gc:setPen("thick","smooth")
gc:setColorRGB(255,0,0)
local z = 5
if szks == 3 then
z = 3
end
if s == 1 then
gc:drawArc(x+l/2,y+l/2,z,z,0,360)
elseif s == 2 then
gc:drawArc(x+l/4,y+l/4,z,z,0,360)
gc:drawArc(x+3*l/4,y+3*l/4,z,z,0,360)
elseif s == 3 then
gc:drawArc(x+l/4,y+l/4,z,z,0,360)
gc:drawArc(x+3*l/4,y+3*l/4,z,z,0,360)
gc:drawArc(x+l/2,y+l/2,z,z,0,360)
elseif s == 4 then
gc:drawArc(x+l/4,y+l/4,z,z,0,360)
gc:drawArc(x+3*l/4,y+l/4,z,z,0,360)
gc:drawArc(x+l/4,y+3*l/4,z,z,0,360)
gc:drawArc(x+3*l/4,y+3*l/4,z,z,0,360)
elseif s == 5 then
gc:drawArc(x+l/4,y+l/4,z,z,0,360)
gc:drawArc(x+3*l/4,y+l/4,z,z,0,360)
gc:drawArc(x+l/4,y+3*l/4,z,z,0,360)
gc:drawArc(x+3*l/4,y+3*l/4,z,z,0,360)
gc:drawArc(x+l/2,y+l/2,z,z,0,360)
elseif s == 6 then
gc:drawArc(x+l/4,y+l/4,z,z,0,360)
gc:drawArc(x+3*l/4,y+l/4,z,z,0,360)
gc:drawArc(x+l/4,y+3*l/4,z,z,0,360)
gc:drawArc(x+3*l/4,y+3*l/4,z,z,0,360)
gc:drawArc(x+l/4,y+l/2,z,z,0,360)
gc:drawArc(x+3*l/4,y+l/2,z,z,0,360)
end
gc:setColorRGB(200,0,100)
gc:drawString(s,x+l/2,y+l+20)
platform.window:invalidate()
end
-- 点数条形图模型
dsfbt = class()
function dsfbt:init(x,y,w,h,s)
self.x = x --条形图左上角x坐标
self.y = y --条形图左上角y坐标
self.w = w --条形图宽图
self.h = h --条形图高度
self.s = s --条形图数据
end
function dsfbt:paint(gc)
local x, y, w, h, s = self.x, self.y, self.w, self.h, self.s
gc:setColorRGB(0,0,0)
gc:setPen("thin","dotted")
gc:drawRect(x,y,w,h)
gc:setPen("thin","smooth")
gc:setColorRGB(150,150,150)
local szzd = szmax(dsh)
local szws = table.getn(dsh)
gc:drawString("%",x+w-5,y)
if szzd > 0 then
for n in pairs(dsh) do
gc:fillRect(x+(w/szws)*(n-1),y+h-h/szzd*dsh[n],w/szws,h/szzd*dsh[n])
gc:setFont("serif","r",9)
if szks == 1 then
gc:drawString(n,x+(w/szws)*(n-1)+3,y+h+20)
elseif szks == 2 then
gc:drawString(n+1,x+(w/szws)*(n-1)+3,y+h+20)
elseif szks ==3 then
gc:drawString(n+2,x+(w/szws)*(n-1)+3,y+h+20)
end
local bls = math.floor(dsh[n]/syzs*100)
gc:setFont("serif","r",6)
gc:drawString(bls,x+(w/szws)*(n-1)+3,y)
end
end
gc:setFont("serif","r",12)
gc:drawString("实验总次数: "..syzs,x,y+h+40)
end
function szmax(x) --数组最大值函数
local max0 = 0
for n in pairs(dsh) do
if max0 < dsh[n] then
max0 = dsh[n]
end
end
return max0
end
--按钮模型
anniu = class()
function anniu:init(x,y,w,h,t)
self.x = x --按钮左上角x坐标
self.y = y --按钮左上角y坐标
self.w = w --按钮宽度
self.h =h --按钮高度
self.t = t --按钮文本
self.s = false
end
function anniu:paint(gc)
local x, y, w, h, t, s = self.x, self.y, self.w, self.h, self.t, self.s
if s then
gc:setColorRGB(255,0,0)
else
gc:setColorRGB(100,0,0)
end
gc:setPen("thin","smooth")
gc:drawRect(x,y,w,h)
gc:drawString(t,x+3,y+20)
end
--鼠标指针事件
function on.mouseMove(x,y)
if x>70 and x<130 and y>5 and y<25 then
anxz = 1
elseif x>70 and x<130 and y>30 and y<50 then
anxz = 2
elseif x>5 and x<20 and y>30 and y<50 then
anxz = 3
elseif x>40 and x<55 and y>30 and y<50 then
anxz = 4
else
anxz = 0
end
end
--鼠标点击事件
function on.mouseDown(x,y)
if x>70 and x<130 and y>5 and y<25 then
ksaj()
elseif x>70 and x<130 and y>30 and y<50 then
tzaj()
elseif x>5 and x<20 and y>30 and y<50 then
szsjs()
elseif x>40 and x<55 and y>30 and y<50 then
szszj()
end
platform.window:invalidate()
end
function szsjs() --骰子数减少
if szks > 1 then
szks = szks - 1
dshcsh()
timer.stop()
kszt = 2
end
end
function on.charIn(x) --快捷键响应
if x == "R" or x == "r" or x == "S" or x == "s" or x == "J" or x == "j" then
ksaj()
elseif x == "T" or x == "t" then
tzaj()
elseif x == "-" then
szsjs()
elseif x == "+" then
szszj()
end
platform.window:invalidate()
end
function szszj() --骰子数增加
if szks < 3 then
szks = szks + 1
dshcsh()
timer.stop()
kszt = 2
end
end
saj() --开始按键
if kszt == 2 then
dshcsh()
end
kszt = math.fmod(kszt+1,2)
if kszt == 1 then
timer.start(1/sdbl)
elseif kszt == 0 then
timer.stop()
end
end
function tzaj() --停止按键
kszt = 2
timer.stop()
local szzd = szmax(dsh)
if szzd >0 then --存储数据
if szks == 1 then
local sz1k = {1,2,3,4,5,6}
var.store("sz1k",sz1k)
var.store("ps1",dsh)
elseif szks == 2 then
local sz2k = {2,3,4,5,6,7,8,9,10,11,12}
var.store("sz2k",sz2k)
var.store("ps2",dsh)
elseif szks == 3 then
local sz3k = {3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}
var.store("sz3k",sz3k)
var.store("ps3",dsh)
end
end
end
function dshcsh() --点数和初始化
if szks == 1 then
dsh = {0,0,0,0,0,0}
elseif szks == 2 then
dsh = {0,0,0,0,0,0,0,0,0,0,0}
elseif szks == 3 then
dsh = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
end
syzs = 0
sz1 = ""
sz2 = ""
sz3 = ""
end
function on.tabKey() -- TAB键响应
anxz = anxz + 1
if anxz > 4 then
anxz = 0
end
platform.window:invalidate()
end
function on.enterKey() --回车键响应
if anxz == 1 then
ksaj()
elseif anxz == 2 then
kszt = 2
timer.stop()
elseif anxz == 3 then
szsjs()
elseif anxz == 4 then
szszj()
end
platform.window:invalidate()
end
--菜单
menu={
{"速度设置",
{"速度1(低)",function () sdsz(1) end},
{"速度2",function () sdsz(2) end},
{"速度3",function () sdsz(3) end},
{"速度4",function () sdsz(4) end},
{"速度5",function () sdsz(5) end},
{"速度6",function () sdsz(6) end},
{"速度7",function () sdsz(7) end},
{"速度8(高)",function () sdsz(8) end},
},
}
toolpalette.register(menu)
function sdsz(x) --速度设置
sdbl = 2 * x
if kszt == 1 then
timer.start(1/sdbl)
else
timer.stop()
end
platform.window:invalidate()
end |
|