打字游戏(一)


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<button class="startBtn">开始</button>
<div class="score">得分:<b>0</b></div>
</body>
<script src="./js/utils.js"></script>
<script>
var b = document.querySelector('.score b');
// 打字游戏
/*
1.随机创建一个span
2.设置样式
3.设置定时器向下移动
4.绑定键盘事件 - 打掉他 - 重新再创建一个
5.判断是否到底 - GAME OVER

6.得分
7.开始按钮
*/
// var span
var arr = []
// var timerId

// 通过定时器调用createSpan - 每隔一会就创建一个span
var time
document.querySelector('.startBtn').onclick = function(){
    createSpan()
    time = setInterval(createSpan, 1500)
}
// 出问题:多个span一起出现,但是他们的变量名都叫span,多个定时器叠加操作同一歌后面的span。前面的span不能移动了,后面的span越走越快

// 解决:
/*
1.将多个span放在一个集合中 - 定义span变量要换成一个集合()
*/


function createSpan(){
    // 创建span
    var span = document.createElement('span')
    // 设置随机字符
    var code = getRandom(65, 91)
    // 将阿斯克码转成字符,放在span中
    span.innerText = String.fromCharCode(code)
    // 将span放在body中
    document.body.appendChild(span)
    // 获取随机的left值
    var left = getRandom(0, document.documentElement.clientWidth - 30)
    // 设置样式
    setStyle(span, {
        width: '30px',
        height: '30px',
        borderRadius: '50%',
        backgroundColor: '#f00',
        textAlign: 'center',
        lineHeight: '30px',
        position: 'absolute',
        top: 0,
        left: left + 'px',
        fontWeight: 'bold',
        color: '#fff'
    })
    // 设置定时器让span向下移动
    var timerId = setInterval(function(){
        // 获取top
        var t = span.offsetTop
        // 加大
        t++
        // 判断t的最大值
        if(t >= document.documentElement.clientHeight - 30){
            t = document.documentElement.clientHeight - 30
            // 提示游戏结束
            alert('GAME OVER')
            // 停止定时器
            clearInterval(timerId)
            // 清空所有定时器
            for(var i=0;i<arr.length;i++){
                clearInterval(arr[i].timer)
            }
            clearInterval(time)
        }
        // 将t设置给top样式
        span.style.top = t + 'px'
    }, 20)
    // 将span和timerId放在一个对象中
    arr.push({
        ele: span, 
        timer: timerId
    })
}
// 给浏览器绑定键盘事件
document.onkeyup = function(){
    // 获取键盘码
    var keycode = window.event.keyCode
    // 将键盘码转为字符
    var word = String.fromCharCode(keycode).toUpperCase()
    // 判断是否输入的span中的字符
    // if(word === span.innerText){
    //     // 删除span
    //     span.parentElement.removeChild(span)
    //     // 清除定时器
    //     clearInterval(timerId)
    //     // 再次创建
    //     createSpan()
    // }

    // 从arr中遍历判断word是哪个span的innerText
    for(var i=0;i<arr.length;i++){
        if(arr[i].ele.innerText === word){
            // 删除span
            arr[i].ele.parentElement.removeChild(arr[i].ele)
            // 清除对应的定时器
            clearInterval(arr[i].timer)
            // 将arr[i]对象中数组中删除
            arr.splice(i, 1)
            // 将分数+1
            b.innerText = +b.innerText + 1
            break
        }
    }
}
/*

*/
</script>
</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里是一个简单的打字游戏示例,使用Java Swing编写。 ```java import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.Timer; public class TypingGame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private static final int WIDTH = 600; private static final int HEIGHT = 400; private JLabel scoreLabel; private JLabel timeLabel; private JTextField textField; private JPanel gamePanel; private int score; private int timeLeft; private Timer timer; private Random random; private String[] words = { "java", "programming", "computer", "language", "algorithm", "software" }; public TypingGame() { super("Typing Game"); setPreferredSize(new Dimension(WIDTH, HEIGHT)); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(new BorderLayout()); scoreLabel = new JLabel("Score: 0"); scoreLabel.setFont(new Font("Arial", Font.BOLD, 16)); scoreLabel.setForeground(Color.BLUE); mainPanel.add(scoreLabel, BorderLayout.NORTH); gamePanel = new JPanel(); gamePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK)); mainPanel.add(gamePanel, BorderLayout.CENTER); timeLabel = new JLabel("Time left: 30"); timeLabel.setFont(new Font("Arial", Font.BOLD, 16)); timeLabel.setForeground(Color.RED); mainPanel.add(timeLabel, BorderLayout.SOUTH); textField = new JTextField(20); textField.setFont(new Font("Arial", Font.PLAIN, 16)); textField.addActionListener(this); gamePanel.add(textField); add(mainPanel); pack(); setLocationRelativeTo(null); score = 0; timeLeft = 30; random = new Random(); timer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { timeLeft--; timeLabel.setText("Time left: " + timeLeft); if (timeLeft == 0) { endGame(); } } }); timer.start(); newWord(); } private void newWord() { String word = words[random.nextInt(words.length)]; gamePanel.removeAll(); gamePanel.add(new JLabel(word)); textField.setText(""); pack(); } private void endGame() { timer.stop(); textField.setEnabled(false); gamePanel.removeAll(); gamePanel.add(new JLabel("Game over!")); pack(); } @Override public void actionPerformed(ActionEvent e) { String input = textField.getText().trim(); String word = ((JLabel) gamePanel.getComponent(0)).getText(); if (input.equals(word)) { score++; scoreLabel.setText("Score: " + score); newWord(); } else { textField.setText(""); } } public static void main(String[] args) { TypingGame game = new TypingGame(); game.setVisible(true); } } ``` 这个游戏有一个单词列表,每次随机选择一个单词并将其显示在屏幕上。玩家必须在文本框中输入正确的单词,以赚取分数。游戏时间为30,计时器会在游戏结束时停止。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值