java别踩白块小游戏

别踩白块小游戏:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>别踩白块</title>
    <style type="text/css">
        #score{
            text-align: center;}
 		h1 {
            text-align: center;
            color:red;    }
        h2 {
            text-align: center;
            color:green;    }
 
        div{
            margin: 0 auto;
            width: 100px;
            height: 100px;}
 
        #main {
            width: 400px;
            height: 400px;
            background: white;
            border: 2px solid gray;
            margin: 0 auto;
            position: relative;
            overflow: hidden;}
 
        #con {
            width: 100%;
            height: 400px;
            position: relative;
            top: -100px;
            border-collapse:collapse;}
 
        .row{
            height: 100px;
            width: 100%;}
 
        .cell{
            height: 100px;
            width: 100px;
            float: left;}
 
        .black {
            background: black;}
    </style>
</head>
<body>
 	<h1>别踩白块</h1>
    <h2>score</h2>
    <h2 id="score">0</h2>
    <div id="main">
        <div id="con"></div>
    </div>
</body>
<script>
    var clock = null;
    var state = 0;
    var speed = 4;
 
        /*
        *    初始化 init
        */
        function init(){
            for(var i=0; i<4; i++){
                createrow();
            }
 
            // 添加onclick事件
            $('main').onclick = function(ev){
                judge(ev);
            }
 
            // 定时器 每30毫秒调用一次move()
                clock = window.setInterval('move()', 90);
        }
 
        // 判断是否点击黑块
        function judge(ev){
            if (ev.target.className.indexOf('black') != -1) {
                ev.target.className = 'cell';
                ev.target.parentNode.pass = 1; //定义属性pass,表明此行row的黑块已经被点击
                score();
            }
        }
 
        // 游戏结束
        function fail(){
            clearInterval(clock);
            confirm('你的最终得分为 ' + parseInt($('score').innerHTML) );
        }
 
        // 创建div, className是其类名
        function creatediv(className){
            var div = document.createElement('div');
            div.className = className;
            return div;
        }
 
        // 创造一个<div class="row">并且有四个子节点<div class="cell">
        function createrow(){
            var con = $('con');
            var row = creatediv('row'); //创建div className=row
            var arr = creatcell(); //定义div cell的类名,其中一个为cell black
 
            con.appendChild(row); // 添加row为con的子节点
 
            for(var i = 0; i < 4; i++){
                row.appendChild(creatediv(arr[i])); //添加row的子节点 cell
            }
 
            if(con.firstChild == null){
                con.appendChild(row);
            }else{
                con.insertBefore(row, con.firstChild);
            }
        }
 
        // 根据id来get DOM元素
        function $(id) {
            return document.getElementById(id);
        }
 
        // 创建一个类名的数组,其中一个为cell black, 其余为cell
        function creatcell(){
            var temp = ['cell', 'cell', 'cell', 'cell',];
            var i = Math.floor(Math.random()*4);
            temp[i] = 'cell black';
            return temp;
        }
 
        //让黑块动起来
        function move(){
            var con = $('con');
            var top = parseInt(window.getComputedStyle(con, null)['top']);
 
            if(speed + top > 0){
                top = 0;
            }else{
                top += speed;
            }            
            con.style.top = top + 'px';
 
            if(top == 0){
                createrow();
                con.style.top = '-100px';
                delrow();
            }else if(top == (-100 + speed)){
                var rows = con.childNodes;
                if((rows.length == 5) && (rows[rows.length-1].pass !== 1) ){
                    fail();
                }
            }
        }
 
        // 加速函数
        function speedup(){
            speed += 2;
            if(speed == 20){
                alert('你超神了');
            }
        }
 
        //删除某行
        function delrow(){
            var con = $('con');
            if(con.childNodes.length == 6) {
                   con.removeChild(con.lastChild);
               }
        }    
 
        // 记分
        function score(){
            var newscore = parseInt($('score').innerHTML) + 1;
            $('score').innerHTML = newscore;
            if(newscore % 10 == 0){
                speedup();
            }
        }
 
    init();
</script>
</html>

我使用的工具为:STS,TOMCAT服务器,和火狐浏览器。

  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,下面是一个简单的别踩白块小游戏Java代码实现: ```java import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; public class BCBKGame extends JPanel implements Runnable, MouseListener { private static final long serialVersionUID = 1L; private static final int ROWS = 4; // 行数 private static final int COLS = 4; // 列数 private static final int BLOCK_SIZE = 100; // 方块大小 private static final int PADDING = 10; // 间隔 private static final int WIDTH = COLS * (BLOCK_SIZE + PADDING); // 窗体宽度 private static final int HEIGHT = ROWS * (BLOCK_SIZE + PADDING); // 窗体高度 private int score = 0; // 得分 private boolean gameOver = false; // 游戏是否结束 private int[][] blocks = new int[ROWS][COLS]; // 方块数组 private int[][] colors = new int[ROWS][COLS]; // 方块颜色数组 private Random rand = new Random(); // 随机数生成器 public BCBKGame() { JFrame frame = new JFrame("别踩白块小游戏"); frame.setSize(WIDTH, HEIGHT); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.add(this); frame.setVisible(true); addMouseListener(this); new Thread(this).start(); } @Override public void run() { while (!gameOver) { try { Thread.sleep(500); moveBlocks(); createBlock(); repaint(); } catch (InterruptedException e) { e.printStackTrace(); } } } private void createBlock() { int col = rand.nextInt(COLS); blocks[0][col] = 1; colors[0][col] = rand.nextInt(3); } private void moveBlocks() { for (int i = ROWS - 1; i > 0; i--) { for (int j = 0; j < COLS; j++) { blocks[i][j] = blocks[i - 1][j]; colors[i][j] = colors[i - 1][j]; } } for (int j = 0; j < COLS; j++) { blocks[0][j] = 0; colors[0][j] = -1; } } @Override public void paint(Graphics g) { super.paint(g); g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH, HEIGHT); for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { if (blocks[i][j] == 1) { g.setColor(getColor(colors[i][j])); g.fillRect(j * (BLOCK_SIZE + PADDING), i * (BLOCK_SIZE + PADDING), BLOCK_SIZE, BLOCK_SIZE); } } } g.setColor(Color.BLACK); g.drawString("得分:" + score, 10, HEIGHT - 10); if (gameOver) { g.setColor(Color.RED); g.drawString("游戏结束!", WIDTH / 2 - 40, HEIGHT / 2); } } private Color getColor(int color) { switch (color) { case 0: return Color.BLUE; case 1: return Color.GREEN; case 2: return Color.RED; default: return Color.BLACK; } } @Override public void mouseClicked(MouseEvent e) { if (!gameOver) { int row = e.getY() / (BLOCK_SIZE + PADDING); int col = e.getX() / (BLOCK_SIZE + PADDING); if (row == 3 && blocks[row][col] == 1) { score++; blocks[row][col] = 0; colors[row][col] = -1; } else if (row < 3 && blocks[row][col] == 1) { gameOver = true; } repaint(); } } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } public static void main(String[] args) { new BCBKGame(); } } ``` 这个小游戏的实现比较简单,主要是利用Java的Swing框架和线程来实现游戏的绘制和逻辑。在游戏中,每个方块有三种颜色可选,玩家需要点击下方的黑色方块,不能点击上方的白色方块,否则游戏结束。玩家可以通过点击黑色方块来获得得分,游戏会不断生成新的方块,直到游戏结束。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值