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

2021/3/20

Why doesn't this code write on the background on the screen?

AnJoMorto AnJoMorto

2021/3/20

#
I'm trying to display the best player name and time, so the player that has the lowest score and wrote this:
import greenfoot.*;
import java.io.*;
import java.util.ArrayList;

public class ArcScoreBoard extends Menu
{

    ArrayList<String> lines = new ArrayList<String>();
    
    public ArcScoreBoard()
    {
        
        GreenfootImage background1;
        Font font1 = new Font("Defaut1", false, false, 24);
        background1 = getBackground();
       
        background1.setFont(font1);
        
        background1.setColor(Color.BLACK);
        
        try
        {
                    
            FileReader fr = new FileReader("ArcadeScores.txt");
            BufferedReader br = new BufferedReader(fr);
            //String line = br.readLine();
            
            String line = null; // sets up a local field for each line of text
            while ((line = br.readLine()) != null)  lines.add(line); // read each line and add them to the 'lines' list
            for(int i = 0; i < lines.size(); i++)
            {
                
                String[] firstPlace  = null;
                
                String[] fullLine = lines.get(i).toString().split(";");
                int score = Integer.parseInt(fullLine[0]);
                                    
                if(firstPlace == null && score == i)
                {
                
                    firstPlace = fullLine;
                   background1 .drawString("1." + firstPlace[1] + firstPlace[2], 50, 50);
                    
                }

            }
            
        }
        catch(IOException e)
        {
            
            System.err.println("Caught IOException: " + e.getMessage());
            
        }
        
        
        
    }
and also tried this:
import greenfoot.*;
import java.io.*;
import java.util.ArrayList;

public class ArcScoreBoard extends Menu
{

    ArrayList<String> lines = new ArrayList<String>();
    
    public ArcScoreBoard()
    {
        
        GreenfootImage background1;
        Font font1 = new Font("Defaut1", false, false, 24);
        background1 = getBackground();
       
        background1.setFont(font1);
        
        background1.setColor(Color.BLACK);
  
    }
    
    public void act(){
        
        try
        {
                    
            FileReader fr = new FileReader("ArcadeScores.txt");
            BufferedReader br = new BufferedReader(fr);
            //String line = br.readLine();
            
                            
            String[] firstPlace  = null;
            
            String line = null; // sets up a local field for each line of text
            while ((line = br.readLine()) != null)  lines.add(line); // read each line and add them to the 'lines' list
            for(int i = 0; i < lines.size(); i++)
            {

                String[] fullLine = lines.get(i).toString().split(";");
                int score = Integer.parseInt(fullLine[0]);
                                    
                if(firstPlace == null && score == i)
                {
                
                    firstPlace = fullLine;
                    getBackground().drawString("1." + firstPlace[1] + firstPlace[2], 50, 50);
                    
                }

            }
            
        }
        catch(IOException e)
        {
            
            System.err.println("Caught IOException: " + e.getMessage());
            
        }
}
    
}
And in the ArcadeScores.txt file there's this:
824;Player1;00:00:03.82
236;Player2;00:00:01.56
654;Player3;00:02:03:36
686;Player4;00:64:65.00
986;Player5;00:05:23.65
355;Player6;00:00:51.67
that I randomly added manually Can anyone help me make this work please?
danpost danpost

2021/3/20

#
Please provide Menu class codes, as well.
AnJoMorto AnJoMorto

2021/3/21

#
danpost wrote...
Please provide Menu class codes, as well.
So... I know it's not very professional, but the Menu class is empty, I just used it to sort the subclasses of the Title Screen class here:
import greenfoot.*;

public class TitleScreen extends World
{

    //MUSIC
    public static GreenfootSound bgMusic;
    
    /**
     * Constructor for objects of class TitleScreen.
     * 
     */
    public TitleScreen()
    {    
        
        super(Game.WIDE, Game.HIGH, Game.CELL, false);
        addObject(new Reference(), -50, -50);
        
    }    
    
    /**
     * Act - do whatever the Class wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        
        if(!this.getObjects(Reference.class).isEmpty()){
        
            if(bgMusic == null) //Play music when the a new world of class TitleScreen is set and bgMusic isn't defined yet
            {
            
                bgMusic= new GreenfootSound("soundtrackCM12.wav");
                bgMusic.setVolume(50);
                bgMusic.playLoop();
                
            } else {
            
                bgMusic.playLoop();
            
            }
            
        }
    
    }
    
    /**
     * This method is called by the Greenfoot system when the execution has started.
     */
    public void started()
    {
           
        if(bgMusic == null) //Play music when the a new world of class TitleScreen is set and bgMusic isn't defined yet
        {
        
            bgMusic= new GreenfootSound("soundtrackCM12.wav");
            bgMusic.setVolume(50);
            bgMusic.playLoop();
            
        }
    
    }
    
