java消灭地鼠代码 简单的打地鼠游戏java

java入门程序,简易打地鼠 。增加一个count计数 , 用来统计打中的次数 。
点击一个button的时候 , 判断当前点击的button的颜色是不是红色,如果是 , count
JAVA出现异常,Exception in thread "main" java.lang.NullPointerExceptionShrewmouse初始化出错,
图片初始化错了,可以这样:
Image image = new ImageIcon("graphics/Window2.png").getImage();
基于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}
按键精灵安卓类是打地鼠代码问题 高手进首先,找图区域错误.其次,按照脚本逻辑是不算的重复显示0123,不管找没有找到图,因为显示没有在条件判断里面.如果要求没有找到图则显示0123,那么ShowMessage要写到判断里的否则语句下面
java 砸地鼠代码//CatchMice.java
//还有部分功能未实现,自己去搞吧
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class CatchMice {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Mice().setUser("Haha"));
f.pack();
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class Mice extends Canvas implements MouseListener,MouseMotionListener{
private String url=":";//老鼠图片
private int index=-1;
private int score;
private int initDelay=2000;
private int delay = initDelay;
private int level = 1;
private int gradeScore=160;
private int caughtScore=10;
private Image mice;
private BufferedImage bimg;
private int cellW=80,cellH=80,r=4,c=4;//53
private Rectangle rec;
private String user;
private Thread t;
private long time;
private boolean pause,gameover;
public Mice(){
try{
bimg=ImageIO.read(new URL(url));
mice=bimg.getSubimage(0,0,cellW,cellH);
bimg=null;
}catch(Exception e){
if(bimg==null){
bimg=new BufferedImage(cellW,cellH,BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bimg.createGraphics();
g.setColor(Color.LIGHT_GRAY);
g.drawOval(10,10,cellW-20,cellH-20);
g.dispose();
mice=bimg.getSubimage(0,0,cellW,cellH);
bimg=null;
}
};
setPreferredSize(new Dimension(cellW*c,cellH*r 60));
index=next();
rec = new Rectangle(0,0,cellW,cellH);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public Mice setUser(String u){user=u;return this;}
private int next(){
int next = (int)(Math.random()*r*c);
return index==next?next():next;
}
public void paint(Graphics gg){
Graphics g=gg.create();
g.setColor(Color.white);
g.fillRect(0,0,cellW*c,cellH*r);
g.setColor(Color.gray);
for(int i=0; ir*c; i){
if(i==index){
g.drawImage(mice,i%c*cellW,i/r*cellH,this);
rec.setLocation(i%c*cellW,i/r*cellH);
}
g.drawRect(i%c*cellW,i/r*cellH,cellW,cellH);
}
g.setColor(Color.DARK_GRAY);
g.setColor(Color.blue);
String info = "User:" user "Level:" level "Score:" score "Delay:" delay;
g.drawString(info,1,r*cellH 16);
g.dispose();
if(t==null){
start();
}
}
private void start(){
time = System.currentTimeMillis();
t=new Thread(){
public void run(){
while(true){
if(pause){
time=System.currentTimeMillis();
try{sleep(50);}catch(Exception e){}
continue;
}
if(!gameovertime delaySystem.currentTimeMillis()){
gameOver();
}
if(!gameover)
drawTime();
else
drawGameOver();
try{sleep(50);}catch(Exception e){}
}
}
};
t.start();
}
private void drawGameOver() {
Graphics g = this.getGraphics();
g.setColor(Color.DARK_GRAY);
g.fillRect(0,r*cellH 18,80,18);
if(System.currentTimeMillis()00500){
g.setColor(Color.yellow);
g.drawString("Game over!",4,r*cellH 32);
}
g.dispose();
}
private void drawTime() {
Graphics g = this.getGraphics();
long t = delay time-System.currentTimeMillis();;
g.setColor(Color.DARK_GRAY);
g.fillRect(0,r*cellH 18,48,18);
g.setColor(Color.yellow);
g.drawString("" t,4,r*cellH 32);
g.dispose();
}
private void gameOver() {
gameover=true;
index=-1;
repaint();
}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
//if(!gameoverrec.contains(e.getPoint())){
//index=next();
//time=System.currentTimeMillis();
//score =caughtScore;
//level=score/gradeScore 1;
//delay=initDelay;
//for(int i=0; ilevel-1; i)
//delay=(int)(delay*2f/3);
//repaint();
//}
}
public void mouseReleased(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
if(!gameoverrec.contains(e.getPoint())){
index=next();
time=System.currentTimeMillis();
score =caughtScore;
level=score/gradeScore 1;
delay=initDelay;
for(int i=0; ilevel-1; i)
delay=(int)(delay*2f/3);
repaint();
}
}
}
JAVA做的打地鼠小游戏,地鼠图片上有黑色边框,高手进你做的是iptv游戏吗 机顶盒游戏吗 还是模拟器运行的啊
请提供更多详细信息,方便问题定位朋友
【java消灭地鼠代码 简单的打地鼠游戏java】关于java消灭地鼠代码和简单的打地鼠游戏java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

    推荐阅读