Reputation: 11
I need to make a 3 second coundown timer for a 'bomberman' style game i'm making in java. I want to make the bombs explode on a 3 second countdown but when I add a delay to the script I have, it delays the whole program. (Thread.sleep() does the same thing).
So I need a really simple example of a script that runs a main program with another thread that runs a 3 second timer.
I'm pretty new to java so don't use many fancy words without explanations!!!!!
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){
//THIS IS THE PART THAT I WANT THE TIMER TO GO
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) {
}
}
Don't comment on my coding, it probably makes you cringe but that's ok,
Upvotes: 0
Views: 413
Reputation: 26198
you can Create a Timer inside a thread using TimerTask.
Using the Thread.sleep
will actually put your Main Thread to the block state
.
example:
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.");
}
}
Upvotes: 0
Reputation: 50716
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
bombs.explode();
} catch (InterruptedException e) {
}
}
}).start();
Upvotes: 1