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

2016/6/1

Score table problem

Juwen Juwen

2016/6/1

#
Hello, i'm actually doing a project for my school where i have to create mazes with mouse. Then I have different level of maze and differents mouse who are more or less 'intelligent'. So, the user can choose the level and mouse ; then, in order to show some statistics i need a score table. To do that, i have a timer, but i don't know how to send the result of the timer in my tab ; i have read things about userInfo and stuff but i don't really understant how can i sent more than one value. (Sorry for my english by the way)! This is how my timer and tab is implemented :
public class Score extends Actor
{
    int aLevel, aIntel;
    int scoreTab[][] = new int[7][6];
    Button menu = new Button("menu.png");
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }

    public Score()
    {
        updateScore();
    }

    public void setScore(final int time, final int pLevel, final int pIntel)
    {
        scoreTab[pLevel][pIntel] = time;
        updateScore();
    }

    private void updateScore()
    {
        GreenfootImage image = new GreenfootImage(1100, 600);
        image.fill();
        GreenfootImage title = new GreenfootImage("Tableau des temps", 50, Color.white, new Color(0, 0, 0, 0));
        image.drawImage(title, 350, 50);
        for(int i=0;i<7;i++)
        {
            if(i>0)scoreTab[i][0] = i;
            for (int j=0;j<6;j++)
            {
                if(j>0)scoreTab[0][j] = j;
                if(i != 0 || j != 0)
                {
                    GreenfootImage textImage = new GreenfootImage(scoreTab[i][j]+"", 40, Color.white, new Color(0, 0, 0, 0));
                    image.drawImage(textImage, ((2*i+1)*75), (2*j+1)*30+150);
                    setImage(image);
                }
            }
        }
    }
}
So, in this tab which looks like a matrix, i would like to send all my timer's stat into it. Thanks :)!
danpost danpost

2016/6/2

#
There does not seem to be much (if anything) wrong with the given Score class. I think you need to show the code of the class that will be "sending' the times to this actor.
Juwen Juwen

2016/6/2

#
Well, i know that but i have absolutly no idea how "send" the times! As i said i have read things about UserInfo but i'm not sure that i have to use that... Have I to implemented it in my class timer ? I tried to use a "saver class"
import greenfoot.*;
public class Saver
{
    private UserInfo player;
    /**
     * Constructor for objects of class Saver
     */
    public Saver()
    {
        if (UserInfo.isStorageAvailable()) {
            if (UserInfo.getMyInfo() != null) {
                player = UserInfo.getMyInfo();
            }
        }
    }
    
    public void saveTime(int time)
    {
        if (UserInfo.isStorageAvailable()) {
            if (player != null) {
                if (time > player.getScore()) {
                    player.setScore(time);
                    player.store();
                }
            }
        }
    }
}
But i'm a bist lost with my timer class to use it, send it....
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;

/**
 * Write a description of class Timer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Timer extends Actor
{
    private int sec = 0, min = 0, count=0;
    private String ssec = "00", smin = "0";
    private boolean aPause = false;
    /**
     * Act - do whatever the Timer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(aPause == false)updatimer();
    }    
    public Timer()
    {
        Initimer();
    }
    private void Initimer()
    {
        GreenfootImage textImage = new GreenfootImage(smin + " : " + ssec, 20, Color.white, new Color(0, 0, 0, 0));
        GreenfootImage image = new GreenfootImage(textImage.getWidth()+20, textImage.getHeight()+10);
        image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, (image.getHeight()-textImage.getHeight())/2);
        setImage(image);
    }
    private void updatimer()
    {
        count++;
        if(count==9)
        {
            count=0;sec++;
        }
        if(sec ==60){ sec = 0; min++;}
        ssec = ""+sec;
        if(sec <= 9) ssec = "0" + sec;
        smin = ""+min;
        Initimer();
    }
    public int getTime()
    {
        return sec + 60*min;
    }
    public void stoptimer()
    {
        this.aPause = true;
    }
}
Sorry for the code which is a bit long and thanks for the help!
danpost danpost

2016/6/2

#
Like I said before, I doubt you will want to use the UserInfo class (it is basically broken now anyway); and the Saver class is not needed. What is needed is for you to create a Score object and keep a reference to it in your World subclass. You can also add a method to the class to 'get' the Score object created. Then, the Timer class can get the Score object from the world and set the appropriate times using the 'setScore' method -- one call for each score to be saved.
You need to login to post a reply.