VB课程设计九格游戏.doc

上传人:文库蛋蛋多 文档编号:2389318 上传时间:2023-02-17 格式:DOC 页数:22 大小:139KB
返回 下载 相关 举报
VB课程设计九格游戏.doc_第1页
第1页 / 共22页
VB课程设计九格游戏.doc_第2页
第2页 / 共22页
VB课程设计九格游戏.doc_第3页
第3页 / 共22页
VB课程设计九格游戏.doc_第4页
第4页 / 共22页
VB课程设计九格游戏.doc_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《VB课程设计九格游戏.doc》由会员分享,可在线阅读,更多相关《VB课程设计九格游戏.doc(22页珍藏版)》请在三一办公上搜索。

1、VB课程设计摘要: 随着计算机的普及和网络技术的飞速发展,人们的娱乐生活越来越丰富,一些小游戏也逐渐成为了人们休闲娱乐生活的一部分。九格是一种老少皆宜、容易上手的益智类小游戏。本游戏采用Visual Basic编程语言完成了该程序的编写,实现了游戏功能。关键词: Visual Basic 小游戏 益智 Abstract: With the popularity of computer and network technology development, Peoples entertainment is more and more rich. Some games have gradually

2、 become part of peoples leisure life. Nine grid is an all ages, easy to use puzzle game. The game uses Visual Basic programming language to complete the preparation process to achieve the game features.Keywords: Visual Basic Small games Puzzle一、程序原理 本游戏采用Visual Basic 编程语言进行游戏编程。Visual Basic是由美国微软公司于

3、1991年开发的一种可视化的、面向对象和采用事件驱动方式的结构化高级程序设计语言,可用于开发 Windows 环境下的各类应用程序。它简单易学、效率高,且功能强大可以与 Windows 专业开发工具SDK相媲美。在Visual Basic环境下,利用事件驱动的编程机制、新颖易用的可视化设计工具,使用Windows内部的广泛应用程序接口(API)函数,动态链接库(DLL)、对象的链接与嵌入(OLE)、开放式数据连接(ODBC)等技术,可以高效、快速地开发Windows环境下功能强大、图形界面丰富的应用软件系统。 在VB中必须熟知以下VB专用术语。控件简单的说,控件就是构成或者说建造Visual

4、Basic应用程序的图形化工具,包括窗体、按钮、复选框、列表框、数据控件、表格控件和图片控件等等.。事件由用户或操作系统引发的动作。事件的示例有击键、单击鼠标(Click)、双击鼠标(DblClick)、一段时间的限制,或从端口接收数据。方法嵌入在对象定义中的程序代码,它定义对象怎样处理信息并响应某事件。例如,数据库对象有打开纪录集并从一个记录移动到另一个记录的方法程序的基本元素,它含有定义其特征的属性,定义其任务和识别它可以响应的事件的方法。控件和窗体是Visual Basic中所有对象的示例。对象一个控件、窗体等都可被看作一个对象。过程为完成某些特定的任务而编写的代码段,过程通常用于响应特

5、定的事件,也可以当作应用程序的用户自定义函数来使用。属性属性是组成用户界面的各对象的性质的具体描述。例如上述“对象”中所提到的尺寸、位置、颜色、宽度、高度等等都称为控件的 属性。属性决定对象的外观,有时也决定对象的行为。对象的属性绝大部分是VB中已经事先定义好的,但也有的属性是需要在应用 过程中才去定义的。属性即可为对象提供数据,也能从对象取回信息。二、窗体介绍本程序只有一个窗体。1.当程序未运行时图1窗体中包含:游戏窗口(Form)、 游戏提示说明(Label)、游戏显示(Picture)、 秒数控制(Timer)、时间显示(text)游戏说明窗口:图22.当程序运行时在游戏选项中,包含开始

