当前位置:首页 > 代码 > 正文

java打地鼠源代码(打地鼠编程代码)

admin 发布:2022-12-19 22:47 172


今天给各位分享java打地鼠源代码的知识,其中也会对打地鼠编程代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求java 比较简单的程序设计!要有注释的。

给个打地鼠游戏给你吧,图片路径自己改下:

import javax.swing.*;

import javax.swing.text.Position;

import javax.swing.text.AbstractDocument.Content;

import com.briup.gui1.GridLayoutTest;

import java.awt.*;

import java.awt.event.*;

public class MousePlay extends JFrame implements ActionListener {

private Container contentPane;

private JComboBox level;

private JLabel centLb1, timeLb1;

private JButton startBtn;

// 九个老鼠洞

private JButton[] btns;

// 两个计时器,一个计时,一个老鼠移动的位置

private Timer timer, postion;

//

private ImageIcon image;

//

private int index;

private boolean flag;

public MousePlay(){

this.setTitle("打地鼠");

this.setBounds(200, 200, 350, 400);

contentPane = this.getContentPane();

this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

//计时器在初始化的时候,需要指定触发的事件间隔(单位ms),并且添加AtionListener

timer = new Timer(1000, this);

postion = new Timer(700, this);

//导入图片的路径

image = new ImageIcon("src/com/briup/gui2/mouse.jpg");

//image = new ImageIcon("/home/briup/mouse.jpg");

initGui();

}

public  void initGui() {

contentPane.setLayout(new BorderLayout());

JPanel north = new JPanel();

level = new JComboBox(new String[]{"easy", "so-so", "hard"});

level.addItemListener(new ItemListener(){

public void itemStateChanged(ItemEvent e) {

Object obj = e.getItem();

int time = 0;

if("easy".equals(obj)){

time = 700;

}else if("so-so".equals(obj)){

time = 400;

}else if("hard".equals(obj)){

time = 100;

}

postion = new Timer(time, MousePlay.this);

}});

timeLb1 = new JLabel("10");

centLb1 = new JLabel("0");

startBtn = new JButton("start");

startBtn.addActionListener(this);

north.add(level);

north.add(new JLabel("time:"));

north.add(timeLb1);

north.add(new JLabel("center:"));

north.add(centLb1);

north.add(startBtn);

contentPane.add(north,BorderLayout.NORTH);

JPanel center = new JPanel();

center.setLayout(new GridLayout(3, 3));

btns = new JButton[9];

for(int i=0; ibtns.length;i++){

btns[i] = new JButton("");

btns[i].setEnabled(false);

btns[i].addActionListener(this);

center.add(btns[i]);

}

contentPane.add(center,BorderLayout.CENTER);

}

public void go(){

this.setVisible(true);

}

public void actionPerformed(ActionEvent e) {

Object obj = e.getSource();

if(obj==startBtn){

startBtn.setEnabled(false); //开始按钮不可操作

level.setEditable(false); // 下拉框不可操作

for(int i=0; ibtns.length; i++)

btns[i].setEnabled(true); //九个按钮可以操作

//启动倒计时计时器和老鼠

timer.start();

postion.start();

timeLb1.setText("10");

centLb1.setText("0");

}

if(obj==timer){ // 先获得页面的值,然后判断是否为0,处理相应的逻辑

int time = Integer.parseInt(timeLb1.getText().trim());

if(time==0){

timeLb1.setText("game over!");

timer.stop();

postion.stop();

startBtn.setEnabled(true);

level.setEnabled(true);

for(int i=0; ibtns.length; i++){

btns[i].setEnabled(false);

btns[i].setIcon(null);

btns[i].setText("");

}

}else{

timeLb1.setText(--time+"");

}

}

if(obj==postion){

for(int i=0; ibtns.length; i++)

btns[i].setIcon(null);

index = (int)(Math.random()*9);

btns[index].setIcon(image);

//btns[index].setText("");

flag = false;

}

for(int i=0; ibtns.length; i++){

if(btns[i]==obj(!flag)i==index){

int cent = Integer.parseInt(centLb1.getText().trim());

centLb1.setText(++cent+"");

flag = true;

}

}

}

public static void main(String[] args) {

new MousePlay().go();

}

java入门程序,简易打地鼠。

增加一个count计数,用来统计打中的次数。

点击一个button的时候,判断当前点击的button的颜色是不是红色,如果是,count++

java打地鼠游戏的源程序会用到数据结构吗

只要是涉及到Map,List,Tree等等集合、列表之类的都算用到数据结构。

所以说一般稍复杂点的程序都会用到数据结构,我想你应该问的是如何用数据结构的思想去理解打地鼠游戏吧?

如果是面向对象语言编程的话,首先你需要将每个小鼠洞看做一个对象,将全部鼠洞设计成该对象的二维数组。然后你需要有随机算法来指定哪个鼠洞是否出现老鼠。然后还需要有触发事件来确定锤子的击打坐标。

真正要做出来要考虑还很多,希望你先理清思路然后由浅入深。

Java 打地鼠游戏如何实现多个老鼠同时出现

public class Game extends Thread{

/**

* @param args

*/

String [][] gameMap = new String[3][3];

public static void main(String[] args) {

// TODO Auto-generated method stub

new Game().start();

}

public void initMap()

{

for (int i = 0; i gameMap.length; i++) {

for (int j = 0; j gameMap[0].length; j++) {

gameMap[i][j] = new String();

gameMap[i][j] = "O" ; //洞,表示没有老鼠出来

}

}

}

public void printMap()

{

for (int i = 0; i gameMap.length; i++) {

for (int j = 0; j gameMap[0].length; j++) {

System.out.print(gameMap[i][j]);

}

System.out.println();

}

}

public void run()

{

int temp = 0 ;

while(true)

{

initMap();

for (int i = 0; i gameMap.length; i++) {

for (int j = 0; j gameMap[0].length; j++) {

temp = (int)(100 * Math.random());

if(temp=20) //可以调整,让老鼠出现的概率降低一些

gameMap[i][j] = "@"; //有老鼠出现

}

}

printMap();

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println();

}

}

}

呵呵,线程加数组就可以了,但是不知道能不能满足你的要求。截图如下:

网上下的java手机游戏源码怎么用Eclipse在电脑上运行?

需要在eclipse中进行配置

工具:

eclipse

方法如下:

在eclipse中,鼠标放在一个android提供的类上,按下ctrl键,会打开一个新页面,提示找不到对应的类的class或者源文件,点击attach source...

之后点击external folder...选择下载好的source位置,确定后就可以了.

基于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}

java打地鼠源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于打地鼠编程代码、java打地鼠源代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/28432.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载