    /**
     * Forces the game to play the music if the bgMusic hasn't been set yet
     */
    public void playMusic()
    {
    
        if(bgMusic == null)
        {
        
            bgMusic= new GreenfootSound("soundtrackCM12.wav");
            bgMusic.setVolume(50);
            bgMusic.playLoop();
            
        }
    
    }
    
    /**
     * Stop music from playing
     */
    public void stopMusic()
    {
        
        bgMusic.stop();
        
    }
    
    /**
     * Starts Arcade mode game
     */
    public void startArcade()
    {
    
        Greenfoot.setWorld(new ArcadeWorld());
        stopMusic();
    
    }
    
    /**
     * Starts Classic mode game
     */
    public void startClassic()
    {
    
        Greenfoot.setWorld(new ClassicWorld());
        stopMusic();
    
    }
    
    /**
     * Opens a new "Help" screen
     */
    public void help()
    {
    
        //Greenfoot.setWorld(new HelpScreen());
    
    }
    
    /**
     * Replays startitng animation
     */
    public void restartAnim() 
    {
    
        Greenfoot.setWorld(new A_StaticBeginning());
    
    }
    
    /**
     * Opens a new Score Board screen
     */
    public void openScoreBoard()
    {
    
        Greenfoot.setWorld(new ArcScoreBoard());
    
    }
    
    /**
     * Opens a new Selection Screen
     */
    public void backToMainMenu()
    {
    
        Greenfoot.setWorld(new SelectionScreen());
    
    }
    
}
There's also an act method here but if I understood correctly, if a subclass has an act method the main class' act is ignored right?
danpost danpost

2021/3/21

#
AnJoMorto wrote...
the Menu class is empty,
Please provide Menu class code anyway.
There's also an act method here but if I understood correctly, if a subclass has an act method the main class' act is ignored right?
Correct.
AnJoMorto AnJoMorto

2021/3/21

#
danpost wrote...
AnJoMorto wrote...
the Menu class is empty,
Please provide Menu class code anyway.
Here it is:
import greenfoot.*;

public class Menu extends TitleScreen
{
/**
     * Constructor for objects of class Menu.
     * 
     */
    public Menu()
    {    
        
        
        
    }
   
}
AnJoMorto AnJoMorto

2021/3/21

#
Found a solution, turns out I'm just dumb:
import greenfoot.*;
import java.io.*;
import java.util.List;
import java.util.ArrayList;

public class ArcScoreBoard extends Menu
{

    ArrayList<String> lines = new ArrayList<String>();
    ArrayList<String> topPl = new ArrayList<String>();
    
public ArcScoreBoard()
    {
        
        GreenfootImage background1 = new GreenfootImage("backgroundSB.jpg");
        Font font1 = new Font("Dialogue", false, false, 40);
       
        background1.setFont(font1);
        
        background1.setColor(Color.BLACK);
        
        setBackground(background1);
        
        background1.drawString("BEST ARCADE MODE PLAYERS", 50, 50);
        background1.drawString("Player Name: ", 50, 150);
        background1.drawString("Time: ", Game.WIDE/2, 150);
        
        String[] top         = null;                
        String[] firstPlace  = null;
        int total = 0;
        int max = 0;
        
        try
        {
                    
            FileReader fr = new FileReader("ArcadeScores.txt");
            BufferedReader br = new BufferedReader(fr);
            //String line = br.readLine();
            
            String line = null; // sets up a local field for each line of text
            while ((line = br.readLine()) != null)  lines.add(line); // read each line and add them to the 'lines' list
            for(int i = 0; i < lines.size(); i++)
            {

                String[] fullLine = lines.get(i).split(";");
                int score = Integer.parseInt(fullLine[0]);
                total = total + score;
                
                for(int j = 0; j < lines.size(); j++)
                {
                
                    fullLine = lines.get(j).split(";");
                    score = Integer.parseInt(fullLine[0]);
                    
                    if(score < total){topPl.add(String.valueOf(score)); max = Integer.parseInt(topPl.get(0));}
                    
                    for(int k = 0; k < lines.size(); k++)
                    {
                
                        fullLine = lines.get(k).split(";");
                        score = Integer.parseInt(fullLine[0]);
                        
                        if(score < max){topPl.clear(); topPl.add(String.valueOf(score)); max = Integer.parseInt(topPl.get(0));
                        firstPlace = fullLine;}
                    
                    }
                        
                }

                //System.out.println(max);

            }
            
        }
        catch(IOException e)
        {
            
            System.err.println("Caught IOException: " + e.getMessage());
            
        }
        
        if(firstPlace != null)
        {
        
            background1.drawString("1. " + firstPlace[1], 50, 250);
            background1.drawString(firstPlace[2].strip(), Game.WIDE/2, 250);
            //System.out.println("1. " + firstPlace[1] + " : " + firstPlace[2]);
        
        } else
        {
        
            background1.drawString("NO SCORE AVAILABLE", Game.WIDE/2, Game.HIGH/2);
        
        }
        
    }
You need to login to post a reply.