adding time i dont know how to add

Here is my maze code i want to add a timer in my game that if he dosent finish the maze in 60 seconds it will show a game over and the game will stop

Main class package javamazegame;

import javax.swing.*;


public class JavaMazegame { 

    public JavaMazegame(){
      JFrame f = new JFrame();
      f.setTitle("MazeGame Version 1.0");
      f.setSize(464,485);
      f.add(new Board());
      f.setLocationRelativeTo(null);
      f.setVisible(true);
      f.setResizable(false);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    }

    public static void main(String[] args) {
        new JavaMazegame();
    }

}

how to add a time in my maze game Board Class

package javamazegame;

import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.*;

public class Board extends JPanel implements ActionListener{

    private Timer timer;

    private Map m;

    private Player p;

    public boolean win = false ;

    private String Message = " ";

    private Font font = new Font("Serif",Font.BOLD,48);



    public Board(){

        m = new Map();
        p = new Player();
        addKeyListener(new Al());
        setFocusable(true);
        timer = new Timer(25,this);
        timer.start();
        System.out.println(timer);
    }



    public void actionPerformed(ActionEvent e){
        if(m.getMap(p.getTileX(),p.getTileY()).equals("f")){
            Message = "Winner";
            win = true;

        }
        repaint();
    }
    public void paint(Graphics g){
        super.paint(g);
        if(!win){
            for(int y = 0 ; y <14 ; y++){
                 for(int x = 0 ; x <14 ; x++){
                     if(m.getMap(x,y).equals("f")){
                         g.drawImage(m.getFinish(),x*32,y*32,null);
                     }
                     if(m.getMap(x, y).equals("g")){
                         g.drawImage(m.getGrass(), x*32, y*32, null);
                     }
                      if(m.getMap(x, y).equals("w")){
                         g.drawImage(m.getWall(), x*32, y*32, null);
                     }
                } 
            }

             g.drawImage(p.getPlayer(),p.getTileX()* 32, p.getTileY() * 32,null);
        }
        if(win){

            g.setColor(Color.GREEN);
            g.setFont(font);
            g.drawString(Message, 150, 200);

        }


    }
    public class Al extends KeyAdapter{
        public void keyPressed(KeyEvent e){
            int keycode = e.getKeyCode();

            if(keycode == KeyEvent.VK_W){
                 if(!m.getMap(p.getTileX(),p.getTileY()-1).equals("w")){
                  p.move(0,-1);
                 }
            }
             if(keycode == KeyEvent.VK_S){
                 if(!m.getMap(p.getTileX(),p.getTileY()+1).equals("w")){
                 p.move(0,1);
                 }
            }
              if(keycode == KeyEvent.VK_A){
                  if(!m.getMap(p.getTileX()-1,p.getTileY()).equals("w")){
                  p.move(-1,0);
                  }
            }
              if(keycode == KeyEvent.VK_D){
                  if(!m.getMap(p.getTileX()+1,p.getTileY()).equals("w")){
                  p.move(1,0);
                  }
            }
        }
         public void keyRelased(KeyEvent e){


        }
        public void keyTyped(KeyEvent e){


        }
    }

}

Map Class

package javamazegame;

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;


public class Map {

    private Scanner m;

    private String Map[] = new String[14];

    private Image grass,
                  finish,
                  wall;


    public Map(){

        ImageIcon img = new ImageIcon("C://Users//Rayjay//Desktop//Assets//road.png");
        grass = img.getImage();
        img = new ImageIcon("C://Users//Rayjay//Desktop//Assets//wall.png");
        wall = img.getImage();
        img = new ImageIcon("C://Users//Rayjay//Desktop//Assets//finish.png");
        finish = img.getImage();

        openFile();
        readFile();
        closeFile();

    }
    public Image getGrass(){
        return grass;
    }
    public Image getWall(){
        return wall;
    }
    public Image getFinish(){
        return finish;
    }

    public String getMap(int x , int  y){
        String index = Map[y].substring(x,x+1);
        return index;
    }

    public void openFile(){

        try{
        m = new Scanner(new File("C://Users//Rayjay//Desktop//Assets//Map.txt"));
        }
        catch(Exception e){
            System.out.println("Error Loading Map");
        }
    }

    public void readFile(){
        while(m.hasNext()){
            for(int i = 0; i < 14; i++){
                Map[i] = m.next();
            }
        }
    }

    public void closeFile(){
        m.close();
    }
}

Player class

package javamazegame;

import java.awt.Image  ;
import javax.swing.ImageIcon;

player class this my class for player public class Player {

    private int  tileX,tileY; 

    private Image player;

    public Player(){

        ImageIcon img = new ImageIcon("C://Users//Rayjay//Desktop//Assets//player.png");
        player = img.getImage();


        tileX = 1;
        tileY = 1;

    }
    public Image getPlayer(){
        return player;

    }


    public int getTileX(){
        return tileX; 

    }
    public int getTileY(){
        return tileY; 

    }

    public void move(int dx,int dy){      
        tileX += dx;
        tileY += dy;
    }
}

Upvotes: 0

Views: 18

Answers (1)

etienneZink
etienneZink

Reputation: 54

You could use the Timer class. There are several possibilities how you could work with this class. Here is the doc to Timer.

Upvotes: 0

Related Questions