超级玛丽--Java课程设计
超级玛丽JAVA小游戏详细设计文档

无关,需要表示层调用定义好的接口与逻辑层的通信 数据持久层。保存游戏中的数据,载入游戏中需要的资源,与程序逻辑层无
关,用 XML 文档存储数据
2.7 程序的三层结构图
表现层(绘制屏幕)
逻辑层(程序的逻辑)
数 据 持 久 层 ( 数 据 存4储 )
不同的操作; 查询记录:查询五个最高游戏分数,排名由上往下递减。 游戏设置(包括音乐设置和按键设置):玩家可以设置游戏进行时的音乐开
或关以及修改相应的控制键。
2.6 程序的总体架构
本程序采用 3 层程序架构,业务表现层,业务逻辑层和数据持久层。 业务表现层。主要是游戏界面的绘制,通过指定的接口绘制,与游戏的逻辑
private void walk()
//人物走的动作
private boolean checkTileCollisionVertical() //检查与地图的纵
向碰撞
private boolean checkTileCollisionHorizon() //检查与地图的横
向碰撞
private void fire() //发射子弹
地图,以提高游戏的运行速度 5) 居于多线程,实现进度条
2.2 游戏功能划分
游戏的基本功能应该包括菜单处理,程序逻辑处理,持久性处理和挂起恢复处理。 关于菜单的处理,包括可以选择的进入游戏、保存、帮助和音效开关等。 程序的逻辑处理就是涵盖了程序中除菜单外几乎所有的功能。 持久性处理就是资源的载入和游戏的进度保存。
private boolean checkSpriteCollisionVertical() //检查纵向与
超级玛丽详细设计文档

超级玛丽详细设计文档超级玛丽JA V A小游戏详细设计目录1.导言 ............................................................................................ 错误!未定义书签。
1.1编写目的 .................................................................... 错误!未定义书签。
1.2项目范围 .................................................................... 错误!未定义书签。
1.3参考资料 .................................................................... 错误!未定义书签。
1.4缩写说明 .................................................................... 错误!未定义书签。
1.5术语定义 .................................................................... 错误!未定义书签。
2.功能需求 .................................................................................... 错误!未定义书签。
2.1界面设计技术............................................................ 错误!未定义书签。
2.2游戏功能划分............................................................ 错误!未定义书签。
超级玛丽JAVA小游戏概要设计文档

目录1.导言 (2)1.1编写目的 (2)1.2项目范围 (2)1.3参考资料 (2)1.4缩写说明 (2)1.5术语定义 (2)2.任务概述 (3)2.1目标 (3)2.2开发环境 (3)2.3运行硬件环境要求 (3)3.功能需求 (3)3.1游戏功能划分 (3)3.2功能划分的架构图 (4)3.3具体的功能模块 (4)3.4功能描述 (4)3.5程序的总体架构 (4)3.6程序的三层结构图 (5)4.面向对象设计 (5)4.1用例图 (5)4.2顺序图 (6)4.3活动图 (6)4.4主要类图 (7)4.5各个模块的方法或类名类表示 (7)4.6地图绘制中的坐标说明 (8)5.运行需求 (8)5.1界面设计技术 (8)5.2硬、软件接口要求 (8)5.3故障处理 (9)6.其它需求 (9)6.1可扩展性要求 (9)1.导言1.1编写目的该文档的目的是说明超级玛丽游戏的功能和设计方向、目标等描述游戏项目的概要设计。
其主要内容包括:游戏项目的功能简介,接口设计,模块设计,界面设计。
本文档预期读者包括:项目管理人员,编程人员,数据库设计人员,美工,测试人员。
1.2项目范围该文档定义了游戏的模块,结构单元,但未确定单元的实现方法,这部分内容将在详细设计/实现中确定。
1.3参考资料《软件工程案例教程》机械工业出版社韩万江编著《J2ME手机游戏开发技术详解》清华大学出版社李振鹏龚剑编著1.4缩写说明UML:Unified Modeling Language(统一建模语言)的缩写,是一个标准的建模语言。
1.5术语定义MIDP2.0定义:MIDP2.0也叫MIDP_NG,它的编号是JSR118。
MIDP2.0与1.0相比有很大提高,增加的特性包括:提供域安全模型,以允许对应用程序进行签名和论证;提供TCP、UDP网络接口;内置OTA;更好的用户界面;基本的声音API。
2.任务概述2.1目标实现超级玛丽游戏的主要功能,主人翁可以吃到蘑菇变大,吃到花可以发射子弹,有的水管可以下去,可以踩到乌龟,可以顶破砖块,实现大家熟悉的基本功能。
java超级玛丽游戏计算机毕业设计开题报告

8迟伟雅. Java语言简介,电脑与网络,23~25
9陈涵生. Java语言与Java技术,计算机工程,1997,27(5):3~6
10J. Martinez-Llario,M. Gonzalez-Alcaide.Design of a Java spatial extension for relational databases,The Journal of Systems and Software,2011 (84):2314~2323
17尚蔚.基于Java的多形式WEB视频监控系统的设计与实现,东华大学,学位论文,2010
18强锋科技. Java开发利器: Eclipse从入门到精通,清华大学出版社, 2005: 24~111
19宋全德.基于Java的Madlib自动化测试框架,计算机系统应用,2014,23(2):28~35
20(英)伊乐斯,克里普斯著,蔡黄辉,马文涛译.架构实战—软件架构设计的过程,机械工业出版社, 2010: 42~76
毕业设计开题报告
2.本课题要研究或解决的问题和拟采用的研究手段(途径):
一.系统开发环境
超级玛丽java游戏课程设计

超级玛丽java游戏课程设计一、课程目标知识目标:1. 学生能够理解Java编程语言的基本语法和结构,掌握面向对象编程的基本概念。
2. 学生能够运用Java编程实现游戏界面设计、角色控制、碰撞检测等基本功能。
3. 学生了解游戏开发中的坐标系统和动画原理,并能够将其应用于超级玛丽游戏设计中。
技能目标:1. 学生掌握使用Java编写程序的方法,培养编程思维和解决问题的能力。
2. 学生能够运用面向对象的设计思想,设计出结构清晰、可维护性强的游戏代码。
3. 学生通过团队协作,学会使用版本控制工具,提高项目管理和团队协作能力。
情感态度价值观目标:1. 学生培养对编程和游戏开发的兴趣,激发创新意识和探索精神。
2. 学生在项目实践中,增强自信心,培养面对困难和挑战时的积极态度。
3. 学生通过团队协作,学会尊重他人意见,培养良好的沟通能力和团队精神。
课程性质:本课程为实践性较强的课程,结合Java编程语言和游戏开发技术,使学生能够在实际操作中掌握编程知识和技能。
学生特点:学生处于初中或高中年级,具有一定的计算机操作基础,对游戏开发感兴趣,具备一定的逻辑思维能力。
教学要求:课程注重理论与实践相结合,强调动手实践,引导学生通过自主学习和团队协作完成游戏设计项目。
在教学过程中,关注学生的个体差异,鼓励学生提问和分享,提高学生的综合素质。
通过课程学习,使学生在知识、技能和情感态度价值观方面取得具体的学习成果。
二、教学内容1. Java基础语法:变量、数据类型、运算符、控制流程(条件语句、循环语句)、数组等基本概念。
2. 面向对象编程:类与对象、构造方法、继承与多态、接口与实现等。
- 相关章节:课本第3章至第5章。
3. Java图形用户界面(GUI):Swing库的使用,窗口、标签、按钮、文本框等组件的创建与应用。
- 相关章节:课本第7章。
4. 游戏设计基础:- 坐标系统与动画原理:游戏中的坐标系,帧动画原理。
- 碰撞检测与角色控制:矩形碰撞检测,角色移动、跳跃等控制逻辑。
基于Java的超级玛丽游戏的设计与实现论文

分类号_______________ 密级________________UDC _______________ 学号_ ___ 毕业设计(论文)论文题目基于Java的超级玛丽游戏的设计与实现thesis Topic学生姓名学号所在院系专业班级导师姓名职称完成日期年月日基于java的超级玛丽游戏的设计与实现摘要近年来,Java作为一种新的编程语言,以其简单性、可移植性和平台无关性等优点,得到了广泛地应用。
J2SE称为Java标准版或Java标准平台。
J2SE提供了标准的SDK开发平台。
利用该平台可以开发Java桌面应用程序和低端的服务器应用程序,也可以开发Java Applet程序。
“超级玛丽”游戏是一个经典的游戏,它因操作简单、娱乐性强而广受欢迎。
本游戏中通过总结和分析JAVA游戏的开发流程和代码功能的设计,采用面向对象的设计模式,对游戏中的所有物体赋予对象的概念和属性。
开发了一款超级玛丽游戏,该游戏主要分为窗体类、场景类、马里奥类、敌人类、障碍物类等几个类。
在主程序运行的线程中,用户控制的超级玛丽随方向键的操作而移动,并且可以实现马里奥游戏中的一些基本功能,例如踩死敌人或者顶掉砖块。
本游戏采用双缓存技术,解决了游戏过程中的闪屏问题。
通过本游戏的开发,达到学习Java技术和熟悉软件开发流程的目的。
本文在介绍Java相关技术和国内外发展现状的基础上,对“超级玛丽”游戏的整个生命周期的各个开发阶段进行了详细地介绍。
为了保证程序代码的正确性和功能实现的可靠性,本文还介绍了游戏软件的程序调试过程和功能测试结果。
【关键字】:面向对象;Java;碰撞检测;超级玛丽Design and implementation of java-based Super Mario gameAbstractIn recent years, Java as a new programming language, with its simplicity, portability and platform independence, etc., have been widely used. J2SE called Java Standard Edition or Java standard platforms. J2SE SDK provides a standard development platform. You can use the platform to develop Java desktop applications and low-end server applications, you can develop Java Applet program."Super Mario" game is a classic game, because it is simple, entertaining and popular. The game features through the development process and code design summary and analysis of JA V A games, using object-oriented design patterns, all objects of the game gives the concept of objects and attributes. Developed a Super Mario game, the game is divided into several classes form class, class scene, Mario class, class enemies, obstacles and so on. In the main thread running, user-controlled Super Mario with the arrow keys to move the operation, and can achieve Mario game some basic functions, such as stepping on the enemy or top off the bricks. The game uses dual caching technology to solve the game during the splash screen problem. Through the development of this game, to learn Java technology and familiar with software development process purposes.This paper describes the state of the art related to the development of Java and abroad on the basis of the various development stages of "Super Mario" game for the entire life cycle of a detailed introduction. In order to ensure the correctness of the program code and functions to achieve reliability, this article describes the game software debugging process and functional test results.Key words: Object-Oriented,java,Collision Detection, Mario目录1、绪论 (5)1.1背景以及现状 (5)1.2 Java语言的特点 (5)1.3 系统运行环境及开发软件: (6)1.4 可行性的分析 (6)1.4.1 技术可行性 (6)1.4.2 经济可行性 (7)1.4.3 操作可行性 (7)2、需求分析 (7)2.1 用户需求分析 (7)2.2功能需求分析 (8)2.3界面设计需求分析 (8)3、系统概要设计 (8)3.1系统模块设计 (8)3.1.1窗体类 (9)3.1.2初始化类 (9)3.1.3背景类 (10)3.1.4马里奥类 (10)3.1.5障碍物类 (11)3.1.6敌人类 (11)3.2系统流程设计 (13)4、系统详细设计 (14)4.1 设计目标 (14)4.2 系统模块设计 (14)4.2.1窗体类 (14)4.2.2初始化类 (18)4.2.3背景类 (21)4.2.4马里奥类 (24)4.2.5障碍物类 (29)4.2.6敌人类 (31)5、系统的实现 (34)5.1游戏开发所需要的图片 (34)5.1.1马里奥的所有图片 (34)5.1.2游戏中障碍物的图片 (35)5.1.3游戏中怪物的图片 (35)5.1.4游戏中的背景图片 (36)5.1.5游戏开始时的图片 (36)5.2游戏设计的界面 (37)5.2.1 游戏逻辑展示 (37)5.2.1 游戏逻辑展示 (38)6、系统测试 (39)6.1 测试的意义 (39)6.2 测试过程 (40)6.3 测试结果 (40)7、总结与展望 (41)7.1 总结 (41)7.2 设计中的不足之处 (41)7.3 展望 (41)致谢 (42)外文原文 (43)外文翻译 (47)1、绪论1.1背景以及现状随着计算机日益普及,计算机已然成为人们日常生活的一部分的今天,计算机游戏也越来越成为人们生活中不可或缺的元素。
超级玛丽小游戏设计毕业设计

摘要《超级玛丽》全名《超级马里奥兄弟》,是任天堂公司出品的著名横版过关游戏,最早在红白机上推出,有多款后续作品,迄今多个版本合共销量已突破4000万套。
其中的主角马里奥、路易、桃子公主、蘑菇王子等等已成为任天堂的招牌人物。
该课题主要是模仿经典的《超级玛丽》完成相应的游戏功能。
本游戏以java为主要技术核心,通过各种核心类实现了游戏中的主要人物和角色,并通过各类函数实现了地图的绘制、碰撞检测、键控、滚屏等核心功能,并对游戏进行了一系列优化,例如产生新对象,收集垃圾,对时间、空间的权衡,实现了电脑版的超级玛丽。
关键字:超级玛丽;Java;碰撞检测;滚屏Abstract"Super Marie" full name "super Mario Bros." version of the cross, is a famous Nintendo Co produced the pass of the game, the first introduced in red and white machine, there are a variety of subsequent works, so multiple versions of total sales have exceeded 40000000 sets. One of the main characters Mario, Louis, Princess Peach, mushroom Prince has become Nintendo sign characters. The main issue is to imitate the classic "super Marie" to complete the corresponding game function. This game is the main core technology in Java, through a variety of core class implements the main characters and the characters in the game, and implements map rendering, collision detection, key control, scrolling and other core functions through various kinds of function, and the Games were a series of optimization, such as creating a new object, garbage collection, to balance time, space, achieve 1 computer version of super Marie.Key words: Super Marie ,Java, Collision detection ,Roller screen目录1. 绪论 (1)1.1 java介绍 (1)1.2 超级玛丽游戏的历史 (1)2. 基本概要 (2)2.1 游戏应用环境的构建 (2)2.1.1游戏需要的硬件环境 (2)2.1.2游戏需要的软件环境 (2)2.2 操作 (2)2.3 游戏界面 (2)3. 系统分析 (4)3.1 本程序需要解决的主要技术问题 (4)3.2 游戏的总体构架 (4)3.3 程序的类结构以及流程图 (5)3.3.1 类结构 (5)3.3.2 流程图 (6)4. 详细设计及实现 (7)4.1 地图的实现 (7)4.1.1地图的结构 (7)4.1.2约定规则 (7)4.1.3地图的产生 (8)4.2 滚屏的实现 (9)4.2.1原理 (9)4.2.2地图内部对象的移动 (9)4.2.3小结 (9)4.3 地图切换的实现 (9)4.4 碰撞检测 (10)4.4.1原理 (10)4.4.2碰撞检测在游戏中的应用 (10)4.5 玛丽以及键控处理的实现 (10)4.5.1 Mario类的实现 (11)4.5.2键控处理 (12)4.6 砖头的实现 (12)4.6.1分类 (12)4.6.2结构 (13)4.7 敌人的实现 (13)4.7.1分类 (13)4.7.2结构 (14)4.8 重力模拟 (15)5. 游戏的优化及注意点 (16)5.1 游戏中屏幕绘制技术的优化 (16)5.2 游戏中新对象的产生和垃圾收集 (16)5.3 游戏对系统资源占用的优化 (17)6. 总结 (18)参考文献 (20)致谢 (21)附录 (22)1.绪论1.1java介绍Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。
超级玛丽课程设计

