
正文
java打地鼠源代码论文 funcode打地鼠代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
基于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();
}
}
}
}
相关问答
Q1: java入门程序,简易打地鼠。
增加一个count计数,用来统计打中的次数。
点击一个button的时候,判断当前点击的button的颜色是不是红色,如果是,count++
Q2: JAVA出现异常,Exception in thread "main" java.lang.NullPointerException
Shrewmouse初始化出错,
图片初始化错了,可以这样:
Image image = new ImageIcon("graphics/Window2.png").getImage();
Q3: JAVA做的打地鼠小游戏,地鼠图片上有黑色边框,高手进
你做的是iptv游戏吗 机顶盒游戏吗 还是模拟器运行的啊
请提供更多详细信息,方便问题定位朋友
Q4: 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" ; //洞java打地鼠源代码论文,表示没有老鼠出来
}
}
}
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) //可以调整java打地鼠源代码论文,让老鼠出现java打地鼠源代码论文的概率降低一些
gameMap[i][j] = "@"; //有老鼠出现
}
}
printMap();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
}
}
}
呵呵,线程加数组就可以java打地鼠源代码论文了,但是不知道能不能满足你的要求。截图如下java打地鼠源代码论文:
java打地鼠源代码论文的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于funcode打地鼠代码、java打地鼠源代码论文的信息别忘了在本站进行查找喔。






