Zapisywanie wyników gry do pliku i wyświetlenie ich

0

Witam, mam taki problem. Nie mogę zapisać pliku z wynikiem po skończonej grze...jak to rozwiązać? Gdy uruchamiam w Eclipse z poziomu klasy GamePanel to gra sie odpala, ale nie tworzy sie plik, a gdy robie na odwrot to tworzy się plik, a gra się nie odpala...

Klasa GamePanel

import javax.swing.*;

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


public class GamePanel  extends JPanel implements KeyListener {

	private static final long serialVersionUID = 1L;
	
	public int WIDTH = 706, HEIGHT = 490;
	public static double score = 0; 
	
	//background
	private Component background1 = new Component(0, 0, 750, 490, "images//background.png");
	private Component background2 = new Component(700, 0, 750, 490, "images//background.png");
	//Bird
	private Component bird = new Component((WIDTH/ 2 - 150), (HEIGHT /2) - 40, 50 , 36, "images//plane.png");
	//Wall array
	private Component[] walls = new Component[8];
	//Ground
	private Component ground1 = new Component(0, 445, 750, 490, "images//ground.png");
	private Component ground2 = new Component(700, 445, 750, 490, "images//ground.png");
	//Wall speed
	private int speed = 2;
	//Game Controller
	private boolean started = false, gameOver = false; 
	
	
	//GamePanel Constructor
	public GamePanel(){
		super.addKeyListener(this);
		super.setFocusable(true);
	}
	
	//Create the wall and
	public void Start(){
		createWalls();
	}
	
	
	//Gives bird Some Gravity
	public void Gravity(){	
		
		bird.setY(bird.getY() + 3);						//Starts falling by One Pixel
		
		try{ Thread.sleep(3); } catch (Exception e) {}
		
		// checks if it touches the ground or Sky
		if(bird.getY() > 450 || bird.getY() <= 0){ 
			// Plays hit Sound and if bird touches anything then makes gameOver true.
			if(!gameOver) GameSound.hitSound();   
			gameOver = true;
			bird.setX(-500);
		}
		
		super.repaint();
	}
	
	
	
	
	//Helps wall to shift from left to right 
	public void scrollWalls(){ 
		
		for(int i = 0; i < walls.length; i+=2){
			if(walls[i].getX() > - 150){
				walls[i].setX(walls[i].getX() - speed);
				walls[i+1].setX(walls[i+1].getX() - speed);
			}
			else{
				walls[i].setX(700);
				walls[i+1].setX(700);
					
			}
		}
		
		
		try{ Thread.sleep(5); }  catch(Exception e) {}
		
		super.repaint();
	}
	
	
	
	//Helps wall to shift from left to right 
	public void scrollBG(){ 
			
		//Moves First BG Image
		if(background1.getX() > -700){
			background1.setX(background1.getX() - speed/2);
		} else { background1.setX(700);	}
		
		//Moves Second BG Image
		if(background2.getX() > -700){
			background2.setX(background2.getX() -speed/2);
		} else { background2.setX(700);	}
		
		try{ Thread.sleep(5); }  catch(Exception e) {}
		super.repaint();
	}
	
	
	
