VB课程设计九格游戏_第1页
VB课程设计九格游戏_第2页
VB课程设计九格游戏_第3页
VB课程设计九格游戏_第4页
VB课程设计九格游戏_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

39、= 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 _ and bottomleft = 2 _ or center = 2 _ and midright = 2) then compwinposition = 4 elseif center = 0 _ and (topleft =

40、 2 _ and bottomright = 2 _ or bottomleft = 2 _ and topright = 2 _ or topmiddle = 2 _ and bottommiddle = 2 _ or midleft = 2 _ and midright = 2) then compwinposition = 5 elseif midright = 0 _ and (topright = 2 _ and bottomright = 2 _ or midleft = 2 _ and center = 2) then compwinposition = 6 elseif bot

41、tomleft = 0 _ and (topleft = 2 _ and midleft = 2 _ or center = 2 _ and topright = 2 _ or bottommiddle = 2 _ and bottomright = 2) then compwinposition = 7 elseif bottommiddle = 0 _ and (topmiddle = 2 _ and center = 2 _ or bottomleft = 2 _ and bottomright = 2) then compwinposition = 8 elseif bottomright = 0 _ and (topleft = 2 _ and center = 2 _ or topright = 2 _ and midright = 2 _ or bottomleft = 2 _ and bottommiddle = 2) then compwinposition = 9 else compwinposition = xwinposition end ifend functionprivate func

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论