6、游戏、游戏模式、退出游戏三种选择。在游戏模式中选择人机游戏则如图所示:图2这时游戏者自动选择的是X,电脑选择的是0。在限时5秒内进行游戏,否则电脑胜利。下图是游戏者胜利时的窗体显示。图33.在游戏模式中选择双人游戏,可以两个人一起玩。在5秒内谁先排成一条直线,则谁胜利。若两个人都没有排成一条直线,则平局。图4三、程序调试源代码写完后,对程序进行调试运行,要求变量声明,对可能出现的几种错误,如:编辑时错误、编译时错误、运行时错误、逻辑错误,进行修改,调试,运行,直至程序能正常运行。但是在一些细节方面仍然需要完善,比如在游戏中可以加入一些声音的提示,在游戏完成和失败的时候可以弹出一些小的Flash

7、动画等等。总的来说本次设计在功能上已经基本达到要求,其他细节方面有待以后完善。四、收获体会Visual Basic 语言是以结构化Basic语言为基础,以事件驱动为运行机制。它的诞生标志着元件设计和开发的新时代的开始。VB具有面向对象可视化设计工具,是事件驱动的编程机制,同时也提供了易学易用的应用程序集成开发环境。从Visual basic 语言的学习到编程,当时很想自己能够熟练的进行程序编辑,工程上的软件编辑。随着Visual basic 语言的学习才逐渐发现这理想想要实现的难度。对我而言学习编程,不仅能够帮自己解决在生活中可能遇到的一些问题;自己设计一些小游戏还可以给自己带来一些乐趣。同时

8、也为自己在寻找理想工作添加重要的砝码。所以,我觉得在学习VB是我们要明确学习的目的。另外我觉得个人的数学基础,思维逻辑能力对VB的学习有着至关重要的影响。而在学习的过程中最重要的就是理解,正如老师在课上经常对我们所讲的,不要始终照搬书上已有的代码,要学会理解、掌握编程的思想与方法,这样才能融会贯通,只有在理解方法的基础上学习才是最有效的学习。Visual Basic 语言的学习要多实践,多交流。掌握编程方法必须在编程实际工作中去实践和体会。在学习初期要经常自己动手设计程序,不要拘泥于固定的思维方式,遇到问题要多想几种解决方案。而这也要求与同学多多的交流,毕竟每个人的思维方法不同,角度各异。通过

9、交流可以不断的吸收他人的长处,丰富编程实践,从而提高自己的水平。亲自动手实践试创造性思维应用的体现,也是培养逻辑思维的好方法。五、致谢在这次完成VB课程设计的过程中,我深刻意识到VB在实际应用中的重要性。感谢老师在这一学期辛勤教导。老师在课堂上认真详细的讲解使我对VB有非常深刻的理解。通过这一学期的学习及这次VB课程设计,我基本掌握了VB知识。感谢同学还有朋友给了我很大的帮助,没有他们的帮助,我也不能很顺利完成这次论文,在此对他们给予我最真心的感谢!这次的课程设计对我的学习有很大的提高,使我在今后找工作中又有了一个有力砝码。六、参考文献1刘炳文.精通Visual Basic 6.0中文版.北京

10、:电子工业出版社,1999年7月2田文胜,刘阳,学勤. Visual Basic编程指南.北京:清华大学出版社,2003年2月3王祖卫,李伟.Visual Basic 程序设计.天津:南开大学出版社,2004年4牛又奇,孙建国.新编Visual Basic程序设计教程.苏州:苏州大学出版社,2004年1月5 范晓平编著.Visual Basic 6.0软件开发项目实训. 北京:海洋出版社.20026 唐兵、李桂花等编著.Visual Basic中文版程序设计教程.北京:机械工业出版社7 松桥工作室编著.深入浅出Visual Basic 6.0程序设计.北京:中国铁道出版社附录:源程序代码Dim

11、 topLeft As IntegerDim topMiddle As IntegerDim topRight As IntegerDim midLeft As IntegerDim center As IntegerDim midRight As IntegerDim bottomLeft As IntegerDim bottomMiddle As IntegerDim bottomRight As IntegerDim xScore As IntegerX赢的次数Dim oScore As IntegerO赢的次数Dim draw As Integer平局次数Dim turnTime As

12、 Single游戏时间Dim maxTime As Single游戏允许的最大时间Dim onePlayer As Boolean单人模式则为trueDim xTurn As Boolean如果Xs为ture则为trueDim gameOver As Boolean如果游戏结束则为true,除非某一方赢。Private Sub mnuExit_Click()退出游戏 EndEnd SubPrivate Sub mnuNewGame_Click() If Val(txtTimeLimit.Text) 0 Then maxTime = Val(txtTimeLimit.Text) Else txt

13、TimeLimit.Text = maxTime End If pic1.Cls pic2.Cls pic3.Cls pic4.Cls pic5.Cls pic6.Cls pic7.Cls pic8.Cls pic9.Cls topLeft = 0 topMiddle = 0 topRight = 0 midLeft = 0 center = 0 midRight = 0 bottomLeft = 0 bottomMiddle = 0 bottomRight = 0 xTurn = True gameOver = False lblMesg = X下子 pic1.FontSize = 8 pi

14、c2.FontSize = 8 pic3.FontSize = 8 pic4.FontSize = 8 pic5.FontSize = 8 pic6.FontSize = 8 pic7.FontSize = 8 pic8.FontSize = 8 pic9.FontSize = 8 tmrTimer.Enabled = True turnTime = 0 Dim i As Integer For i = 1 To 9 taken(i) = False Next iEnd SubPrivate Sub Form_Load() Randomize topLeft = 0 topMiddle = 0

15、 topRight = 0 midLeft = 0 center = 0 midRight = 0 bottomLeft = 0 bottomMiddle = 0 bottomRight = 0 xTurn = True gameOver = True lblMesg = 开始新游戏 lblPlayers.Caption = 人机游戏 onePlayer = True txtTimeLimit.Text = 5 maxTime = Val(txtTimeLimit.Text) turnTime = 0 xScore = 0 oScore = 0 draw = 0 lblX.Caption =

16、X赢: + Str(xScore) lblO.Caption = O赢: + Str(oScore) lblDraw.Caption = 平局: + Str(draw) Dim i As Integer For i = 1 To 9 taken(i) = False Next iEnd SubPrivate Sub pic1_Click() If Not gameOver Then If topLeft = 0 Then taken(1) = True turnTime = 0 If xTurn Then pic1.Print ; pic1.FontSize = 36 pic1.Print X

17、 xTurn = False topLeft = 1 If Not TTT Then lblMesg = O下子 End If If Not gameOver And onePlayer Then Call compPlay End If Else pic1.Print ; pic1.FontSize = 36 pic1.Print O xTurn = True topLeft = 2 If Not TTT Then lblMesg = X下子 End If End If End If End IfEnd SubPrivate Sub pic2_Click() If Not gameOver

18、Then If topMiddle = 0 Then taken(2) = True turnTime = 0 If xTurn Then pic2.Print ; pic2.FontSize = 36 pic2.Print X xTurn = False topMiddle = 1 If Not TTT Then lblMesg = O下子 End If If Not gameOver And onePlayer Then Call compPlay End If Else pic2.Print ; pic2.FontSize = 36 pic2.Print O xTurn = True t

19、opMiddle = 2 If Not TTT Then lblMesg = X下子 End If End If End If End IfEnd SubPrivate Sub pic3_Click() If Not gameOver Then If topRight = 0 Then taken(3) = True turnTime = 0 If xTurn Then pic3.Print ; pic3.FontSize = 36 pic3.Print X xTurn = False topRight = 1 If Not TTT Then lblMesg = O下子 End If If N

20、ot gameOver And onePlayer Then Call compPlay End If Else pic3.Print ; pic3.FontSize = 36 pic3.Print O xTurn = True topRight = 2 If Not TTT Then lblMesg = X下子 End If End If End If End IfEnd SubPrivate Sub pic4_Click() If Not gameOver Then If midLeft = 0 Then taken(4) = True turnTime = 0 If xTurn Then

