java web小游戏_打地鼠都会玩,作为Java程序员这辈子能不开发几个小游戏吗!

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

这是一款非常有趣的打地鼠游戏。游戏的规则很简单,把冒出头来的地鼠给全部打下去就算成功。

7c38a8773360d064a72ce0000d2fb08a.png

import java.awt.Cursor;

import java.awt.Image;

import java.awt.Point;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.util.Random;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.Timer;

public class HitMouse extends JFrame implements ActionListener,MouseListener {

boolean isOver = false; //设置标记,游戏是否结束

private String dir =“./ images /”; //图片目录,当前工程下

JLabel jlbMouse; //地鼠

定时器定时器; //时间定时器

随机随机; //随机数对象,即生成地鼠的位置

int delay = 1100; //延迟时间

Toolkit tk;

图像;

游标myCursor;

JLabel showNum,currentGrade,hitNum;

int showNumber = 0,hitNumber = 0,currentGrades = 1;

public HitMouse(){

super(“打地鼠”);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSize(449,395);

this.setLocationRelativeTo(NULL); //设置窗口在屏幕中心

的setBackground(); //设置背景

this.getContentPane()setLayout的(NULL); //设置框架布局模式为空,只有这样,才能知道图片的真正位置

//设置鼠标为锤子图片

tk = Toolkit.getDefaultToolkit();

image = tk.createImage(dir +“chui1.png”);

myCursor = tk.createCustomCursor(image,new Point(10,10),“xxx”);

this.setCursor(myCursor);

setMessage(); //设置一些提示信息

//在背景图片的基础上设置地鼠图片

ImageIcon imageMouse = new ImageIcon(dir +“dishu.png”);

jlbMouse = new JLabel(imageMouse);

jlbMouse.setSize(80,80);

this.getContentPane()添加(jlbMouse)。

jlbMouse.setVisible(假);

jlbMouse.addMouseListener(this); //添加鼠标监听

//

定时器timer = new Timer(delay,this);

random = new Random();

timer.start();

addMenu(); //添加菜单

this.setResizable(假); //设置窗口大小不能改变

this.setVisible(真);

}

private void addMenu(){

JMenuBar menubar = new JMenuBar();

this.setJMenuBar(菜单栏);

JMenu game = new JMenu(“游戏”);

JMenuItem jitemNew = new JMenuItem(“新游戏”);

jitemNew.setActionCommand( “新”);

jitemNew.addActionListener(本);

JMenuItem jitemPause = new JMenuItem(“暂停”);

jitemPause.setActionCommand( “暂停”);

jitemPause.addActionListener(本);

JMenuItem jitemExit = new JMenuItem(“退出”);

jitemExit.setActionCommand( “退出”);

jitemExit.addActionListener(本);

game.add(jitemNew);

game.add(jitemPause);

game.addSeparator(); //菜单里设置分隔线

game.add(jitemExit);

menubar.add(游戏);

}

private void setbackground(){

((JPanel)(this.getContentPane()))setOpaque(false); //如果为true,则该组件绘制其边界内的所有像素。否则该组件可能不绘制部分或所有像素,从而允许其底层像素透视出来

。ImageIcon bgImage = new ImageIcon(“images / beijing.jpg”);

JLabel bgLabel = new JLabel(bgImage);

bgLabel.setBounds(0,25,bgImage.getIconWidth(),bgImage.getIconHeight());

this.getLayeredPane()。add(bgLabel,new Integer(Integer.MIN_VALUE)); //设置背景图片的层次最低

}

private void setMessage(){

ImageIcon showNumb = new ImageIcon(dir +“chuxiancishu.png”);

JLabel showLabel = new JLabel(showNumb);

showLabel.setBounds(8,8,92,80);

this.getContentPane()添加(showLabel)。

showNum = new JLabel(“0”);

showNum.setBounds(110,8,92,80);

this.getContentPane()添加(showNum)。

ImageIcon hitNumb =新的ImageIcon(dir +“chuxiancishu.png”);

JLabel hitLabel = new JLabel(hitNumb);

hitLabel.setBounds(148,8,92,80);

this.getContentPane()添加(hitLabel)。

hitNum = new JLabel(“0”);

hitNum.setBounds(251,8,92,80);

this.getContentPane()添加(hitNum)。

ImageIcon等级=新ImageIcon(dir +“dangqiandengji.png”);

JLabel gradeLabel = new JLabel(grade);

gradeLabel.setBounds(288,8,92,80);

this.getContentPane()添加(gradeLabel)。

currentGrade = new JLabel(“1”);

currentGrade.setBounds(391,8,92,80);

this.getContentPane()添加(currentGrade)。

}

public static void main(String [] args){

new HitMouse();

}

public void actionPerformed(ActionEvent e){

//对菜单项注册事件监听

if(e.getSource()instanceof JMenuItem){

menuItemFun(e);

}

int run = random.nextInt(9); //随机生成一个0〜9(不包括9)的随机数

ImageIcon imageMouse = new ImageIcon(dir +“dishu.png”); //保证每次随机生成的地鼠图片都是为没被打时 的图片

jlbMouse.setIcon(imageMouse);

switch(ran){

case 0:jlbMouse.setLocation(55,63); break;

情况1:jlbMouse.setLocation(321,204); break;

情况2:jlbMouse.setLocation(184,204); break;

情况3:jlbMouse.setLocation(47,203); break;

情况4:jlbMouse.setLocation(297,133); break;

情况5:jlbMouse.setLocation(161,133); break;

case 6:jlbMouse.setLocation(21,133); break;

case 7:jlbMouse.setLocation(310,63); break;

case 8:jlbMouse.setLocation(185,63); break;

}

jlbMouse.setVisible(true);

showNumber ++;

showNum.setText( “” + showNumber);

if(!gamePlan()){//判断游戏是否结束,并显示游戏进程

timer.stop();

}

由于帖子太长 没法都发表

想要了解更多Java知识点击加入我们一起学习交流一起进步吧!群331789133

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Java地鼠小游戏代码示例: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class WhackAMole extends JFrame implements ActionListener { private JButton[] buttons; private JLabel scoreLabel; private int score; private int molesRemaining; private Timer timer; public WhackAMole() { super("Whack-a-Mole"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(4, 4)); setSize(400, 400); score = 0; molesRemaining = 16; buttons = new JButton[16]; for (int i = 0; i < 16; i++) { buttons[i] = new JButton(" "); buttons[i].addActionListener(this); add(buttons[i]); } scoreLabel = new JLabel("Score: " + score + " Moles Remaining: " + molesRemaining); add(scoreLabel); timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { int randomButton = (int) (Math.random() * 16); buttons[randomButton].setText("M"); timer.setDelay((int) (Math.random() * 1000) + 500); } }); timer.start(); } public void actionPerformed(ActionEvent e) { JButton button = (JButton) e.getSource(); if (button.getText().equals("M")) { score++; molesRemaining--; } else { score--; } button.setText(" "); scoreLabel.setText("Score: " + score + " Moles Remaining: " + molesRemaining); if (molesRemaining == 0) { timer.stop(); JOptionPane.showMessageDialog(this, "Game Over! Final Score: " + score); } } public static void main(String[] args) { WhackAMole game = new WhackAMole(); game.setVisible(true); } } ``` 这个代码创建了一个 4x4 的按钮网格,其中每个按钮都有可能在随机时间内显示“M”(代表地鼠),家需要在出现后尽快点击按钮来“打地鼠”,每次打中增加得分并减少剩余地鼠数量,每次未打中则减少得分,游戏结束时弹出得分提示窗口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值