java打地鼠参考代码 打地鼠程序代码

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

public class Game extends Thread{

10余年的永昌网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整永昌建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“永昌网站设计”,“永昌网站推广”以来,每个客户项目都认真落实执行。

/**

* @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 比较简单的程序设计!要有注释的。

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

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语言的打地鼠的小游戏源代码是什么?

 public void mouseClicked(MouseEvent e){

Object source=e.getSource(); //获取事件源,即地鼠标签

if(source instanceof JLabel){ //如果事件是标签组件

JLabel mouse=(JLabel)source; //强制转换为JLabel标签

mouse.setIcon(null); //取消标签图标

}

}

});

this.getContentPane().add(mouses[i]); //添加显示地鼠的标签到窗体

}

mouses[0].setLocation(253, 300); //设置每个标签的位置

mouses[1].setLocation(333, 250);

mouses[2].setLocation(388, 296);

mouses[3].setLocation(362, 364);

mouses[4].setLocation(189, 353);

mouses[5].setLocation(240, 409);

final JLabel backLabel=new JLabel(); //创建显示背景的标签

backLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());

this.setBounds(100,100,img.getIconWidth(),img.getIconHeight());

backLabel.setIcon(img); //添加背景到标签

this.getContentPane().add(backLabel); //添加背景标签到窗体

}

/**

* 线程的核心方法

*/

public void run(){

while(true){ //使用无限循环

try{

Thread.sleep(3000); //使线程休眠3秒

int index=(int)(Math.random()*6); //生成随机的地鼠索引

if(mouses[index].getIcon()==null){ //如果地鼠标签没有设置图片

mouses[index].setIcon(imgMouse); //为该标签添加地鼠图片

}

}catch(InterruptedException e){

e.printStackTrace();

}

}

}

}

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

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

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

JAVA出现异常,Exception in thread "main" java.lang.NullPointerException

Shrewmouse初始化出错,

图片初始化错了,可以这样:

Image image = new ImageIcon("graphics/Window2.png").getImage();

分享名称:java打地鼠参考代码 打地鼠程序代码
网站URL:https://www.cdcxhl.com/article48/hiophp.html

成都网站建设公司_创新互联,为您提供品牌网站建设网站设计公司响应式网站营销型网站建设云服务器

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站建设