21、 pic4.Print ; pic4.FontSize = 36 pic4.Print X xTurn = False midLeft = 1 If Not TTT Then lblMesg = O下子 End If If Not gameOver And onePlayer Then Call compPlay End If Else pic4.Print ; pic4.FontSize = 36 pic4.Print O xTurn = True midLeft = 2 If Not TTT Then lblMesg = X下子 End If End If End If End IfEnd

22、 SubPrivate Sub pic5_Click() If Not gameOver Then If center = 0 Then taken(5) = True turnTime = 0 If xTurn Then pic5.Print ; pic5.FontSize = 36 pic5.Print X xTurn = False center = 1 If Not TTT Then lblMesg = O下子 End If If Not gameOver And onePlayer Then Call compPlay End If Else pic5.Print ; pic5.Fo

23、ntSize = 36 pic5.Print O xTurn = True center = 2 If Not TTT Then lblMesg = X下子 End If End If End If End IfEnd SubPrivate Sub pic6_Click() If Not gameOver Then If midRight = 0 Then taken(6) = True turnTime = 0 If xTurn Then pic6.Print ; pic6.FontSize = 36 pic6.Print X xTurn = False midRight = 1 If No

24、t TTT Then lblMesg = O下子 End If If Not gameOver And onePlayer Then Call compPlay End If Else pic6.Print ; pic6.FontSize = 36 pic6.Print O xTurn = True midRight = 2 If Not TTT Then lblMesg = X下子 End If End If End If End IfEnd SubPrivate Sub pic7_Click() If Not gameOver Then If bottomLeft = 0 Then tak

25、en(7) = True turnTime = 0 If xTurn Then pic7.Print ; pic7.FontSize = 36 pic7.Print X xTurn = False bottomLeft = 1 If Not TTT Then lblMesg = O下子 End If If Not gameOver And onePlayer Then Call compPlay End If Else pic7.Print ; pic7.FontSize = 36 pic7.Print O xTurn = True bottomLeft = 2 If Not TTT Then

26、 lblMesg = X下子 End If End If End If End IfEnd SubPrivate Sub pic8_Click() If Not gameOver Then If bottomMiddle = 0 Then taken(8) = True turnTime = 0 If xTurn Then pic8.Print ; pic8.FontSize = 36 pic8.Print X xTurn = False bottomMiddle = 1 If Not TTT Then lblMesg = O下子 End If If Not gameOver And oneP

27、layer Then Call compPlay End If Else pic8.Print ; pic8.FontSize = 36 pic8.Print O xTurn = True bottomMiddle = 2 If Not TTT Then lblMesg = X下子 End If End If End If End IfEnd SubPrivate Sub pic9_Click() If Not gameOver Then If bottomRight = 0 Then taken(9) = True turnTime = 0 If xTurn Then pic9.Print

28、; pic9.FontSize = 36 pic9.Print X xTurn = False bottomRight = 1 If Not TTT Then lblMesg = O下子 End If If Not gameOver And onePlayer Then Call compPlay End If Else pic9.Print ; pic9.FontSize = 36 pic9.Print O xTurn = True bottomRight = 2 If Not TTT Then lblMesg = X下子 End If End If End If End IfEnd Sub

29、Private Function TTT() As Boolean If topLeft = 1 _ And topMiddle = 1 _ And topRight = 1 _ Or midLeft = 1 _ And center = 1 _ And midRight = 1 _ Or bottomLeft = 1 _ And bottomMiddle = 1 _ And bottomRight = 1 _ Or topLeft = 1 _ And midLeft = 1 _ And bottomLeft = 1 _ Or topMiddle = 1 _ And center = 1 _

