本帖最后由 “” 于 2016-8-28 19:57 编辑
- show=0
- '光标当前位置
- CurrentSquare=0
- '光标四周位置
- CurrentSquareRound[8]=0
- '方格数量设定
- SquareWidth=0
- SquareHeight=0
- SquareTol=0
- '地雷数组
- MineTol=25
- Mines[149]=0
- '屏幕显示数组
- SquareShow[149]=0
- '判断游戏结束
- GameOver=0
- Opened=0
- '循环时间间隔100ms
- TimeInterval=100
- SelectTime=0
- '按键
- CurrentClick = ""
- LastClick = ""
- Clicktime=0
- '电机移动光标
- LastMoto=0
- CurrentMoto=0
- MoveMode=0
- MoveModeTime=0
- LastMoto_2=0
- CurrentMoto_2=0
- '触动
- LastTouch=0
- Touch=0
- '—————————————————————————————————————————————————————————————————
- '判断电机数量
- Motor.ResetCount ("AD")
- Motor.Start ("AD", 20)
- program.delay(50)
- if Motor.GetCount ("A")<>0 and Motor.GetCount ("D")<>0 then
- MotoMode=1
- endif
- Motor.Stop ("AD", "False")
- While 0<1
- '初始化
- initialization()
-
- '游戏过程
- GameOver=0
- Opened=0
- Flag=0
- Motor.ResetCount ("D")
- CurrentSquare=Math.Ceiling(SquareTol/2)+Math.Floor(SquareWidth/2)
- Draw_f()
- program.delay(200)
- While GameOver=0
- LCD.Text(1,0,0,1,squaretol-opened)
- LCD.text(1,150,0,1,MineTol-Flag)
- '按键操作
- CurrentClick=Buttons.Current
- if CurrentClick = LastClick then
- Clicktime=Clicktime+TimeInterval
- endif
- if Clicktime=300 or CurrentClick <> LastClick then
- if CurrentClick="E" or Sensor.ReadPercent (1) =100 then
- if SquareShow[CurrentSquare]=-3 then
- '打开
- if Mines[CurrentSquare]=-1 then
- SquareShow[CurrentSquare]=Mines[CurrentSquare]
- GameOver=1
- else
- if Mines[CurrentSquare] <> 0 then
- Open_1()
- SelectTime=500-TimeInterval
- else
- Open_0()
- SelectTime=500-TimeInterval
- endif
- endif
- elseif SquareShow[CurrentSquare]=-2 or SquareShow[CurrentSquare]=-1 then
- '旗子,不处理
- else
- '检测周围插旗数量 可以打开周围所有的数
- Open_S()
- endif
- elseif CurrentClick <> "" then
- MoveSelect()
- LastClick=CurrentClick
- Clicktime=0
- SelectTime=0-TimeInterval
- endif
- endif
-
- '电机旋转移动光标
- if MotoMode=0 then '单个电机
- CurrentMoto=Motor.GetSpeed ("D")
- if MoveMode=0 then
- if CurrentMoto =0 then
- elseif CurrentMoto<-12 or CurrentMoto>12 then
- MoveMode=1
- else
- MoveMode=2
- endif
- elseif MoveMode=1 then '垂直模式
- if CurrentMoto>0 then
- Draw()
- if CurrentSquare-SquareWidth<0 then
- CurrentSquare=CurrentSquare+SquareTol-SquareWidth
- else
- CurrentSquare=CurrentSquare-SquareWidth
- endif
- Draw_f()
- SelectTime=0
- MoveModeTime=0
- program.delay(200)
- elseif CurrentMoto<0 then
- Draw()
- if CurrentSquare+SquareWidth>SquareTol-1 then
- CurrentSquare=CurrentSquare-SquareTol+SquareWidth
- else
- CurrentSquare=CurrentSquare+SquareWidth
- endif
- Draw_f()
- SelectTime=0
- MoveModeTime=0
- program.delay(200)
- else
- MoveModeTime=MoveModeTime+TimeInterval
- if MoveModeTime>500 then
- MoveMode=0
- MoveModeTime=0
- Speaker.Tone(100,1000,100)
- endif
- endif
- elseif MoveMode=2 then'水平模式
- if CurrentMoto<0 then
- Draw()
- if CurrentSquare=SquareTol-1 then
- CurrentSquare=0
- else
- CurrentSquare=CurrentSquare+1
- endif
- Draw_f()
- SelectTime=0
- MoveModeTime=0
- program.delay(200)
- elseif CurrentMoto>0 then
- Draw()
- if CurrentSquare=0 then
- CurrentSquare=SquareTol-1
- else
- CurrentSquare=CurrentSquare-1
- endif
- Draw_f()
- SelectTime=0
- MoveModeTime=0
- program.delay(200)
- else
- MoveModeTime=MoveModeTime+TimeInterval
- if MoveModeTime>1000 then
- MoveMode=0
- MoveModeTime=0
- Speaker.Tone(100,1000,100)
- endif
- endif
- endif
- else '多个电机
- '上下移动
- CurrentMoto=Motor.GetSpeed ("A")
-
- if CurrentMoto <-3 then
- CurrentMoto=-1
- elseif CurrentMoto >3 then
- CurrentMoto=1
- else
- CurrentMoto=0
- endif
-
- if CurrentMoto > 0 then
- Draw()
- if CurrentSquare+SquareWidth > SquareTol-1 then
- CurrentSquare=CurrentSquare+SquareWidth-SquareTol
- else
- CurrentSquare=CurrentSquare+SquareWidth
- endif
- Draw_f()
- program.delay(200)
- SelectTime=0
- elseif CurrentMoto < 0 then
- Draw()
- if CurrentSquare-SquareWidth<0 then
- CurrentSquare=CurrentSquare-SquareWidth+SquareTol
- else
- CurrentSquare=CurrentSquare-SquareWidth
- endif
- Draw_f()
- program.delay(200)
- SelectTime=0
- endif
-
- '左右移动
- CurrentMoto_2=Motor.GetSpeed ("D")
-
- if CurrentMoto_2 <-3 then
- CurrentMoto_2=-1
- elseif CurrentMoto_2 >3 then
- CurrentMoto_2=1
- else
- CurrentMoto_2=0
- endif
-
- if CurrentMoto_2 > 0 then
- Draw()
- if Math.Remainder(CurrentSquare,SquareWidth)=0 then
- CurrentSquare=CurrentSquare+SquareWidth-1
- else
- CurrentSquare=CurrentSquare-1
- endif
- Draw_f()
- program.delay(200)
- SelectTime=0
- elseif CurrentMoto_2 < 0 then
- Draw()
- if Math.Remainder(CurrentSquare,SquareWidth)=SquareWidth-1 then
- CurrentSquare=CurrentSquare-SquareWidth+1
- else
- CurrentSquare=CurrentSquare+1
- endif
- Draw_f()
- program.delay(200)
- SelectTime=0
- endif
- endif
-
- '触动操作(按下100,弹出0)
- Touch=Sensor.ReadPercent (4)
- if Touch=100 and LastTouch=0 then
- if SquareShow[CurrentSquare]=-3 then
- SquareShow[CurrentSquare]=-2
- Flag=Flag+1
- Draw()
- elseif SquareShow[CurrentSquare]=-2 then
- SquareShow[CurrentSquare]=-3
- Draw()
- Flag=Flag-1
- endif
- endif
- LastTouch=Touch
-
- if Sensor.ReadPercent (1) =100 then
- if SquareShow[CurrentSquare]=-3 then
- '打开
- if Mines[CurrentSquare]=-1 then
- SquareShow[CurrentSquare]=Mines[CurrentSquare]
- GameOver=1
- else
- if Mines[CurrentSquare] <> 0 then
- Open_1()
- SelectTime=500-TimeInterval
- else
- Open_0()
- SelectTime=500-TimeInterval
- endif
- endif
- elseif SquareShow[CurrentSquare]=-2 or SquareShow[CurrentSquare]=-1 then
- '旗子,不处理
- else
- '检测周围插旗数量 可以打开周围所有的数
- Open_S()
- endif
- endif
-
-
-
-
-
-
-
-
-
- '时间间隔,500毫秒闪烁
- SelectTime=SelectTime+TimeInterval
- if SelectTime=0 then
- Draw_f()
- elseif SelectTime=500 then
- Draw()
- elseif SelectTime=1000 then
- SelectTime=0-TimeInterval
- endif
- Program.Delay (TimeInterval)
-
- if opened=SquareTol-MineTol then
- GameOver=1
- endif
- endWhile
-
- '显示结果
- LCD.Clear()
- if opened=SquareTol-MineTol then
- LCD.text(1,28,70,2,"YOU WIN")
- else
- LCD.text(1,20,70,2,"YOU LOST")
- endif
- Program.delay(1000)
- '重玩或者显示错误结果
- CurrentClick=""
- show=0
- while CurrentClick <> "E"
- CurrentClick=Buttons.Current
- if Sensor.ReadPercent (1) >30 then
- CurrentClick = "E"
- endif
- if Sensor.ReadPercent (4) >30 then
- CurrentClick = "L"
- endif
-
- if CurrentClick <>"E" and CurrentClick<>"" and show = 0 then
- LCD.Clear()
- '画所有格子
- for i=0 to 178 step 2
- LCD.Line(1,0+i,0+10,178,178+10-i)
- endfor
- for i= 2 to 118 step 2
- LCD.Line(1,0,i+10,118-i,118+10)
- endfor
- for i=0 to SquareWidth
- LCD.FillRect(0,i*12+10,10,2,118)
- LCD.FillRect(0,0,i*12+20,178,2)
- endfor
-
- for CurrentSquare=0 to SquareTol-1
- if SquareShow[CurrentSquare]=-3 and Mines[CurrentSquare]=-1 then
- DrawMine()
- elseif SquareShow[CurrentSquare]=-1 then
- DrawMine_f()
- elseif SquareShow[CurrentSquare]=-2 and Mines[CurrentSquare]=-1 then
- DrawFlag()
- elseif SquareShow[CurrentSquare]=-2 and Mines[CurrentSquare]<>-1 then
- DrawFlag_f()
- elseif SquareShow[CurrentSquare]<>-3 then
- DrawNum()
- endif
- endfor
- show =1
- endif
- endwhile
- endwhile
- sub initialization
- LCD.clear()
- show =0
- '观察扫雷界面
- 'if show=1 then
- ' for i=0 to 178 step 12
- ' for j=0 to 119 step 12
- ' LCD.FillRect (1, i, j+10, 10, 10)
- ' endfor
- ' endfor
- 'endif
-
- '扫雷界面长宽数量
- SquareWidth=15
- SquareHeight=10
- SquareTol=SquareWidth*SquareHeight
-
- '画所有格子
- for i=0 to 178 step 2
- LCD.Line(1,0+i,0+10,178,178+10-i)
- endfor
- for i= 2 to 118 step 2
- LCD.Line(1,0,i+10,118-i,118+10)
- endfor
- for i=0 to SquareWidth
- LCD.FillRect(0,i*12+10,10,2,118)
- LCD.FillRect(0,0,i*12+20,178,2)
- endfor
-
- '生成地雷数组
- for i=0 to SquareTol-1
- Mines[i]=0
- endfor
-
- '随机生成地雷
- i=0
- while MineTol <> i
- Rand=Math.GetRandomNumber (SquareTol)-1
- '可以让地雷周围的8格增加1
- if Mines[Rand] =0 then
- Mines[Rand] =-1
- i=i+1
- CurrentSquare=Rand
- GetCurrentSquareRound()
- for j= 0 to 7
- if CurrentSquareRound[j]<>-1 then
- Mines[CurrentSquareRound[j]]=Mines[CurrentSquareRound[j]]+1
- endif
- endfor
- endif
- endWhile
-
- '设定显示状态
- for i=0 to SquareTol -1
- SquareShow[i]=-3
- endfor
- endsub
- sub GetCurrentSquareRound
- '左上
- if Math.Remainder(CurrentSquare,SquareWidth)>0 and CurrentSquare-SquareWidth>=0 then
- CurrentSquareRound[0]=CurrentSquare-SquareWidth-1
- else
- CurrentSquareRound[0]=-1
- endif
-
- '上
- if CurrentSquare-SquareWidth>=0 then
- CurrentSquareRound[1]=CurrentSquare-SquareWidth
- else
- CurrentSquareRound[1]=-1
- endif
-
- '右上
- if Math.Remainder(CurrentSquare,SquareWidth)<SquareWidth-1 and CurrentSquare-SquareWidth>=0 then
- CurrentSquareRound[2]=CurrentSquare-SquareWidth+1
- else
- CurrentSquareRound[2]=-1
- endif
-
- '左
- if Math.Remainder(CurrentSquare,SquareWidth)>0 then
- CurrentSquareRound[3]=CurrentSquare-1
- else
- CurrentSquareRound[3]=-1
- endif
-
- '右
- if Math.Remainder(CurrentSquare,SquareWidth)<SquareWidth-1 then
- CurrentSquareRound[4]=CurrentSquare+1
- else
- CurrentSquareRound[4]=-1
- endif
-
- '左下
- if Math.Remainder(CurrentSquare,SquareWidth)>0 and CurrentSquare+SquareWidth <150 then
- CurrentSquareRound[5]=CurrentSquare+SquareWidth-1
- else
- CurrentSquareRound[5]=-1
- endif
-
- '下
- if CurrentSquare+SquareWidth <150 then
- CurrentSquareRound[6]=CurrentSquare+SquareWidth
- else
- CurrentSquareRound[6]=-1
- endif
-
- '右下
- if Math.Remainder(CurrentSquare,SquareWidth)<SquareWidth-1 and CurrentSquare+SquareWidth <150 then
- CurrentSquareRound[7]=CurrentSquare+SquareWidth+1
- else
- CurrentSquareRound[7]=-1
- endif
- endSub
- sub MoveSelect
- Draw()
- if CurrentClick="U" then
- if CurrentSquare > SquareWidth-1 then
- CurrentSquare=CurrentSquare-SquareWidth
- else
- CurrentSquare=CurrentSquare+SquareTol-SquareWidth
- endif
-
- elseif CurrentClick="D" then
- if CurrentSquare < SquareTol-SquareWidth then
- CurrentSquare=CurrentSquare+SquareWidth
- else
- CurrentSquare=CurrentSquare+SquareWidth-SquareTol
- endif
-
- elseif CurrentClick="L" then
- if Math.Remainder(CurrentSquare,SquareWidth)>0 then
- CurrentSquare=CurrentSquare-1
- else
- CurrentSquare=CurrentSquare+SquareWidth-1
- endif
-
- elseif CurrentClick="R" then
- if Math.Remainder(CurrentSquare,SquareWidth)<SquareWidth-1 then
- CurrentSquare=CurrentSquare+1
- else
- CurrentSquare=CurrentSquare+1-SquareWidth
- endif
-
- endif
- endsub
- sub Draw
- if SquareShow[CurrentSquare]=-3 then
- DrawGray()
- elseif SquareShow[CurrentSquare]=-2 then
- DrawFlag()
- elseif SquareShow[CurrentSquare]=-1 then
- elseif SquareShow[CurrentSquare]=0 then
- DrawWhite()
- else
- DrawNum()
- endif
- endsub
- sub Draw_f
- if SquareShow[CurrentSquare]=-3 then
- DrawGray_f()
- elseif SquareShow[CurrentSquare]=-2 then
- DrawFlag_f()
- elseif SquareShow[CurrentSquare]=0 then
- DrawWhite_f()
- else
- DrawNum_f()
- endif
- endsub
- sub DrawGray
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (0, DrawX, DrawY, 10, 10)
- for i=0 to 8 step 2
- LCD.Line(1,DrawX+i, DrawY,DrawX+10-1, DrawY+10-i-1)
- LCD.Line(1,DrawX, DrawY+i,DrawX+10-i-1, DrawY+10-1)
- endfor
- endsub
- sub DrawGray_f
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (1, DrawX, DrawY, 10, 10)
- 'for i=0 to 10 step 2
- 'LCD.Line(0,DrawX+i, DrawY,DrawX+10, DrawY+10-i)
- 'LCD.Line(0,DrawX, DrawY+i,DrawX+10-i, DrawY+10)
- 'endfor
- endsub
- sub DrawNum
- if SquareShow[CurrentSquare]<>-1 then
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (0, DrawX, DrawY, 10, 10)
- LCD.Text(1,DrawX+1, DrawY+1,1,Mines[CurrentSquare])
- endif
- endsub
- sub DrawNum_f
- if SquareShow[CurrentSquare]<>-1 then
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (1, DrawX, DrawY, 10, 10)
- LCD.Text(0,DrawX+1, DrawY+1,1,Mines[CurrentSquare])
- endif
- endsub
- sub DrawFlag
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (0, DrawX, DrawY, 10, 10)
- LCD.Line(1,DrawX+3, DrawY,DrawX+3, DrawY+8)
- LCD.Line(1,DrawX+4, DrawY+1,DrawX+4, DrawY+8)
- LCD.Line(1,DrawX+5, DrawY+2,DrawX+5, DrawY+4)
- LCD.Line(1,DrawX+6, DrawY+3,DrawX+6, DrawY+4)
- LCD.Line(1,DrawX+7, DrawY+4,DrawX+7, DrawY+4)
- LCD.Line(1,DrawX+3, DrawY,DrawX+8, DrawY+4)
- LCD.Line(1,DrawX+1, DrawY+7,DrawX+7, DrawY+7)
- LCD.Line(1,DrawX+1, DrawY+8,DrawX+7, DrawY+8)
- endsub
- sub DrawFlag_f
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (1, DrawX, DrawY, 10, 10)
- LCD.Line(0,DrawX+3, DrawY,DrawX+3, DrawY+8)
- LCD.Line(0,DrawX+4, DrawY+1,DrawX+4, DrawY+8)
- LCD.Line(0,DrawX+5, DrawY+2,DrawX+5, DrawY+4)
- LCD.Line(0,DrawX+6, DrawY+3,DrawX+6, DrawY+4)
- LCD.Line(0,DrawX+7, DrawY+4,DrawX+7, DrawY+4)
- LCD.Line(0,DrawX+3, DrawY,DrawX+8, DrawY+4)
- LCD.Line(0,DrawX+1, DrawY+7,DrawX+7, DrawY+7)
- LCD.Line(0,DrawX+1, DrawY+8,DrawX+7, DrawY+8)
- endsub
- sub DrawWhite
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (0, DrawX, DrawY, 10, 10)
- endsub
- sub DrawWhite_f
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (1, DrawX, DrawY, 10, 10)
- endsub
- sub DrawMine
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (0, DrawX, DrawY, 10, 10)
- LCD.FillCircle (1, DrawX+4+1,DrawY+6, 3)
- LCD.FillRect(1,DrawX+4+1,DrawY+1,2,5)
- endsub
- sub DrawMine_f
- DrawX=Math.Remainder(CurrentSquare,SquareWidth)*12
- DrawY=Math.Floor(CurrentSquare/ SquareWidth)*12+10
- LCD.FillRect (1, DrawX, DrawY, 10, 10)
- LCD.FillCircle (0, DrawX+4+1,DrawY+6, 3)
- LCD.FillRect(0,DrawX+4+1,DrawY+1,2,5)
- endsub
- sub Open_1
- SquareShow[CurrentSquare]=Mines[CurrentSquare]
- Opened=Opened+1
- Draw()
- endsub
- sub Open_0
- '选中的是0,打开周围都是0的方格
- CurrentSquare0=CurrentSquare
- for i=0 to 150
- Select0[i]=0
- endfor
- Select0Head=0
- Select0Tail=0
-
- Select0[Select0Head]=CurrentSquare
- Select0Head=Select0Head+1
- open_1()
-
- while Select0Head <> Select0Tail
- CurrentSquare=Select0[Select0Tail]
- GetCurrentSquareRound()
-
- for i=0 to 7
- CurrentSquare=CurrentSquareRound[i]
- if Mines[CurrentSquare]=0 and SquareShow[CurrentSquare]=-3 then
- Open_1()
- Select0[Select0Head]=CurrentSquare
- Select0Head=Select0Head+1
- elseif Mines[CurrentSquare] <> 0 and SquareShow[CurrentSquare]=-3 then
- Open_1()
- endif
- endfor
- Select0Tail=Select0Tail+1
- endwhile
- CurrentSquare=CurrentSquare0
- Endsub
- sub Open_S
- GetCurrentSquareRound()
- MineFlag=0
- for i =0 to 7
- if SquareShow[CurrentSquareRound[i]]=-2 then
- MineFlag=MineFlag+1
- endif
- endfor
-
- if MineFlag= SquareShow[CurrentSquare] then
- CurrentSquareS=CurrentSquare
- for i =0 to 7
- if Squareshow[CurrentSquareRound[i]]=-3 then
- CurrentSquare=CurrentSquareRound[i]
- Squareshow[CurrentSquare]=Mines[CurrentSquare]
- if Squareshow[CurrentSquare]=-1 then
- GameOver=1
- elseif Squareshow[CurrentSquare]=0 then
- open_0()
- else
- open_1()
- endif
- endif
- endfor
- CurrentSquare=CurrentSquareS
- endif
- endsub
复制代码
|