Whack-a-mole(打地鼠)

本文探讨了前端开发中几个实用的技巧,包括flex布局的应用、setTimeout()的使用详解、classList属性的操作方法以及line-height属性的不同设置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码传送门:https://github.com/yycer/share

这里写图片描述

几个小问题
1.flex布局

传送门:http://blog.csdn.net/m0_37851778/article/details/77241009

2.setTimeout()

setTimeout(()=> timeUp = true , 10000);

//等价于
setTimeout(function(){
    timeUp = true;
},10000);

3.classList属性返回元素的类名,作为DOMTokenList对象。

该属性是只读的,但可使用以下方法修改它。
①add(class1,class2,...)
②remove(class1,class2,...)
③toggle(class,true|false)
④item(index)
⑤contain(class)

4.line-height
http://cssreference.io/property/line-height/

line-height行高:两行文字间基线(base line)的距离。
②line-height: normal;line-height: 1.6;--- the line height will be relative to the font size.
④line-height: 30px;line-height: 0.8em;--- the line height will be relative to the font size.
好的,以下是用Java实现打地鼠游戏(简版)的代码示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class WhackAMoleGame extends JFrame implements ActionListener { private JButton[][] buttons; private int score; private JLabel scoreLabel; private Timer timer; private Random random; public WhackAMoleGame() { super("打地鼠游戏"); // 创建按钮网格布局 JPanel panel = new JPanel(new GridLayout(3, 3)); buttons = new JButton[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { buttons[i][j] = new JButton("地鼠"); buttons[i][j].addActionListener(this); panel.add(buttons[i][j]); } } add(panel, BorderLayout.CENTER); // 创建得分标签 score = 0; scoreLabel = new JLabel("得分:" + score); add(scoreLabel, BorderLayout.NORTH); // 创建计时器和随机数生成器 timer = new Timer(1000, this); random = new Random(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { JButton button = (JButton) e.getSource(); if (button.getText().equals("地鼠")) { score++; scoreLabel.setText("得分:" + score); JOptionPane.showMessageDialog(this, "恭喜你,打到了地鼠!"); } else { JOptionPane.showMessageDialog(this, "很遗憾,你没有打到地鼠"); } } else if (e.getSource() == timer) { // 随机显示地鼠 int row = random.nextInt(3); int col = random.nextInt(3); buttons[row][col].setText("地鼠"); // 2秒后隐藏地鼠 timer.setInitialDelay(2000); buttons[row][col].setText(""); } } public static void main(String[] args) { WhackAMoleGame game = new WhackAMoleGame(); game.setSize(300, 300); game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); game.setVisible(true); game.timer.start(); } } ``` 希望这个代码示例可以帮助您更好地理解如何用Java实现打地鼠游戏
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值