	//Helps Ground to shift from left to right 
	public void scrollGround(){ 
		
		//Moves First ground Image
		if(ground1.getX() > -700){
			ground1.setX(ground1.getX() - speed);
		} else {  ground1.setX(700); }
		
		//Moves First ground Image
		if(ground2.getX() > -700){	
			ground2.setX(ground2.getX() -speed);
		} else {  ground2.setX(700); }
		
		//interval
		try{ Thread.sleep(5); }  catch(Exception e) {}
		super.repaint();
	}
	
	
	//Draws All Components on panel
	public void paint(Graphics g){
		
		//Draws backgrounds
		background1.draw(g);
		background2.draw(g);
		
		//Draws Bird
		bird.draw(g);
		
		//Draws Walls
		for(int i = 0; i < walls.length; i++){
			walls[i].draw(g);
		}
	
		////Draws Texts
		if (gameOver) {	
			g.setFont(new Font("Arial", 1, 40));
			g.setColor(Color.red.brighter().brighter().brighter());
			g.drawString("Game Over!", 235, HEIGHT / 2 - 50);
		} 
		
		if(!started || gameOver){
			g.setColor(Color.white);
			g.setFont(new Font("Arial", 1, 20));
			g.drawString(String.valueOf("Wciśnij spację, aby rozpocząć"), 10 , 25);
		}
		
		else if (!gameOver && started){	
			g.setColor(Color.red);
			g.setFont(new Font("Arial", 1, 20));
			g.drawString(String.valueOf("Score: " +score), 10 , 25);
		}
		
		//Draws Grounds
		ground1.draw(g);
		ground2.draw(g);
		
	}
	
	
	//Checks Collisions between wall and bird
	public void checkCollision(){
		
		//Creates bound of bird
		Rectangle birdBound = new Rectangle( bird.getX(), bird.getY(),25,25);
		
		//Creates bound of all walls
		for(int i = 0; i < walls.length; i++){
			Rectangle WallRect=new Rectangle(walls[i].getX(),walls[i].getY(),walls[i].getWidth(),walls[i].getHeight());
			if(WallRect.intersects(birdBound)==true){
				bird.setX(-500);
				if(!gameOver) GameSound.hitSound();
				gameOver = true;
				break;
			} else if(bird.getX() == walls[i].getX()+1){
				//if bird passes one wall then counts a point
				score();
			}	
		}
		
	}
	
	
	//When any key press this code executes 
	public void keyPressed(KeyEvent e) {
		
		if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_SPACE){
			//Starts the game
			if(!started) {
				started = true;
			}
			
			//Restarts The Game if its Over
			if(gameOver){
				System.exit(0);
			}
				
			//Flaps if key pressed
			BirdFlap flap = new BirdFlap(this,bird);
			flap.start();
			GameSound.flapSound();
		}
	
	}
	
	
	//Increments the score
	public void score(){
		score +=10;
		GameSound.pointSound();
	}

	public void speed(){
		if(score == +20){
			speed += 2;
		}
		else {
			speed = 0;
		}
	}
	
	public void createWalls(){
		int i = 0;
		int x = 600; // First walls distance from very right side
		
		//walls width and height, width is static and height changes constantly
		int width = 60, height = 280; 
		
		
		//1st & 2nd wall
		walls[i] = new Component(x, 280, width, height, "images//wallUp.png");
		i++;
		walls[i] = new Component(x, -90, width, height, "images//wallDown.png");
		x = walls[i].getX()+220; // 160 is the difference between wall
		i++;
		
		//3rd & 4th wall
		walls[i] = new Component(x, 360, width, height, "images//wallUp.png");
		i++;
		walls[i] = new Component(x, -10, width, height, "images//wallDown.png");
		x = walls[i].getX()+220;
		i++;
		
		
		//5rd & 6th wall
		walls[i] = new Component(x, 280, width, height, "images//wallUp.png");
		i++;
		walls[i] = new Component(x, -90, width, height, "images//wallDown.png");
		x = walls[i].getX()+220;
		i++;
		
		
		//7rd & 8th wall
		walls[i] = new Component(x, 220, width, height, "images//wallUp.png");
		i++;
		walls[i] = new Component(x, -170, width, height, "images//wallDown.png");
		x = walls[i].getX()+220;
		
	}
	

	
	//Setters anmd Getters
	public boolean isStarted() {
		return started;
	}

	public void setStarted(boolean started) {
		this.started = started;
	}

	public boolean isGameOver() {
		return gameOver;
	}

	public void setGameOver(boolean gameOver) {
		this.gameOver = gameOver;
	}
	
	
	//Key Events
	@Override
	public void keyReleased(KeyEvent e) {
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

}

A tutaj moja klasa, która zapisuje wynik:

 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
 

public class WriteString  {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        FileWriter fileWriter = null;
        try {
            String content = "Hello! Java-Buddy :)";
            File newTextFile = new File("C:/Users/szymo/workspace/FlappyBird/test.txt");
            fileWriter = new FileWriter(newTextFile);
            fileWriter.write(content);
            fileWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(WriteString.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fileWriter.close();
            } catch (IOException ex) {
                Logger.getLogger(WriteString.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
0

Przecież w Twojej klasie GamePanel nie ma nigdzie zapisu do pliku. Oczekujesz, że program magicznie domyśli się, że ma wywołać coś z innego maina ?

Stwórz w GamePanelu jakąś metodkę, która będzie wywoływana np. poprzez naciśnięcie buttona i w niej umieść logikę zapisu do pliku.

1 użytkowników online, w tym zalogowanych: 0, gości: 1