超级玛丽课程设计一、课程目标知识目标:1. 学生能理解并掌握游戏设计的基本概念,如角色设定、场景布局等。
2. 学生能运用所学的编程知识,设计并实现一个简单的“超级玛丽”游戏角色和场景。
3. 学生了解游戏设计中的数学知识,如坐标系统、碰撞检测等。
技能目标:1. 学生能运用图形化编程软件,进行游戏角色的设计和场景的搭建。
2. 学生掌握基本的编程逻辑,能够实现角色的移动、跳跃等动作。
3. 学生通过团队协作,完成一个完整的游戏项目,提高沟通与协作能力。
情感态度价值观目标:1. 学生培养对编程和游戏设计的兴趣,激发创新意识和探索精神。
2. 学生在游戏设计过程中,体验团队合作的重要性,增强团队意识和责任感。
3. 学生通过创作游戏,培养审美观念和艺术素养,提高对美的追求和表达能力。
分析课程性质、学生特点和教学要求:1. 本课程为信息技术课程,旨在让学生在实践中学习编程和游戏设计,提高动手能力。
2. 学生为四年级学生,具有一定的图形化编程基础,对游戏设计感兴趣。
3. 教学要求注重实践性、趣味性和团队合作,以激发学生的学习兴趣和主动性。
1. 独立设计并实现一个简单的“超级玛丽”游戏角色和场景。
2. 掌握游戏设计的基本编程逻辑,实现角色的移动、跳跃等动作。
3. 通过团队协作,完成一个具有创意的“超级玛丽”游戏项目,展示给其他同学和老师。
二、教学内容1. 游戏设计基本概念:角色设定、场景布局、游戏规则等。
- 教材章节:第二章“游戏设计概述”- 内容安排:第一课时,介绍游戏设计的基本概念和要素。
2. 图形化编程软件操作:使用Scratch等软件进行角色设计和场景搭建。
- 教材章节:第三章“图形化编程软件应用”- 内容安排:第二、三课时,学习软件操作,进行角色和场景设计。
3. 编程逻辑:实现角色移动、跳跃等动作,掌握条件语句和循环语句。
- 教材章节:第四章“编程逻辑与应用”- 内容安排:第四、五课时,学习编程逻辑,实现角色动作。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
《面向对象程序设计》课程设计报告题目:超级玛丽软件的设计与实现院(系):信息科学与工程学院专业班级:计算机科学与技术1201班学生姓名:程伟学号: 20121183011指导教师:吴奕20 14 年 12 月 29 日至20 15 年 1 月 9 日华中科技大学武昌分校制面向对象程序设计课程设计任务书目录1需求与总体设计 (4)1.1需求分析 (4)1.2程序功能图 (4)1.3程序类图 (5)2详细设计 (6)2.1 MyFrame实现 (6)2.1.1窗体对象的序列图 (6)2.1.2 Myframe详细类图 (7)2.2 Mario实现 (7)2.2.1 Mario对象的序列图 (7)2.2.2 Mario类图 (8)2.3 Enemy类的实现 (9)2.4 Obstruction类的实现 (10)2.5 BackGround类实现 (10)3编码实现 (11)4系统测试 (61)4.1 游戏开始界面的测试 (61)4.2 游戏运行的测试 (61)4.3 排除错误的处理 (64)总结 (65)1需求与总体设计1.1需求分析提供一个友好的用户交互界面,简单明了容易操作。
并且游戏有一定的难度和娱乐性。
开发一个Mario类。
一个场景类,以及敌人类,障碍物类。
让超级玛丽能够在场景中完成各种动作。
并且有场景的切换等等功能。
游戏中设计了一些很有意思的陷阱,玩家一步小心掉入陷阱,超级玛丽就会死亡。
这样会给人意想不到的惊奇,达到娱乐大众,增加游戏难度的效果。
游戏的设计匠心独具,并且都经过了各种测试,除了为玩家增加一些意想不到的游戏陷阱以外,还必须保证玩家能够通关。
1.2程序功能图如图1所示:图11.3程序类图系统的整体类图的框架如图2所示。
首先是窗体类。
他需要调用到Mario和BackGround类中的方法。
以便动态的显示Mario类和BackGround。
而BackGround里面又包含了Enemy和Obstruction。
Enemy,Obstruction和Mario类又调用静态类StaticValue的方法,用以显示真实的图片。
而StaticValue则是一个静态类,将存储在硬盘的图片资源加载进入内存以便其他模块调用。
图22详细设计2.1MyFrame实现2.1.1窗体对象的序列图窗体对象继承了JFrame类并且实现了Runnable接口。
作为窗体对象,它首先显示窗体,绘制背景。
然后启动线程。
在run方法里面动态得绘制窗体中需要显示的Mario 的图片,障碍物的图片,敌人的图片。
该窗体的run方法是一个死循环,每次执行一个循环调用sleep方法睡眠50毫秒。
序列图如图3所示。
图32.1.2 Myframe详细类图private Mario mario;//Mario对象private boolean isStart = false;//标识游戏是否已经开始this.paintAllEnemys(g2);//绘制所有的敌人this.paintAllObstruction(g2);//绘制所有障碍物2.2 Mario实现2.2.1 Mario对象的序列图因为需要有一个专门的线程来实时监测Mario的移动状况,和障碍物和敌人的碰撞情况。
所以让Mario类继承了Runnable接口。
Mario先启动线程。
该线程的run方法也是一个死循环。
首先根据Mario的状态changeMarioImage方法修改Mario的显示图片。
再根据Mario的determinexy方法决定Mario的移动。
然后通过deterMarioWithObstruction方法检测Mario和障碍物之间的碰撞,在根据marioTouchEnemys方法检测Mario和敌人之间的碰撞。
然后调用sleep()方法让线程休息5毫秒。
如此结束一个循环。
序列图如图4所示。
图4 2.2.2 Mario类图类图中:private int x;//Mario的x坐标private int y;//Mario的y坐标private int life;//Mario的生命BackGround bg = null;//Mario所在的背景private int uptime;//Mario的上升时间String status;//Mario的移动状态int score;//Mario的分数Thread t;//Mario的线程int xmove;//Mario的x移动距离int ymove;//Mario的y移动距离private boolean isClear = false;//标识用户是否通关。
isClear为true时代表用户通关了2.3 Enemy类的实现private int x;//敌人的x坐标private int y;//敌人的y坐标private int startX;//敌人的初始x坐标private int startY;//敌人的初始y坐标private boolean startIsLeftOrUp;//敌人初始的移动方向private int startImageType;//初始的显示图片private int type;//敌人的类型1代表蘑菇,2代表食人花,3代表乌龟private BufferedImage bufferedImage;private BackGround bg;//障碍物所在的背景private int imageType;//移动的极限范围详细类图如下:2.4 Obstruction类的实现private int x;//障碍物的x坐标private int y;//障碍物的y坐标private BufferedImage showImage = null;//障碍物的显示图片private int type;//障碍物的类型private int startType;//障碍物的初始类型private BackGround bg; //标识该障碍物在哪一个场景中详细类图如下:2.5 BackGround类实现private int sort;//背景的序列号private boolean flag;//标识是否是最后一张图片,flag为true时代表到了最后一张图片private boolean isOver = false;//标识游戏是否已经结束private boolean isDown = false;//标志是否降旗结束详细类图如下:3编码实现源代码import java.awt.image.BufferedImage;import java.util.ArrayList;import java.util.List;import java.awt.Graphics;import java.awt.Toolkit;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Iterator;import javax.swing.JFrame;import javax.swing.JOptionPane;import java.io.File;import javax.imageio.ImageIO;public class MyFrame extends JFrame implements KeyListener,Runnable {public static void main(String[] args){new MyFrame();}private List<BackGround> allBg = new ArrayList<BackGround>();//当前背景private BackGround nowBg = null;private Mario mario;//Mario对象private boolean isStart = false;//标识游戏是否已经开始public void setNowBg(BackGround nowBg) {this.nowBg = nowBg;}private Thread t;/***/public MyFrame(){this.setSize(900,600);this.setTitle("马里奥游戏");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);int width = Toolkit.getDefaultToolkit().getScreenSize().width;int height = Toolkit.getDefaultToolkit().getScreenSize().height;this.setLocation((width - 900) / 2,(height - 600) / 2);this.addKeyListener(this);StaticValue.init();this.setVisible(true);//绘制出所有的背景for(int i = 1; i <= 4; i++){this.allBg.add(new BackGround(i, i == 4 ? true : false));}this.nowBg = this.allBg.get(0);this.mario = new Mario(0, 480);this.mario.setBg(nowBg);this.setResizable(false);t = new Thread(this);t.start();}@Overridepublic void paint(Graphics g){BufferedImage image = new BufferedImage(900, 600, BufferedImage.TYPE_4BYTE_ABGR);if(isStart){Graphics g2 = image.getGraphics();g2.drawImage(this.nowBg.getBgImage(), 0, 0, this);//绘制背景g2.drawString("生命:" + this.mario.getLife(), 800, 60);//显示生命数g2.drawString("分数:" + this.mario.getScore(), 60, 60);//显示生命数this.paintAllEnemys(g2);//绘制所有的敌人this.paintAllObstruction(g2);//绘制所有障碍物}else{Graphics g2 = image.getGraphics();g2.drawImage(StaticValue.startImage, 0, 0, this);//绘制游戏开始的图片}g.drawImage(image, 0, 0, this);}/***/@Overridepublic void keyPressed(KeyEvent e){//int k = e.getKeyCode();//获得键盘的ASCII码//System.out.println(k);if(this.isStart){if(e.getKeyCode() == 39){this.mario.rightMove();}if(e.getKeyCode() == 37){this.mario.leftMove();}if(e.getKeyCode() == 32){this.mario.jump();}//按下空格键,Mario的生命加10if(70 == e.getKeyCode()){this.mario.setLife(this.mario.getLife() + 10);}}else{if(e.getKeyCode() == 32){this.isStart = true;this.nowBg.enemysStartMove();}}}/***/@Overridepublic void keyReleased(KeyEvent e) {if(e.getKeyCode() == 39){this.mario.rightStop();}if(e.getKeyCode() == 37){this.mario.leftStop();}}/***/@Overridepublic void keyTyped(KeyEvent e){// TODO Auto-generated method stub }@Overridepublic void run(){while(true){this.repaint();if(840 == this.mario.getX()){this.intoNewBackGround();//进入下一张图片}if(this.mario.isClear()){JOptionPane.showMessageDialog(this, "恭喜您通关了,敬请期待新的关卡");System.exit(0);}if(this.mario.isDead()){this.gameOver();//游戏结束}this.sleep();}}//进入下一张图片private void intoNewBackGround(){this.nowBg = this.allBg.get(this.nowBg.getSort());this.mario.setBg(this.nowBg);this.mario.setX(0);this.mario.setY(480);this.mario.setUptime(0);this.nowBg.enemysStartMove();}//绘制所有的敌人private void paintAllEnemys(Graphics g2){Iterator<Enemy> iterEnemy = this.nowBg.getAllEnemy().iterator();while(iterEnemy.hasNext()){Enemy e = iterEnemy.next();g2.drawImage(e.getBufferedImage(), e.getX(), e.getY(), this);}}//绘制所有障碍物private void paintAllObstruction(Graphics g2){Iterator<Obstruction> iter = this.nowBg.getAllObstruction().iterator();while(iter.hasNext()){Obstruction ob = iter.next();g2.drawImage(ob.getShowImage(), ob.getX(), ob.getY(), this);}g2.drawImage(mario.getShowImage(), mario.getX(), mario.getY(), this);}private void sleep(){try{Thread.sleep(20);}catch (InterruptedException e){e.printStackTrace();}}//游戏结束private void gameOver(){JOptionPane.showMessageDialog(this, "马里奥死亡");this.mario.setDead(false);System.exit(0);}}class BackGround{private BufferedImage bgImage = null;private int sort;//背景的序列号private boolean flag;//标识是否是最后一张图片,flag为true时代表到了最后一张图片private List<Enemy> allEnemy = new ArrayList<Enemy>();//所有敌人的集合//所有障碍物的集合private List<Obstruction> allObstruction = new ArrayList<Obstruction>();//所有被移除的敌人的集合private List<Enemy> removeEnemy = new ArrayList<Enemy>();//所有被移除的障碍物的集合private List<Obstruction> removeObstruction = new ArrayList<Obstruction>();private boolean isOver = false;//标识游戏是否已经结束private boolean isDown = false;//标志是否降旗结束public boolean isDown() {return isDown;}public void setDown(boolean isDown) {this.isDown = isDown;}public boolean isOver() {return isOver;}public void setOver(boolean isOver) {this.isOver = isOver;}public boolean isFlag() {return flag;}public void setFlag(boolean flag) {this.flag = flag;}public int getSort() {return sort;}public void setSort(int sort) {this.sort = sort;}public BufferedImage getBgImage(){return bgImage;}public List<Obstruction> getAllObstruction(){return allObstruction;}public List<Obstruction> getRemoveObstruction() { return removeObstruction;}public List<Enemy> getAllEnemy() {return allEnemy;}public List<Enemy> getRemoveEnemy() {return removeEnemy;}/**** @param sort 背景的序列号* @param flag 标识是否是最后一张背景*/public BackGround(int sort, boolean flag) {this.sort = sort;this.flag = flag;if(flag){bgImage = StaticValue.endImage;}else{bgImage = StaticValue.bgImage;}switch(sort){case 1:this.creat1Map();break;case 2:this.creat2Map();break;case 3:this.creat3Map();break;case 4:this.creat4Map();break;}}public void reset(){this.allEnemy.addAll(removeEnemy);for(int i = 0; i < this.allEnemy.size(); ++i){this.allEnemy.get(i).reSet();}this.removeEnemy.clear();this.allObstruction.addAll(removeObstruction);for(int i = 0; i < this.allObstruction.size(); ++i) {this.allObstruction.get(i).reset();}this.removeObstruction.clear();}public void enemysStartMove(){for(int i =0; i < this.allEnemy.size(); ++i){this.allEnemy.get(i).starMove();}}//创建第一张图private void creat1Map(){for(int i = 0; i < 15; i++){//创建出所有地面this.allObstruction.add(new Obstruction(i * 60,540,9, this));}//创建出所有砖块,10代表问好,0代表木块this.allObstruction.add(new Obstruction(120,300,10, this));this.allObstruction.add(new Obstruction(60,300,10, this));this.allObstruction.add(new Obstruction(0,300,10, this));this.allObstruction.add(new Obstruction(300,360,0, this));this.allObstruction.add(new Obstruction(360,360,10, this));this.allObstruction.add(new Obstruction(420,360,0, this));this.allObstruction.add(new Obstruction(480,360,10, this));this.allObstruction.add(new Obstruction(540,360,0, this));this.allObstruction.add(new Obstruction(420,180,10, this));//创建水管this.allObstruction.add(new Obstruction(660,540,6, this));this.allObstruction.add(new Obstruction(720,540,7, this));this.allObstruction.add(new Obstruction(660,480,4, this));this.allObstruction.add(new Obstruction(720,480,5, this));//创建出所有的敌人,1代表三角菇,2代表食人花this.allEnemy.add(new Enemy(600, 480, true, 1, this));this.allEnemy.add(new Enemy(540, 300, true, 1, this));this.allEnemy.add(new Enemy(480, 300, true, 1, this));this.allEnemy.add(new Enemy(420, 300, true, 1, this));this.allEnemy.add(new Enemy(360, 300, true, 1, this));this.allEnemy.add(new Enemy(690, 540, true, 2, 540, 420, this)); }//创建第二张图private void creat2Map(){for(int i = 0; i < 15; i++){if(i != 10){this.allObstruction.add(new Obstruction(i * 60,540,9, this));}}//创建水管this.allObstruction.add(new Obstruction(60,540,6, this));this.allObstruction.add(new Obstruction(120,540,7, this));this.allObstruction.add(new Obstruction(60,480,6, this));this.allObstruction.add(new Obstruction(120,480,7, this));this.allObstruction.add(new Obstruction(60,420,4, this));this.allObstruction.add(new Obstruction(120,420,5, this));//创建水管this.allObstruction.add(new Obstruction(240,540,6, this));this.allObstruction.add(new Obstruction(300,540,7, this));this.allObstruction.add(new Obstruction(240,480,6, this));this.allObstruction.add(new Obstruction(300,480,7, this));this.allObstruction.add(new Obstruction(240,420,6, this));this.allObstruction.add(new Obstruction(300,420,7, this));this.allObstruction.add(new Obstruction(240,360,4, this));this.allObstruction.add(new Obstruction(300,360,5, this));//创建水管this.allObstruction.add(new Obstruction(720,540,6, this));this.allObstruction.add(new Obstruction(780,540,7, this));this.allObstruction.add(new Obstruction(720,480,6, this));this.allObstruction.add(new Obstruction(780,480,7, this));this.allObstruction.add(new Obstruction(720,420,6, this));this.allObstruction.add(new Obstruction(780,420,7, this));this.allObstruction.add(new Obstruction(720,360,4, this));this.allObstruction.add(new Obstruction(780,360,5, this));//创建砖块for(int i = 0; i < 6; ++i){this.allObstruction.add(new Obstruction(360 + i * 60,360,3, this));}//创建敌人this.allEnemy.add(new Enemy(90, 480, true, 2, 480, 360, this));//食人花this.allEnemy.add(new Enemy(270, 420, true, 2, 420, 300, this));//食人花this.allEnemy.add(new Enemy(750, 420, true, 2, 420, 300, this));//食人花}//创建第三张图private void creat3Map(){for(int i = 0; i < 15; i++){//创建出所有地面this.allObstruction.add(new Obstruction(i * 60,540,9, this));}//创建砖块this.allObstruction.add(new Obstruction(60, 360, 0, this));this.allObstruction.add(new Obstruction(120, 360, 0, this));this.allObstruction.add(new Obstruction(180, 360, 0, this));this.allObstruction.add(new Obstruction(360, 360, 10, this));this.allObstruction.add(new Obstruction(360, 180, 3, this));this.allObstruction.add(new Obstruction(420, 180, 3, this));this.allObstruction.add(new Obstruction(540, 360, 10, this));this.allObstruction.add(new Obstruction(540, 180, 10, this));this.allObstruction.add(new Obstruction(720, 360, 10, this));//创建敌人this.allEnemy.add(new Enemy(840, 480, true, 3, 4, this));this.allEnemy.add(new Enemy(540, 480, true, 3, this));this.allEnemy.add(new Enemy(840, 480, true, 1, this));this.allEnemy.add(new Enemy(780, 480, true, 1, this));this.allEnemy.add(new Enemy(720, 480, true, 1, this));this.allEnemy.add(new Enemy(660, 360, true, 1, this));this.allEnemy.add(new Enemy(600, 360, true, 1, this));}private void creat4Map(){for(int i = 0; i < 15; i++){//创建出所有地面this.allObstruction.add(new Obstruction(i * 60,540,9, this));}//绘制旗子this.allObstruction.add(new Obstruction(550, 190, 11, this));//绘制旗子下面的砖块this.allObstruction.add(new Obstruction(520,480,2, this));}}class Enemy implements Runnable{private int x;//敌人的x坐标private int y;//敌人的y坐标private int startX;//敌人的初始x坐标private int startY;//敌人的初始y坐标private boolean startIsLeftOrUp;//敌人初始的移动方向private int startImageType;//初始的显示图片private int type;//敌人的类型1代表蘑菇,2代表食人花,3代表乌龟private BufferedImage bufferedImage;private BackGround bg;//障碍物所在的背景//敌人的显示图片,不同的type代表不同的敌人显示图片,通过显示图片的切换,达到动态的效果private int imageType;//移动的极限范围private int upMax;private int downMax;private boolean isLeftOrUP;Thread t = new Thread(this);//普通敌人的构造方法,加入显示图片参数public Enemy(int x, int y, boolean isLeft, int type, int imageType, BackGround bg){this.x = x;this.y = y;this.startX = x;this.startY = y;this.isLeftOrUP = isLeft;this.startIsLeftOrUp = isLeft;this.type = type;this.bg = bg;this.imageType = imageType;this.startImageType = imageType;switch(type){case 1://蘑菇//取蘑菇的图片this.bufferedImage = StaticValue.allTriangleImage.get(0);break;case 3://乌龟this.bufferedImage = StaticValue.allTurtleImage.get(0);break;}t.start(); //启动线程t.suspend();//抑制线程启动}//普通敌人的构造方法public Enemy(int x, int y, boolean isLeft, int type, BackGround bg) {this.x = x;this.y = y;this.startX = x;this.startY = y;this.isLeftOrUP = isLeft;this.startIsLeftOrUp = isLeft;this.type = type;this.bg = bg;switch(type){case 1://蘑菇//取蘑菇的图片this.bufferedImage = StaticValue.allTriangleImage.get(0);break;case 3://乌龟this.bufferedImage = StaticValue.allTurtleImage.get(0);break;}t.start(); //启动线程t.suspend();//抑制线程启动}//食人花敌人的构造方法public Enemy(int x, int y, boolean isUp, int type, int upMax, int downMax, BackGround bg){this.x = x;this.y = y;this.startX = x;this.startY = y;this.isLeftOrUP = isUp;this.bg = bg;this.type = type;this.upMax = upMax;this.downMax = downMax;if(2 == type){//取食人花图片this.bufferedImage = StaticValue.allFlowerImage.get(0);}t.start();//启动线程t.suspend();//抑制线程启动}//当Mario死亡的时候重置敌人的方法public void reSet(){this.x = this.startX;this.y = this.startY;this.isLeftOrUP = this.startIsLeftOrUp;this.imageType = this.startImageType;switch(this.type){case 1:this.bufferedImage = StaticValue.allTriangleImage.get(0);break;case 2:this.bufferedImage = StaticValue.allFlowerImage.get(0);this.isLeftOrUP = true;break;case 3:this.bufferedImage = StaticValue.allTurtleImage.get(this.startImageType);break;}}//敌人死亡的方法public void dead(){switch(this.type){case 1://蘑菇//将显示图片改为敌人死亡的图片this.bufferedImage = StaticValue.allTriangleImage.get(2);this.bg.getAllEnemy().remove(this);this.bg.getRemoveEnemy().add(this);break;case 3://乌龟//将显示图片改为敌人死亡的图片this.imageType = 4;this.bufferedImage = StaticValue.allTurtleImage.get(imageType);break;}}public void run(){while(true){this.sleep();switch(this.type){case 1://蘑菇this.triangleMove();//蘑菇移动break;case 2://食人花this.flowerMove();//食人花移动break;case 3://乌龟this.turtleMove();//乌龟移动break;}}}public BufferedImage getBufferedImage() { return bufferedImage;}public int getStartX() {return startX;}public int getStartY() {return startY;}public int getX() {return x;}public int getY() {return y;}public int getType() {return type;}public void starMove(){this.t.resume();}//蘑菇移动private void triangleMove(){if(this.isLeftOrUP){this.x -= 2;}else{this.x += 2;}//改变imageType来让敌人的图片切换,达到动态的效果if(0 == this.imageType){this.imageType = 1;}else{this.imageType = 0;}//定义用来标记敌人是否可以向左或者向右移动的标记boolean canleft = true;boolean canright = true;boolean onLand = false;for(int i = 0; i < bg.getAllObstruction().size(); i++){Obstruction ob = bg.getAllObstruction().get(i);//敌人不能向右移动if(this.x == ob.getX() - 50 && ((this.y < ob.getY() + 50) && (this.y > ob.getY() - 50))){canright = false;}//敌人不能向左移动if(this.x == ob.getX() + 50 && ((this.y < ob.getY() + 50) && (this.y > ob.getY() - 50))){canleft = false;}if((this.y == ob.getY() - 60) && ((this.x < ob.getX() + 50) && (this.x > (ob.getX() - 50)))){onLand = true;}}if(!onLand){this.down();}//如果敌人正在向左移动,但是又不能向左移动了,那么改变敌人的移动方向if((this.isLeftOrUP && !canleft) || 0 == this.x){this.isLeftOrUP = false;}//如果敌人正在向右移动,但是又不能向右移动了,那么改变敌人的移动方向if((!this.isLeftOrUP && !canright) || 840 == this.x){this.isLeftOrUP = true;}//通过imageType获取在内存中真正的图片this.bufferedImage = StaticValue.allTriangleImage.get(this.imageType);}//食人花移动private void flowerMove(){if(this.isLeftOrUP){this.y -= 2;}else{this.y += 2;}//当食人花到达极限位置的时候,修改它移动的方向if(!this.isLeftOrUP && this.y == this.upMax){this.isLeftOrUP = true;}else if(this.isLeftOrUP && this.y == this.downMax){this.isLeftOrUP = false;}//改变imageType来让敌人的图片切换,达到动态的效果if(0 == this.imageType){this.imageType = 1;}else{this.imageType = 0;}//通过imageType获取在内存中真正的图片this.bufferedImage = StaticValue.allFlowerImage.get(this.imageType);}//乌龟移动private void turtleMove(){if(this.isLeftOrUP){if(4 == this.imageType){this.x -= 10;}else{this.x -= 2;}}else{if(4 == this.imageType){this.x += 10;}else{this.x += 2;}}this.changeTurtleImage();//改变乌龟的显示图片//定义用来标记敌人是否可以向左或者向右移动的标记boolean canleft = true;boolean canright = true;boolean onLand = false;for(int i = 0; i < bg.getAllObstruction().size(); i++){Obstruction ob = bg.getAllObstruction().get(i);//敌人不能向右移动if(this.x == ob.getX() - 50 && ((this.y < ob.getY() + 50) && (this.y > ob.getY() - 50))){canright = false;}//敌人不能向左移动if(this.x == ob.getX() + 50 && ((this.y < ob.getY() + 50) && (this.y > ob.getY() - 50))){canleft = false;}if((this.y == ob.getY() - 60) && ((this.x < ob.getX() + 50) && (this.x > (ob.getX() - 50)))){onLand = true;}}if(!onLand){this.down();}//如果敌人正在向左移动,但是又不能向左移动了,那么改变敌人的移动方向if((this.isLeftOrUP && !canleft) || this.x <= 0){this.isLeftOrUP = false;}//如果敌人正在向右移动,但是又不能向右移动了,那么改变敌人的移动方向if((!this.isLeftOrUP && !canright) || this.x >= 840){this.isLeftOrUP = true;}//通过imageType获取在内存中真正的图片this.bufferedImage = StaticValue.allTurtleImage.get(this.imageType);}//敌人下落private void down(){this.y += 5;}private void sleep(){try{Thread.sleep(20);}catch(Exception e){e.printStackTrace();}}//改变乌龟的显示图片private void changeTurtleImage(){if(4 == this.imageType){return;}if(this.isLeftOrUP)//如果是向左移动{//循环切换向左移动的两张图片if(0 == this.imageType){this.imageType = 1;}else{this.imageType = 0;}}else//如果是向右边移动{if(2 == this.imageType){this.imageType = 3;}else{this.imageType = 2;}}}}class Mario implements Runnable{private int x;//Mario的x坐标private int y;//Mario的y坐标private int moving = 1;private BufferedImage showImage = null;private int life;//Mario的生命BackGround bg = null;//Mario所在的背景private int uptime;//Mario的上升时间String status;//Mario的移动状态int score;//Mario的分数Thread t;//Mario的线程int xmove;//Mario的x移动距离int ymove;//Mario的y移动距离private boolean isClear = false;//标识用户是否通关。