本帖最后由 “” 于 2016-8-28 20:02 编辑
- show=0
- '变量设定
- Leave=1
- score=0
- LastClick=""
- CurrentClick=""
- CurrentDir=0
- LastMoto=0
- CurrentMoto=0
- ClickTime=0
- Gameover=0
- EatingApple=0
- SquareApple=0
- MoveDir=0'1右 -1左 -SquareWidth上 SquareWidth下
- MoveStep=0
- TailDir=0
- LastTime=0
- '游戏界面的宽和高(设计好界面后确定)
- SquareWidth=25
- SquareHeight=16
- SnakeMax=SquareWidth*SquareHeight
- DrawX=0
- DrawY=0
- SnakeHead=0
- SnakeTail=0
- SnakeLength=0
- For i=0 to SnakeMax step 1
- Square[i]=0
- EndFor
- for i=0 to 9 step 1
- Dirs[i]=0
- EndFor
- DirsAt=0
- '进入游戏
- LCD.Clear ()
- Motor.ResetCount ("D")
- '选择难度leave1——leave7
- While CurrentClick <> "E"
- '屏幕按键选择难度
- CurrentClick=Buttons.Current
- if CurrentClick = LastClick then
- ClickTime=ClickTime+1
- else
- ClickTime=0
- LeaveChange()
- LastClick=CurrentClick
- endif
-
- if ClickTime >=300 then
- ClickTime=0
- LeaveChange()
- endif
-
- '电机旋转选择难度
- if CurrentClick = "" then
- CurrentMoto=Motor.GetSpeed ("D")
- if CurrentMoto <0 then
- CurrentMoto=-1
- elseif CurrentMoto >0 then
- CurrentMoto=1
- else
- CurrentMoto=0
- endif
-
- if CurrentMoto <> LastMoto and CurrentMoto <> 0 then
- MotorBeginCount=Motor.GetCount ("D")
- Leave=Leave-CurrentMoto
- elseif CurrentMoto = LastMoto then
- if Motor.GetCount ("D")-MotorBeginCount>20 then
- Leave=Leave-1
- MotorBeginCount=Motor.GetCount ("D")
- elseif Motor.GetCount ("D")-MotorBeginCount<-20 then
- Leave=Leave+1
- MotorBeginCount=Motor.GetCount ("D")
- endif
-
- if Leave < 1 then
- Leave = 1
- elseif Leave > 7 then
- Leave = 7
- endif
-
- LCD.Text (1, 28, 80, 2,"Leave "+Leave)
- endif
- LastMoto=CurrentMoto
- endif
-
- Program.Delay (1)
- Endwhile
- '游戏
- while 0<1
- '游戏初始化
- Initialization()
- Gameover=0
-
-
- '游戏过程
- while GameOver=0
- LastTime=EV3.Time
- '动作检测
-
-
- '屏幕按键检测
- if DirsAt = 0 then
- CurrentClick=Buttons.Current
- if CurrentClick="R" then
- CurrentDir=1
- elseif CurrentClick="L" then
- CurrentDir=-1
- elseif CurrentClick="U" then
- CurrentDir=0-SquareWidth
- elseif CurrentClick="D" then
- CurrentDir=SquareWidth
- else
- CurrentDir=0
- endif
- if CurrentDir <> MoveDir and CurrentDir + MoveDir <> 0 and CurrentDir <> 0 then
- Dirs[DirsAt]=CurrentDir
- DirsAt=DirsAt+1
- endif
- LastClick = CurrentClick
- elseif DirsAt < 9 then
- CurrentClick=Buttons.Current
- if CurrentClick <> LastClick and CurrentClick <> "" then
- if CurrentClick="R" then
- CurrentDir=1
- elseif CurrentClick="L" then
- CurrentDir=-1
- elseif CurrentClick="U" then
- CurrentDir=0-SquareWidth
- elseif CurrentClick="D" then
- CurrentDir=SquareWidth
- else
- CurrentDir=0
- endif
-
- if CurrentDir <> Dirs[DirsAt-1] and CurrentDir + Dirs[DirsAt-1] <>0 and CurrentDir <> 0 then
- Dirs[DirsAt]=CurrentDir
- DirsAt=DirsAt+1
- endif
- LastClick = CurrentClick
- endif
- endif
-
- CurrentMoto=Motor.GetSpeed ("D")
- if CurrentMoto <-5 then
- CurrentMoto=-1
- elseif CurrentMoto >5 then
- CurrentMoto=1
- else
- CurrentMoto=0
- endif
-
-
- 'A电机转动检测(没有按键的条件下检测)
- if CurrentClick="" and LastMoto<>CurrentMoto and CurrentMoto <>0 then
- if DirsAt = 0 then
- Temp=MoveDir
- else
- Temp=Dirs[DirsAt]
- endif
-
- if Temp=1 and CurrentMoto < 0 then
- Dirs[DirsAt]=SquareWidth
- DirsAt=DirsAt+1
- elseif Temp=1 and CurrentMoto > 0 then
- Dirs[DirsAt]=0-SquareWidth
- DirsAt=DirsAt+1
- elseif Temp=SquareWidth and CurrentMoto < 0 then
- Dirs[DirsAt]=-1
- DirsAt=DirsAt+1
- elseif Temp=SquareWidth and CurrentMoto > 0 then
- Dirs[DirsAt]=1
- DirsAt=DirsAt+1
- elseif Temp=-1 and CurrentMoto < 0 then
- Dirs[DirsAt]=0-SquareWidth
- DirsAt=DirsAt+1
- elseif Temp=-1 and CurrentMoto > 0 then
- Dirs[DirsAt]=SquareWidth
- DirsAt=DirsAt+1
- elseif Temp=0-SquareWidth and CurrentMoto < 0 then
- Dirs[DirsAt]=1
- DirsAt=DirsAt+1
- elseif Temp=0-SquareWidth and CurrentMoto > 0 then
- Dirs[DirsAt]=-1
- DirsAt=DirsAt+1
- endif
- endif
- LastMoto=CurrentMoto
-
- '蛇
- if MoveStep=0 then
-
-
- if SnakeHead+MoveDir<0 then '上
- GameOver=1
- elseif SnakeHead+MoveDir>=SnakeMax then '下
- GameOver=1
- elseif MoveDir=-1 and Math.Remainder(SnakeHead,SquareWidth)=0 then '左
- GameOver=1
- elseif MoveDir=1 and Math.Remainder(SnakeHead,SquareWidth)=SquareWidth-1 then '右
- GameOver=1
- endif
-
- SnakeHead=SnakeHead+MoveDir
-
-
- if Square[SnakeHead]=0 then
- elseif Square[SnakeHead]=5 then
- score=score+Leave
- LCD.Text (1, 0, 1, 1,"Leave: "+Leave+" score: "+score)
- EatingApple=1
- '把Apple清掉
- LCD.FillCircle (0,Math.Remainder(SquareApple,SquareWidth)*7+4, Math.Floor(SquareApple / SquareWidth)*7+17, 3)
- SnakeLength=SnakeLength+1
- AppleRand()
- else
- GameOver=1
- endif
-
-
- if EatingApple=0 then
- TailDir=Square[SnakeTail]
- Square[SnakeTail]=0
- endif
- DrawSnake()
-
- if TailDir=1 then
- LCD.Line (0,DrawX-2,DrawY-(MoveStep-3), DrawX+2, DrawY-(MoveStep-3))
- LCD.Line (0,DrawX-2,DrawY-(3-MoveStep), DrawX+2, DrawY-(3-MoveStep))
- elseif TailDir=-1 then
- LCD.Line (0,DrawX-2,DrawY-(MoveStep-3), DrawX+2, DrawY-(MoveStep-3))
- LCD.Line (0,DrawX-2,DrawY-(3-MoveStep), DrawX+2, DrawY-(3-MoveStep))
- elseif TailDir=SquareWidth then
- LCD.Line (0,DrawX-(3-MoveStep),DrawY-2, DrawX-(3-MoveStep), DrawY+2)
- LCD.Line (0,DrawX-(MoveStep-3),DrawY-2, DrawX-(MoveStep-3), DrawY+2)
- elseif TailDir=0-SquareWidth then
- LCD.Line (0,DrawX-(3-MoveStep),DrawY-2, DrawX-(3-MoveStep), DrawY+2)
- LCD.Line (0,DrawX-(MoveStep-3),DrawY-2, DrawX-(MoveStep-3), DrawY+2)
- endif
-
-
- elseif MoveStep=6 then
- '读取并设定方向
- if Dirs[0]<>0 and Dirs[0]+MoveDir<>0 then
- MoveDir=Dirs[0]
- for i=0 to 8 step 1
- Dirs[i]=Dirs[i+1]
- EndFor
- Dirs[9]=0
- if DirsAt > 0 then
- DirsAt=DirsAt-1
- endif
- endif
-
- '设置蛇头蛇尾
- DrawSnake()
- Square[SnakeHead]=MoveDir
- if EatingApple=0 then
- Square[SnakeTail]=0
- SnakeTail=SnakeTail+TailDir
- endif
- EatingApple=0
- else
- DrawSnake()
- endif
-
- '延时,不同leave 不同延时时间
- WaitByLeave()
-
-
- MoveStep=MoveStep+1
- if MoveStep=7 then
- MoveStep=0
- endif
- endwhile
-
- '游戏结束
- LCD.clear()
- CurrentClick=""
-
- LastTime=0
- LCD.Text (1, 15, 50, 2,"GAME OVER")
- '分数居中
- if Math.Floor(score/1000)>=1 then
- LCD.Text (1, 40, 70, 1,"score: "+score)
- elseif Math.Floor(score/100)>=1 then
- LCD.Text (1, 44, 70, 1,"score: "+score)
- elseif Math.Floor(score/10)>=1 then
- LCD.Text (1, 48, 70, 1,"score: "+score)
- else
- LCD.Text (1, 52, 70, 1,"score: "+score)
- endif
- LCD.Text (1, 47, 100, 1,"Try Again")
-
- while CurrentClick <> "E"'重新开始
- CurrentClick =Buttons.Current
- LastTime=LastTime+1
- if LastTime = 20 then
- LastTime=0
- endif
-
- if LastTime<10 then
- LCD.Text (1, 42, 90, 1,"PRESS ENTER")
- else
- LCD.FillRect (0,42, 90,88 , 8)
- endif
- Program.Delay (50)
- EndWhile
- endwhile
- sub LeaveChange
- if CurrentClick = "R" or CurrentClick = "D" then
- if Leave<7 then
- Leave=leave+1
- endif
- elseif CurrentClick = "L" or CurrentClick = "U" then
- if Leave>1 then
- Leave=leave-1
- endif
- endif
- LCD.Text (1, 28, 80, 2,"Leave "+Leave)
- endsub
- sub Initialization
- LCD.Clear ()
- score=0
- LCD.Text (1, 0, 1, 1,"Leave: "+Leave+" score: "+score)
- LCD.Rect (1, 0, 13, 178,128-14)
- if show=1 then
- '调整观察游戏界面方格
- For i=0 to 180 step 7
- for j=0 to 128-21 step 7
- LCD.FillRect (1,i+2, j+15, 5, 5)
- EndFor
- EndFor
- '游戏方格总数25*16=400
- endif
-
-
- For i=0 to SnakeMax step 1
- Square[i]=0
- EndFor
-
- Square[8*SquareWidth+3]=1'第9行 第4格
- Square[8*SquareWidth+4]=1
- Square[8*SquareWidth+5]=1
- Square[8*SquareWidth+6]=1
- MoveDir=1
-
- SnakeLength=4
- SnakeHead=8*SquareWidth+6
- SnakeTail=8*SquareWidth+3
- MoveStep=0
-
- DirsAt=0
- LCD.FillRect (1,7*3+2-1, 7*8+15, 28, 5)
- AppleRand()
-
- endsub
- sub AppleRand
- if SnakeLength<SnakeMax then
- RandNum=Math.GetRandomNumber (SnakeMax-SnakeLength)
- i=-1
- j=0
- while j <> randNum
- i=i+1
- if Square[i]=0 then
- j=j+1
- endif
- endwhile
- Square[i]=5
- SquareApple=i
- DrawApple()
- else '已经最长了
- LCD.Clear ()
- LCD.Text (1, 30, 50, 2,"You Win")
- while 0<1
- endwhile
- EndIf
- endsub
- sub DrawApple '根据SquareApple画出Apple的位置
- LCD.FillCircle (1,Math.Remainder(SquareApple,SquareWidth)*7+4, Math.Floor(SquareApple / SquareWidth)*7+17, 3)
- endsub
- sub DrawSnake
-
- DrawX=Math.Remainder(SnakeHead,SquareWidth)*7+4
- DrawY=Math.Floor(SnakeHead / SquareWidth)*7+17
- if MoveDir=1 then
- LCD.Line (1,DrawX-(3-MoveStep),DrawY-2, DrawX-(3-MoveStep), DrawY+2)
- elseif MoveDir=-1 then
- LCD.Line (1,DrawX-(MoveStep-3),DrawY-2, DrawX-(MoveStep-3), DrawY+2)
- elseif MoveDir=0-SquareWidth then
- LCD.Line (1,DrawX-2,DrawY-(MoveStep-3), DrawX+2, DrawY-(MoveStep-3))
- elseif MoveDir=SquareWidth then
- LCD.Line (1,DrawX-2,DrawY-(3-MoveStep), DrawX+2, DrawY-(3-MoveStep))
- Endif
- if EatingApple=1 then
- if MoveDir=1 then
- LCD.Line (1,DrawX-(3-MoveStep)-1,DrawY-2, DrawX-(3-MoveStep)-1, DrawY+2)
- elseif MoveDir=-1 then
- LCD.Line (1,DrawX-(MoveStep-3)+1,DrawY-2, DrawX-(MoveStep-3)+1, DrawY+2)
- elseif MoveDir=0-SquareWidth then
- LCD.Line (1,DrawX-2,DrawY-(MoveStep-3)+1, DrawX+2, DrawY-(MoveStep-3)+1)
- elseif MoveDir=SquareWidth then
- LCD.Line (1,DrawX-2,DrawY-(3-MoveStep)-1, DrawX+2, DrawY-(3-MoveStep)-1)
- Endif
-
-
- endif
-
-
-
- if EatingApple=0 then
- DrawX=Math.Remainder(SnakeTail,SquareWidth)*7+4
- DrawY=Math.Floor(SnakeTail / SquareWidth)*7+17
- if TailDir=1 then
- LCD.Line (0,DrawX-(3-MoveStep),DrawY-2, DrawX-(3-MoveStep), DrawY+2)
- elseif TailDir=-1 then
- LCD.Line (0,DrawX-(MoveStep-3),DrawY-2, DrawX-(MoveStep-3), DrawY+2)
- elseif TailDir=0-SquareWidth then
- LCD.Line (0,DrawX-2,DrawY-(MoveStep-3), DrawX+2, DrawY-(MoveStep-3))
- elseif TailDir=SquareWidth then
- LCD.Line (0,DrawX-2,DrawY-(3-MoveStep), DrawX+2, DrawY-(3-MoveStep))
- Endif
- endif
- EndSub
- sub WaitByLeave
- while EV3.Time-LastTime < 280-30*Leave
- endWhile
- LastTime=EV3.Time
- endsub
复制代码
|