30、And bottomMiddle = 1 _ Or topRight = 1 _ And midRight = 1 _ And bottomRight = 1 _ Or topLeft = 1 _ And center = 1 _ And bottomRight = 1 _ Or topRight = 1 _ And center = 1 _ And bottomLeft = 1 Then gameOver = True xScore = xScore + 1 lblX.Caption = X赢: + Str(xScore) lblMesg.Caption = X赢! ElseIf topLe

31、ft = 2 _ And topMiddle = 2 _ And topRight = 2 _ Or midLeft = 2 _ And center = 2 _ And midRight = 2 _ Or bottomLeft = 2 _ And bottomMiddle = 2 _ And bottomRight = 2 _ Or topLeft = 2 _ And midLeft = 2 _ And bottomLeft = 2 _ Or topMiddle = 2 _ And center = 2 _ And bottomMiddle = 2 _ Or topRight = 2 _ A

32、nd midRight = 2 _ And bottomRight = 2 _ Or topLeft = 2 _ And center = 2 _ And bottomRight = 2 _ Or topRight = 2 _ And center = 2 _ And bottomLeft = 2 Then gameOver = True oScore = oScore + 1 lblO.Caption = O赢: + Str(oScore) lblMesg.Caption = O赢! ElseIf allFull Then gameOver = True lblMesg.Caption =

33、平局! draw = draw + 1 lblDraw.Caption = 平局: + Str(draw) End If TTT = gameOverEnd FunctionPrivate Function allFull() As Boolean If topLeft = 0 _ Or topMiddle = 0 _ Or topRight = 0 _ Or midLeft = 0 _ Or center = 0 _ Or midRight = 0 _ Or bottomLeft = 0 _ Or bottomMiddle = 0 _ Or bottomRight = 0 Then allF

34、ull = False Else allFull = True End IfEnd FunctionPrivate Sub compPlay() Select Case compWinPosition Case 1 Call pic1_Click Case 2 Call pic2_Click Case 3 Call pic3_Click Case 4 Call pic4_Click Case 5 Call pic5_Click Case 6 Call pic6_Click Case 7 Call pic7_Click Case 8 Call pic8_Click Case 9 Call pic

35、9_Click Case 0 如果平局 Dim choice As Integer Do choice = Int(Rnd * 9) + 1 Loop Until Not taken(choice) Select Case choice Case 1 Call pic1_Click Case 2 Call pic2_Click Case 3 Call pic3_Click Case 4 Call pic4_Click Case 5 Call pic5_Click Case 6 Call pic6_Click Case 7 Call pic7_Click Case 8 Call pic8_Cli

36、ck Case 9 Call pic9_Click End Select End Select xTurn = True If Not gameOver Then lblMesg.Caption = X下子 End IfEnd SubPrivate Sub tmrTimer_Timer() If Not gameOver Then lblTime.Caption = Time: + FormatNumber(turnTime, 1) turnTime = turnTime + 0.1 End If If turnTime = maxTime Then lblTime.Caption = Tim

37、e: + FormatNumber(maxTime, 1) gameOver = True tmrTimer.Enabled = False If xTurn Then oScore = oScore + 1 lblO.Caption = O赢: + Str(oScore) lblMesg.Caption = O赢! Else xScore = score + 1 lblX.Caption = X赢: + Str(xScore) lblMesg.Caption = X赢! End If End IfEnd SubPrivate Function compWinPosition() As Int

38、eger If topLeft = 0 And (topMiddle = 2 _ And topRight = 2 _ Or center = 2 _ And bottomRight = 2 _ Or midLeft = 2 _ And bottomLeft = 2) Then compWinPosition = 1 ElseIf topMiddle = 0 _ And (topLeft = 2 _ And topRight = 2 _ Or center = 2 _ And bottomMiddle = 2) Then compWinPosition = 2 ElseIf topRight = 0 _ And (topLeft = 2 _ And topMiddle = 2 _ Or bottomLeft = 2 _ And center = 2 _ Or midRight = 2 _ And bottomRight = 2) Then compWinPosition = 3 ElseIf midLeft = 0 _ And (topLeft = 2 _ A

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号