0
\$\begingroup\$

Does anybody know the best method for taking game data and setting up java to read the data then print it to the window... of which you want the game loop to start the game and read the data...

Here is what my code is right now mabe some advice would be in order..

import java.io.File;
import java.util.Scanner;

import DATA_Data.Data.readfile;

public class ReadFile {

private Scanner x;


public void openFile() {

    try {

        x = new Scanner (new File("DATA_1.Pak"));
        x = new Scanner (new File("DATA_2.Pak"));
        x = new Scanner (new File("DATA_3.Pak"));
        x = new Scanner (new File("DATA_4.Pak"));
        x = new Scanner (new File("DATA_5.Pak"));
        x = new Scanner (new File("DATA_6.Pak"));
        x = new Scanner (new File("DATA_7.Pak"));
        x = new Scanner (new File("DATA_8.Pak"));
        x = new Scanner (new File("DATA.Pak"));
        x = new Scanner (new File("DATAA_1.Pak"));
        x = new Scanner (new File("DATAA_2.Pak"));
        x = new Scanner (new File("DATAA_3.Pak"));
        x = new Scanner (new File("DATAA_4.Pak"));
        x = new Scanner (new File("DATAA_5.Pak"));
        x = new Scanner (new File("DATAA_6.Pak"));
        x = new Scanner (new File("DATAA_7.Pak"));
        x = new Scanner (new File("DATAA_8.Pak"));
        x = new Scanner (new File("DATAA.Pak"));
        x = new Scanner (new File("DATAE_1.Pak"));
        x = new Scanner (new File("DATAE_2.Pak"));
        x = new Scanner (new File("DATAE_3.Pak"));
        x = new Scanner (new File("DATAE_4.Pak"));
        x = new Scanner (new File("DATAE_5.Pak"));
        x = new Scanner (new File("DATAE_6.Pak"));
        x = new Scanner (new File("DATAE_7.Pak"));
        x = new Scanner (new File("DATAE_8.Pak"));
        x = new Scanner (new File("DATAE.Pak"));
        x = new Scanner (new File("DATAL_1.Pak"));
        x = new Scanner (new File("DATAL_2.Pak"));
        x = new Scanner (new File("DATAL_3.Pak"));
        x = new Scanner (new File("DATAL_4.Pak"));
        x = new Scanner (new File("DATAL_5.Pak"));
        x = new Scanner (new File("DATAL_6.Pak"));
        x = new Scanner (new File("DATAL_7.Pak"));
        x = new Scanner (new File("DATAL_8.Pak"));
        x = new Scanner (new File("DATAL.Pak"));



    }
    catch (Exception e) {

        System.out.println("Could not read file");


    }

}

    public void readFile() {

        while(x.hasNextLine()) {


            String a = x.next();
            String b = x.next();
            String c = x.next();




                System.out.printf("%s %s %s\n", a,b,c);


        }


    }

        public void closeFile() {


            x.close();

        }




public static void main(String[]args) {

 readfile r = new readfile();

 r.openFile();
 r.readFile();
 r.closeFIle();

}

}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You first need to start with creating your game loop. Consider something like this below. It will loop every second by default(60fps), but you're able to adjust this. Now you're able to keep checking for your .Pak file(s) in the render() loop.

    public class Game {

 static boolean running = false;
 private Thread thread;


private Game(){

    //no other functions should be here
    initializeGame();
}


private void initializeGame() {
    //Create your game objects and get input    
}

private synchronized void startGame() {
    if(running){
        System.out.println("[DEBUG] Already running");
        return;
    }

    thread = new Thread();
    running = true;
    thread.start(); 
    run();
}

public void run(){
    long lastTime = System.nanoTime();
    final double ticks = 60.0;
    double ns = 1000000000 / ticks;
    double delta = 0;
    int updates = 0;
    int frames = 0;
    long timer = System.currentTimeMillis();

    //Game Loop
    while(running){

        long now = System.nanoTime();
        delta += (now - lastTime)/ns;
        lastTime = now;

        if(delta >= 1){
            tick();
            delta--;
            updates++;
        }
        render();
        frames++;

        if(System.currentTimeMillis() - timer >= 1000){
            timer += 1000;

            frames = 0;
            updates = 0;    
        }
    }
    stop();
}

//Everything that updates
private void tick(){


}
//Everything that renders 
private void render(){

}

private synchronized void stop() {
    if(!running){
        return;
    }

    running = false;
    try{
        //Pause thread
        thread.join();
    }catch(InterruptedException e){
        e.printStackTrace();
    }
    System.exit(1);

}

public static void main(String []args){

    Game game = new Game();
    game.startGame();

}

}

\$\endgroup\$
3
  • \$\begingroup\$ Thank you kind sir you helped me out so much thank you for taking your time it means alot thanks \$\endgroup\$
    – John Nay
    Mar 29, 2018 at 4:34
  • \$\begingroup\$ A question in the render loop do I specify my pak files in there or just call them somewhere else can you give a more better example in the render loop thanks again \$\endgroup\$
    – John Nay
    Mar 29, 2018 at 4:42
  • \$\begingroup\$ You could call a method in the render loop that would attempt to read your pak files. It's not immediately clear what you're trying to accomplish with trying the scanner on multiple file names. If you don't know the file name, you can just look in a certain directory for files of type .pak? \$\endgroup\$ Mar 29, 2018 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .