您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > (完整版)vb小游戏代码
数字排序小游戏OptionExplicitDimLabel2XAsInteger'记录标签控件数组中要移动的标签控件左上角X的位置DimLabel2YAsInteger'记录标签控件数组中要移动的标签控件左上角Y的位置'让标签数组中的每个标签控件上显示的数字是随机的,无重复的PrivateSubInit()RandomizeDima(7)AsIntegerDimiAsInteger,kAsIntegerLabel1.Caption=Fori=0To7a(i)=iNextFori=0To7k=Int(Rnd*8)DoWhilea(k)=-1'a(k)=-1表示该数组元素对应的数字已经被使用过了k=Int(Rnd*8)'重新生成k的值,直到a(k)的值不等于-1LoopLabel2(i).Caption=Trim(Str(a(k)))a(k)=-1'a(k)的值已经使用了,不能再用,重新赋值为-1与其他的元素值相区别NextiEndSubPrivateSubCommand1_Click()DimxAsInteger,yAsIntegerDimzAsIntegerInitPicture1.Enabled=True'让空白标签Label1出现的位置随机Randomize'记录下空白标签Label1的位置x=Label1.Lefty=Label1.Topz=Int(Rnd*8)'将空白标签Label1和标签控件数组任一控件交换位置Label1.MoveLabel2(z).Left,Label2(z).TopLabel2(z).Movex,yCommand1.Enabled=FalseEndSubPrivateSubCommand2_Click()EndEndSubPrivateSubForm_Load()DimiAsIntegerPicture1.Enabled=False'在标签中显示游戏说明信息Label3.Caption=如左图所示,将数字按0-7顺&vbCrLf&vbCrLf&序依次排列,即取得胜利。'在标签中显示排列规则后的数字顺序Label1.Caption=0Fori=0To6Label2(i).Caption=i+1NextEndSubPrivateSubLabel1_DragDrop(SourceAsControl,xAsSingle,yAsSingle)DimLabel1XAsInteger'记录空白控件Label1左上角X的位置DimLabel1YAsInteger'记录空白控件Label1左上角Y的位置Dimflag(3)AsBoolean'获取空白控件Label1的位置Label1X=Label1.LeftLabel1Y=Label1.Top'要移动的控件位于空白控件Label1的正左侧flag(0)=(Label2X=Label1X-Source.Width)And(Label2Y=Label1Y)'要移动的控件位于空白控件Label1的正右侧flag(1)=(Label2X=Label1X+Source.Width)And(Label2Y=Label1Y)'要移动的控件位于空白控件Label1的正上方flag(2)=(Label2X=Label1X)And(Label2Y=Label1Y-Source.Height)'要移动的控件位于空白控件Label1的正下方flag(3)=(Label2X=Label1X)And(Label2Y=Label1Y+Source.Height)Ifflag(0)Orflag(1)Orflag(2)Orflag(3)ThenLabel1.MoveLabel2X,Label2YSource.MoveLabel1X,Label1YEndIfWinEndSubPrivateSubLabel2_MouseDown(IndexAsInteger,ButtonAsInteger,ShiftAsInteger,xAsSingle,yAsSingle)IfButton=vbLeftButtonThen'如果按下鼠标左键'记录下要拖动控件的位置Label2X=Label2(Index).LeftLabel2Y=Label2(Index).TopLabel2(Index).Drag1'启动拖动操作EndIfEndSubPrivateSubLabel2_MouseUp(IndexAsInteger,ButtonAsInteger,ShiftAsInteger,xAsSingle,yAsSingle)Label2(Index).Drag2'结束拖动操作EndSubPrivateSubWin()DimwinnerAsIntegerDimiAsIntegerDimanswerAsInteger'对于给定的标签控件数组中的任一标签控件,可以落在符合要求(对应位置应显示对应数字)'的八个位置中的任一位置'利用循环语句对标签控件数组中的每个标签控件进行检查,如果其落在某一符号要求的位置,'则变量winner的值加1,如果所有标签控件都落在符号要求的位置,则变量winner的值应为8Fori=0To7IfLabel2(i).Left=0AndLabel2(i).Top=0And_Label2(i).Caption=0Thenwinner=winner+1ElseIfLabel2(i).Left=Label2(i).WidthAndLabel2(i).Top=0And_Label2(i).Caption=1Thenwinner=winner+1ElseIfLabel2(i).Left=2*Label2(i).WidthAndLabel2(i).Top=0And_Label2(i).Caption=2Thenwinner=winner+1ElseIfLabel2(i).Left=0AndLabel2(i).Top=Label2(i).HeightAnd_Label2(i).Caption=3Thenwinner=winner+1ElseIfLabel2(i).Left=Label2(i).WidthAndLabel2(i).Top=Label2(i).HeightAnd_Label2(i).Caption=4Thenwinner=winner+1ElseIfLabel2(i).Left=2*Label2(i).WidthAndLabel2(i).Top=Label2(i).HeightAnd_Label2(i).Caption=5Thenwinner=winner+1ElseIfLabel2(i).Left=0AndLabel2(i).Top=2*Label2(i).HeightAnd_Label2(i).Caption=6Thenwinner=winner+1ElseIfLabel2(i).Left=Label2(i).WidthAndLabel2(i).Top=2*Label2(i).HeightAnd_Label2(i).Caption=7Thenwinner=winner+1EndIfNextiIfwinner=8ThenMsgBox恭喜您,胜利了!,0+64+0,提示Picture1.Enabled=Falseanswer=MsgBox(还继续吗?,4+32+0,提示)Ifanswer=vbYesThenCommand1.Enabled=TrueElseEndEndIfEndIfEndSub弹球游戏Dimx_stepAsIntegerDimy_stepAsIntegerPrivateSubcommand1_Click()IfTimer1.Enabled=TrueThenTimer1.Enabled=FalseElseTimer1.Enabled=TrueEndIfIfcommand1.Caption=暂停Thencommand1.Caption=继续Elsecommand1.Caption=暂停EndIfEndSubPrivateSubForm_Load()x_step=200y_step=200EndSubPrivateSubPicture1_KeyDown(KeyCodeAsInteger,ShiftAsInteger)IfKeyCode=37ThenIfLine1.X10ThenLine1.X1=0:Line1.X2=2000ElseLine1.X1=Line1.X1-100:Line1.X2=Line1.X2-100EndIfEndIfIfKeyCode=39ThenIfLine1.X1Picture1.WidthThenLine1.X1=Picture1.Width-2000:line2.X2=Picture.WidthElseLine1.X1=Line1.X1+100:Line1.X2=Line1.X2+100EndIfEndIfEndSubPrivateSubTimer1_Timer()IfShape1.Top0ThenShape1.Top=0:y_step=-y_stepEndIfIfShape1.Left0ThenShape1.Left=0x_step=-x_stepEndIfIfShape1.LeftPicture1.Width-Shape1.WidthThenShape1.Left=Picture1.Width-Shape1.Widthx_step=-x_stepEndIfIfShape1.Left=Line1.X1AndShape1.Left=Line1.X2AndShape1.Top=Line1.Y1-Shape1.HeightThenShape1.Top=Line1.Y1-Shape1.Heighty_step=-y_step*1.01x_step=x_step*1.01Label2.Caption=Label2.Caption+1EndIfShape1.Top=Shape1.Top+y_stepShape1.Left=Shape1.Left+x_stepIfShape1.Top=Picture1.Height-Shape1.HeightThenMsgBox游戏结束command1.Caption=开始Timer1.Enabled=FalseShape1.Top=1000Label2.Caption=0EndIfEndSub打字游戏DimscoreAsIntegerDimspeedAsIntegerDimtypetimeAsIntegerPrivateSubinit()Randomizelblletter1.Caption=Chr(Int(Rnd*42)+48)lblletter1.Left=Int(Rnd*2800)+1lblletter1.Top=0EndSubPrivateSubinit1()Randomizelblletter2.Caption=Chr(Int(Rnd*25)+97)lblletter2.Left=Int(Rnd*2800)+1lblletter2.Top=0EndSubPrivateSubCommand1_Click()score=Int(lblscore.Text)initinit1Timer1=TrueTimer2=TrueHScroll1.Enabled=FalseCommand1.Enabled=FalseCommand2.Enabled=FalseHScroll1.Enabled=FalseIflbltime.Text=0ThenTimer1=FalseTimer2=Falselblletter1.Caption=lblletter2.Caption=EndIfEndSubPrivateSubCommand2_Click()typetime=InputBox(请输入打字时间。,时间设置)Iftypetime=0Thenlbltime.Text=60EndIflbltime.Text=typetimeEn
本文标题:(完整版)vb小游戏代码
链接地址:https://www.777doc.com/doc-8545749 .html