制作简单的计时器

时间:2014-06-08 21:12:46

标签: java multithreading timer

我需要为一个“炸弹人”制作一个3秒钟的定时器'风格游戏我在java中制作。我想让炸弹在3秒钟倒计时爆炸,但是当我对我的脚本添加延迟时,它会延迟整个程序。 (Thread.sleep()做同样的事情。)

所以我需要一个非常简单的脚本示例,它运行一个主程序,另一个运行3秒计时器的线程。

我对java很陌生,所以不要使用许多花哨的单词而不解释!!!!!

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.Border;

public class MainClass extends JFrame implements KeyListener{

    private static final long serialVersionUID = 1L;

    public MainClass(){     
        super("WaterBalloonWars");
        JPanel panel=new JPanel(new GridLayout(width,height));
        myGrid = new JPanel[width][height];
        Border border=BorderFactory.createLineBorder(Color.DARK_GRAY, 1);   
        for(int r=0;r<width-1;r++){
            for(int c=0;c<height-1;c++){
                double randNum = Math.random();
                myGrid[c][r] = new JPanel();
                myGrid[c][r].setBackground(Color.WHITE);
                myGrid[c][r].setBorder(border);
                if(randNum<=0.65){
                    myGrid[c][r].setBackground(Color.GRAY);
                }
                if((r & 1)==1){
                    if((c & 1)==1){
                        myGrid[c][r].setBackground(Color.BLACK);
                    }                   
                }

                panel.add(myGrid[c][r]);            
                            }
        }               
        myGrid [0][0].setBackground(Color.BLUE);
        myGrid [0][1].setBackground(Color.WHITE);
        myGrid [1][0].setBackground(Color.WHITE);
        myGrid [2][0].setBackground(Color.WHITE);
        myGrid [0][2].setBackground(Color.WHITE);
        myGrid [10][10].setBackground(Color.RED);
        myGrid [10][9].setBackground(Color.WHITE);
        myGrid [9][10].setBackground(Color.WHITE);
        myGrid [8][10].setBackground(Color.WHITE);
        myGrid [10][8].setBackground(Color.WHITE);  
        super.setContentPane(panel);
        this.addKeyListener(new UKeyListener());
        this.addKeyListener(new DKeyListener());  
        this.addKeyListener(new LKeyListener()); 
        this.addKeyListener(new RKeyListener());
        this.addKeyListener(new ShiftKeyListener());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        }

    class ShiftKeyListener implements KeyListener{
        @Override 
        public void keyPressed(KeyEvent event) { 
            if (event.getKeyCode() == KeyEvent.VK_SHIFT) {  
               plantBomb(Color.red);
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {   

        }
        @Override
        public void keyTyped(KeyEvent e) {          
        }
    }

    class UKeyListener implements KeyListener{        

        @Override 
        public void keyPressed(KeyEvent event) { 
            if (event.getKeyCode() == KeyEvent.VK_UP) {  
               redMove(1);
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {   

        }
        @Override
        public void keyTyped(KeyEvent e) {          
        }
       }
    class DKeyListener implements KeyListener{        
        @Override 
        public void keyPressed(KeyEvent event) { 
            if (event.getKeyCode() == KeyEvent.VK_DOWN) {  
               redMove(2);
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {   

        }
        @Override
        public void keyTyped(KeyEvent e) {          
        }
    }   
    class LKeyListener implements KeyListener{        
        @Override 
        public void keyPressed(KeyEvent event) { 
            if (event.getKeyCode() == KeyEvent.VK_LEFT) {  
               redMove(3);
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {   

        }
        @Override
        public void keyTyped(KeyEvent e) {          
        }
    }
    class RKeyListener implements KeyListener{        
        @Override 
        public void keyPressed(KeyEvent event) { 
            if (event.getKeyCode() == KeyEvent.VK_RIGHT) {  
               redMove(4);
            }
        }
        @Override
        public void keyReleased(KeyEvent e) {   

        }
        @Override
        public void keyTyped(KeyEvent e) {          
        }
    }
        public void plantBomb(Color color){     
            if(color.equals(Color.red)){
                rbombx=rx;
                rbomby=ry;
                myGrid[rbombx][rbomby].setBackground(Color.orange);
                redBombPlaced=true; 
                explode(Color.red,2,rbombx,rbomby);

            }
            if(color.equals(Color.blue)){

            }
        }
    public void explode(Color color,int power,int x,int y){ 

//这是我想要定时器去的部分

        boolean ublock=false;
        boolean dblock=false;
        boolean lblock=false;
        boolean rblock=false;
        for(int i=1;i<=power;i++){
            if(!rblock&&(x+i)<=10){
                if(myGrid[x+i][y].getBackground().equals(Color.black)){
                    rblock=true;                    
                }else{myGrid[x+i][y].setBackground(Color.white);}
            }
            if (!lblock&&(x-i)>=0){
                if(myGrid[x-i][y].getBackground().equals(Color.black)){
                    lblock=true;                
                }else{myGrid[x-i][y].setBackground(Color.white);}
            }   
            if(!dblock&&(y+i)<=10){
                if(myGrid[x][y+i].getBackground().equals(Color.black)){
                    dblock=true;
                }else{myGrid[x][y+i].setBackground(Color.white);}
            }
            if(!ublock&&(y-i)>=0){
                if(myGrid[x][y-i].getBackground().equals(Color.black)){
                    ublock=true;
                }else{myGrid[x][y-i].setBackground(Color.white);}
            }

            if(color.equals(Color.red)){
                redBombPlaced=false;
            }
        }
    }


    public void redMove(int direction){//1=up 2=down 3=left 4=right
        if(direction==1){
            if(ry-1!=-1){
            if(myGrid[rx][ry-1].getBackground()==(Color.BLACK)||myGrid[rx][ry-1].getBackground()==(Color.GRAY)||ry-1==-1){  
            }else{
                ry--;           
                myGrid[rx][ry].setBackground(Color.red);
                if(redBombPlaced){
                    redBombPlaced=false;
                }else{
                    myGrid[rx][ry+1].setBackground(Color.white);
                }
            }
            }
        }
        if(direction==2){
            if(ry+1!=11){
            if(myGrid[rx][ry+1].getBackground()==(Color.BLACK)||myGrid[rx][ry+1].getBackground()==(Color.GRAY)||ry+1>11){   
            }else{
            ry++;
            myGrid[rx][ry].setBackground(Color.red);
            if(redBombPlaced){
                redBombPlaced=false;
            }else{
                myGrid[rx][ry-1].setBackground(Color.white);
            }
            }
            }
        }
        if(direction==3){
            if(rx-1!=-1){
            if(myGrid[rx-1][ry].getBackground()==(Color.BLACK)||myGrid[rx-1][ry].getBackground()==(Color.GRAY)||rx-1==-1){
            }else{
            rx--;
            myGrid[rx][ry].setBackground(Color.red);
            if(redBombPlaced){
                redBombPlaced=false;
            }else{
                myGrid[rx+1][ry].setBackground(Color.white);
            }
            }
            }
        }
        if(direction==4){
            if(rx+1!=11){
                if(myGrid[rx+1][ry].getBackground()==(Color.BLACK)||myGrid[rx+1][ry].getBackground()==(Color.GRAY)||rx+1==11){
                }else{
                    rx++;
                    myGrid[rx][ry].setBackground(Color.red);
                    if(redBombPlaced){
                        redBombPlaced=false;
                    }else{
                        myGrid[rx-1][ry].setBackground(Color.white);
                    }
                }
            }
        }
    }
    public void centerFrame(){
        Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
        int drawAtX=screen.width/2 - super.getWidth() / 2;
        int drawAtY=screen.height/2 - super.getHeight() / 2;
        super.setLocation(drawAtX-50,drawAtY-50);
    }
    private JPanel [][] myGrid;
    private int width=12;
    private int height=12;  
    private int rx=10;
    private int ry=10;
    int rbombx=0;
    int rbomby=0;
    private boolean redBombPlaced=false;
    private boolean blueBombPlaced=false;
    /*public static void main(String[] args){
        MainClass frame = new MainClass();
        frame.setSize(600,600);
        frame.setVisible(true);
        frame.centerFrame();
    }*/
    @Override
    public void keyPressed(KeyEvent e) {
    }
    @Override
    public void keyReleased(KeyEvent e) {
    }
    @Override
    public void keyTyped(KeyEvent e) {      
    }
}

不要对我的编码发表评论,这可能会让你感到畏缩但是没关系,

2 个答案:

答案 0 :(得分:1)

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(3000);
            bombs.explode();
        } catch (InterruptedException e) {
        }
    }
}).start();

答案 1 :(得分:0)

您可以使用TimerTask在线程内创建一个Timer。

使用Thread.sleep实际上会将主线程设置为block state

示例:

    public class TimerBomb {
  Toolkit toolkit;

  Timer timer;

  public TimerBomb(int seconds) {
    toolkit = Toolkit.getDefaultToolkit();
    timer = new Timer();
    timer.schedule(new RemindTask (), seconds * 1000);
  }

  class RemindTask extends TimerTask {
    public void run() {
      System.out.println("3 second is up");
    }
  }

  public static void main(String args[]) {
    System.out.println("About to schedule task.");
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            new TimerBomb(3);


        }
    });
    t.start();
    System.out.println("Task scheduled.");
  }
}