This site requires JavaScript, please enable it in your browser!
Greenfoot back
ant
ant wrote ...

2019/1/17

Highscores

ant ant

2019/1/17

#
Hey I need help for storing my top high scores in a file and displaying them. Any advice, this is what I have so far but it's not quite working
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;

public class City extends World {
    
    int PIPE_SPACING = 200;
    int counter = 0;
    int counter1 = 0;
    int score = 0;
    int firstPipe = 215;
    ScoreBoard scoreText = null;
    
    public City() {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1250, 750, 1, false); 
        
        setPaintOrder(ScoreBoard.class, GameOver.class, LeaderboardButton.class, RateButton.class, StartButton.class, Bird.class, TopPipe.class, BottomPipe.class);
               
        //Adding the bird into the game
        Bird flappy = new Bird();
        addObject(flappy, getWidth()/2 - 100, getHeight()/2);
                
        scoreText = new ScoreBoard();
        scoreText.setScore(0);
        addObject(scoreText, 825, 300);
    }
    public void act() {
        scrollBackground();
        
        counter++;
    
        BottomPipe bp1 = new BottomPipe();
        TopPipe tp2 = new TopPipe();
        
        GreenfootImage image = bp1.getImage();
    
        if (counter % 110 == 0) {
            addObject(bp1, getWidth(), getHeight() / 2 + image.getHeight() / 2 + Greenfoot.getRandomNumber(image.getHeight() / 2));
            addObject(tp2, bp1.getX(), bp1.getY() - image.getHeight() - PIPE_SPACING);
        
        }
        
        if (counter >= firstPipe) {
            if (counter1 % 110 == 0) {
                score++;
                Greenfoot.playSound("ping.mp3");
                scoreText.setScore(score);
            }
            counter1++;
        }
        
    }
    private void scrollBackground()
    {
    GreenfootImage bg = new GreenfootImage(getBackground());
    getBackground().drawImage(bg, -1, 0);
    getBackground().drawImage(bg, getWidth() - 1, 0);
    }
    
    
    
}
 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Bird extends Actor {
    
    int GRAVITY = 5;
    int VERTICAL_SPEED = -15;

    public Bird() {    
        
        GreenfootImage image = getImage();  
        image.scale(70, 49);
        setImage(image);
        
    }
    public void act() 
    {
        move();
        checkHit();
    }
    public void move() {
        
        if(Greenfoot.mouseClicked(null)) {
            
            GRAVITY = VERTICAL_SPEED;  
            
        }
        else if(Greenfoot.isKeyDown("space")||Greenfoot.isKeyDown("up")) {
            
            GRAVITY = VERTICAL_SPEED / 2;
            
        }
        
        if(GRAVITY != 5) {
            
            GRAVITY++; 
            
        }
        
        setLocation(getX(), getY() + GRAVITY);
        
    }
    public void checkHit() {
        if((isTouching(BottomPipe.class)) || (isTouching(TopPipe.class) || (getY() > getWorld().getHeight()))) {
          Greenfoot.playSound("nani.mp3");  
          GameOver g1 = new GameOver(); 
          getWorld().addObject(g1,getWorld().getWidth() /2, getWorld().getHeight()/2 - 100);
          
          StartButton s2 = new StartButton();
          getWorld().addObject(s2,getWorld().getWidth() /2, getWorld().getHeight()/2 + 150);
          
          getWorld().removeObject(this);
          
          City c1 = new City();
          //System.out.println(c1.score);
          LeaderboardScreen l1 = new LeaderboardScreen();
          l1.act(c1.score);
          Greenfoot.stop();
          
        }
    }    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ScoreBoard here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ScoreBoard extends Actor
{
    public ScoreBoard() {
            
            GreenfootImage img = new GreenfootImage(500, 250);
            setImage(img);
            
    }
    public void act() 
    {
    } 
    public void setScore(int score) {
        
        GreenfootImage img = getImage();
        img.clear();
        
        Font f = new Font("Invasion2000", false, false, 50);
        img.setFont(f);
        
        img.setColor(Color.WHITE);
        img.drawString("" + score, 30, 30);
        setImage(img);
    }
    
}
data17 data17

2019/1/17

#
Try with this write method:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.nio.file.* ;

private void writeHighScore(int highScore)
    {
        Path file = Paths.get("highScore.dat");

        try (DataOutputStream dos = new DataOutputStream(Files.newOutputStream(file))) {
            dos.writeInt(highScore); 
            // dos.writeUTF(playerName); // here you can store the player name as a string format
        } catch (IOException ioe) {
            System.err.println(ioe);
            System.exit(1);
        }
    }
data17 data17

2019/1/17

#
With this method you can read the highscore file:
    private void getHighScore() {

        Path file = Paths.get("highScore.dat"); // the file is stored in your project folder root

        try (DataInputStream dis = new DataInputStream(Files.newInputStream(file))) {

            highScore = dis.readInt();
            // highScorePlayerName = dis.readUTF(); // read secpnd line in the file to get the player name

        } catch (IOException ioe) {
            System.err.println(ioe);
        }
    }
You need to login to post a reply.