
正文
java源代码手游 java源码app
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
基于Java语言的打地鼠的小游戏源代码是什么?
public void mouseClicked(MouseEvent e){\x0d\x0aObject source=e.getSource(); //获取事件源,即地鼠标签\x0d\x0aif(source instanceof JLabel){ //如果事件是标签组件\x0d\x0aJLabel mouse=(JLabel)source; //强制转换为JLabel标签\x0d\x0amouse.setIcon(null); //取消标签图标\x0d\x0a}\x0d\x0a}\x0d\x0a});\x0d\x0athis.getContentPane().add(mouses[i]); //添加显示地鼠的标签到窗体\x0d\x0a}\x0d\x0a\x0d\x0amouses[0].setLocation(253, 300); //设置每个标签的位置\x0d\x0amouses[1].setLocation(333, 250);\x0d\x0amouses[2].setLocation(388, 296);\x0d\x0amouses[3].setLocation(362, 364);\x0d\x0amouses[4].setLocation(189, 353);\x0d\x0amouses[5].setLocation(240, 409);\x0d\x0a\x0d\x0afinal JLabel backLabel=new JLabel(); //创建显示背景的标签\x0d\x0abackLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());\x0d\x0athis.setBounds(100,100,img.getIconWidth(),img.getIconHeight());\x0d\x0abackLabel.setIcon(img); //添加背景到标签\x0d\x0athis.getContentPane().add(backLabel); //添加背景标签到窗体\x0d\x0a}\x0d\x0a/**\x0d\x0a* 线程的核心方法\x0d\x0a*/\x0d\x0a\x0d\x0apublic void run(){\x0d\x0awhile(true){ //使用无限循环\x0d\x0atry{\x0d\x0aThread.sleep(3000); //使线程休眠3秒\x0d\x0aint index=(int)(Math.random()*6); //生成随机的地鼠索引\x0d\x0aif(mouses[index].getIcon()==null){ //如果地鼠标签没有设置图片\x0d\x0amouses[index].setIcon(imgMouse); //为该标签添加地鼠图片\x0d\x0a}\x0d\x0a}catch(InterruptedException e){\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}
相关问答
Q1: 开发手游的代码
4.1游戏的的思路、构想
4.1.1游戏想法的产生
相信大家一定都在8位机机上玩过《冒险岛》这款游戏,非常有趣味性。
游戏中玩家通过不断的闯关,来解救公主。在每个关都很很多的怪物阻挡着你,所以需要运用各种机关或者秘籍来杀死它们。杀死他们的同时还可以获得各种奖励,加生命,加血等,增加了游戏的趣味性。
如图2所示:
这款《冒险岛》游戏的实现相对于其他RPG或者网络版手机游戏稍简单一些,适合初学者作为练习,所以我决定编写一款类似的手机游戏。
由于之前对手机游戏的编程知识以及游戏的设计只有初步的了解,因此,我们在游戏的构架和思路上经历了几个阶段。
这款《冒险岛》游戏的实现相对于其他RPG或者网络版手机游戏稍简单一些,适合初学者作为练习,所以我决定编写一款类似的手机游戏。
由于之前对手机游戏的编程知识以及游戏的设计只有初步的了解,因此,我们在游戏的构架和思路上经历了几个阶段。
4.1.2对游戏设计的初步认识
刚开始我们只对J2ME有初步的了解。这时我们只是模仿之前在PC上看到的游戏,用语言把游戏的实现感性的描述为几大部分:
游戏界面系统:包括游戏开始界面;游戏开局界面;游戏运行界面;游戏结束界面。
游戏元素:菜单类;画布类;人物类;排行榜类。
4.1.3模块成型阶段
在进一步熟悉了J2ME知识后,对框架做出了一些修改,逐步把游戏的基本功能确定。游戏依次进入加载界面;主菜单;游戏运行界面;游戏结束界面。
具体实现的功能为:
1.主菜单,有如下选项:
(1)开始游戏——进入游戏界面。
(2)声音——设置声音的有无选项。
(3)帮助——介绍游戏的玩法。
(4)排行榜——玩家所得分数的排行榜。
(5)关于——用来显示说明信息以及背景图片。
2.游戏运行界面,包括:
游戏界面;目前游戏得分;游戏关数;生命次数;
3.游戏结束界面:游戏结束后,显示一行说明信息,然后退回到菜单。
游戏的主要模块为:
1.游戏主MIDlet(GameMIDlet)——对游戏生命周期的判断;对画布类的调用;管理游戏程序中各个屏幕之间的转换。
2.游戏画布(MyGame)——对游戏所用变量,常量的设定;游戏的初始化;游戏中精灵运动轨迹的控制;精灵与砖块的碰撞检测以及砖块状态的控制;游戏中各关卡的基本设定;游戏中对按键状态的处理。
3.菜单类——游戏中菜单事件的处理。
4.GameOgre类——游戏中怪物的类。
5.GamePlayer类——玩家控制的精灵类。
6.GameRMS类——用于实现分数排行榜。
7.PlayMusic类——用于实现音乐的播放。
8.MySet类——声音大小的设置。
4.2 程序的类结构
程序一共有8个主要类,其中菜单类负责各个屏幕的切换。程序的类结构如图3所示:
4.3 游戏的流程图
进入游戏菜单。初始情况下,游戏菜单有5个选项,它们分别是开始游戏、游戏说明和排行榜、设置、关于。选择开始新游戏则进入游戏,在游戏中如果按下非游戏键则中断游戏返回菜单,此时菜单中增加了一个继续游戏的选项,可以返回游戏也可以重新开始新的游戏。在菜单中选择游戏说明或者高分记录,则进入相应的屏幕,他们都能用“后退”软键返回菜单。菜单中的退出选项用于退出程序。游戏的流程如图4所示:
4.4.1主类GameMIDlet的实现
MIDlet是最核心的类。MIDlet程序有三种状态:
1.暂停状态
2.运行状态
3.销毁状态
J2ME程序都是从MIDlet类开始执行,系统在执行MIDlet程序时,首先构造一个MIDlet类型的对象,然后使程序进入到暂停状态,按照生命周期的规定,系统会自动调用MIDlet对象的startApp方法使程序进入到运行状态,开始程序的执行。
下图是运行时显示的画布对象:
首先,先要创建MIDlet类型的对象,下面我们来看对象的构造方法:
//主程序构造方法
public GameMIDlet()
{
rs = null;
RecordName = “GameRMS”;
GameMenu.display = Display.getDisplay(this) ;
GameMenu.midlet = this;
}
java
开发语言
oppo手机型号及价格
精选推荐
广告
JAVA基于J2ME的手机游戏开发(文档+源代码).zip
0下载·0评论
2022年1月27日
JAVA基于J2ME的手机游戏开发免费
717阅读·0评论·0点赞
2022年8月23日
JAVA五子棋手机网络对战游戏的设计与实现(源代码+论文)
568阅读·2评论·0点赞
2022年12月5日
J2ME手机游戏引擎程序结构简述
170阅读·0评论·0点赞
2021年9月12日
最新45款Java手机游戏开发源代码免费下载
10下载·0评论
2019年3月4日
经典50个Java手机游戏源码.7z
3下载·0评论
2022年7月8日
无敌版游戏下载
精选推荐
广告
java手机小游戏源码_Java手机版数独小游戏(J2me)JAVA游戏源码下载
435阅读·0评论·0点赞
2021年3月14日
java 300行代码 冒险闯关小游戏(代码+讲解)
2637阅读·1评论·6点赞
2022年9月9日
java俄罗斯方块代码_【俄罗斯方块java】分享一个Java写的俄罗斯方块源码 算法简单(300行) 注释详细!...
304阅读·0评论·0点赞
2021年3月5日
java小游戏源码_分享几款java小游戏源码
4921阅读·0评论·4点赞
2021年3月5日
java手机游戏开发如何_用JAVA开发手机游戏需要如何构建开发环境?
1209阅读·0评论·0点赞
2021年2月26日
《精通Java手机游戏与应用程序设计》源码
35阅读·0评论·0点赞
2022年3月24日
java怎么制作游戏,看完这篇彻底明白了
4803阅读·0评论·2点赞
2021年6月29日
泡泡堂代码 JAVA_Java手机游戏泡泡堂源码
566阅读·0评论·1点赞
2021年3月14日
十款经典游戏的Java版本(开源)
19.0W阅读·95评论·214点赞
2014年12月7日
飞翔的小鸟--Java小游戏实战(代码完整)
1.1W阅读·13评论·50点赞
2021年4月5日
Vue——获取后端json数据中的URL并通过按钮跳转到此URL
1683阅读·4评论·0点赞
2021年2月5日
java安卓游戏源码下载_77个安卓游戏 android源码
801阅读·0评论·0点赞
2021年3月15日
去首页
看看更多热门内容
Q2: 求java小游戏源代码
表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格java源代码手游的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘java源代码手游的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为truejava源代码手游, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); }}
java源代码手游的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java源码app、java源代码手游的信息别忘了在本站进行查找喔。





