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

2017/2/10

Quiz - need help...

EdiPosteuca EdiPosteuca

2017/2/10

#
Ok, so I am REALLY new to programming, yet I started to make a program with a friend and we cannot quite figure it out. So, it is a quiz. We used images for questions/answers. My question is, how can I get the questions and the answers to succed each other randomly directly from the world?
EdiPosteuca EdiPosteuca

2017/2/10

#
 public void act() 
    {
       Actor a=this.getOneIntersectingObject(bullet.class);
       if(a!=null&&m==0)
       {
           this.getWorld().removeObject(a);
           this.setImage("x.png.18.jpg");
           m=1; 
           World myWorld = getWorld();
  
        myWorld.addObject(new Message2(),600,550);
        
        }
        m=1;
         if(Greenfoot.isKeyDown("space")&&m==1)
        {
            
            this.setImage("Python.png");
        }
        if(a!=null&&m==1)
       {
           this.getWorld().removeObject(a);
           this.setImage("x.png.18.jpg");
            
           World myWorld = getWorld();
  
        myWorld.addObject(new Message2(),600,550);
        
        }

}    
}
This is the code I used for one of the images. I have 3 different actors. The thing is, I used the same code for each whenever one of them is true. I tried to use that m variable u see there, but simply the one that was true the last time is true everytime. I really need help as soon as possible! Also, how can I randomize the questions?
EdiPosteuca EdiPosteuca

2017/2/10

#
By the way, the bullet is what is supposed to hit the --tiles-- with the questions.
danpost danpost

2017/2/10

#
Line 14 looks like it does not belong -- especially since you are checking to see if that variable holds the same value being set to it immediately following it on line 15.
Super_Hippo Super_Hippo

2017/2/10

#
I just created a sample. Run this code, look what it does and how it is done. If there are parts of the code which you do not understand, feel free to ask.
import greenfoot.*;

public class QuestionWorld extends World
{
    private String[] questions =
    {
        "How high is the Eiffel Tower?",
        "Which of the following is a fruit?",
        "What is the cube root of 166375?"
    };
    
    private String[][] answers = //first one = correct answer
    {
        {"324 m", "314 m", "334 m", "344 m"},
        {"Apple", "Tomato", "Cucumber", "Pumpkin"},
        {"55", "65", "75", "85"}
    };
    
    private Actor question = new Actor() {};
    private Actor[] answer;
    
    private int lastQuestion = -1;
    
    private int score = 0;
    
    public QuestionWorld()
    {    
        super(600, 400, 1);
        GreenfootImage bg = new GreenfootImage(getWidth(), getHeight());
        bg.setColor(new Color(0,255,255));
        bg.fill();
        setBackground(bg);
        addObject(question, getWidth()/2, 100);
        createNewQuestion();
        Greenfoot.start();
    }
    
    private void createNewQuestion()
    {
        if (answer != null)
        {
            for (int i=0; i<4; i++) removeObject(answer[i]);
        }
        answer = new Answer[4];
        int random;
        do
        {
            random = Greenfoot.getRandomNumber(questions.length);
        }
        while(random==lastQuestion);
        lastQuestion = random;
        
        question.setImage(new GreenfootImage(questions[random], 30, Color.BLACK, new Color(0,0,0,0)));
        question.setLocation(100+question.getImage().getWidth()/2, question.getY());
        int[] randomPosition = {-1, -1, -1, -1};
        for (int i=0; i<4; i++)
        {
            whileLoop: while (true)
            {
                int r = Greenfoot.getRandomNumber(4);
                for (int j=0; j<i; j++)
                {
                    if (randomPosition[j] == r) continue whileLoop;
                }
                randomPosition[i] = r;
                break;
            }
            answer[i] = new Answer(i==0, answers[random][i]);
            addObject(answer[i], 100+answer[i].getImage().getWidth()/2, 200+randomPosition[i]*30);
        }
        
    }
    
    private void result(boolean correct)
    {
        score += correct?1:-1;
        createNewQuestion();
    }
    
    
    private class Answer extends Actor
    {
        private boolean correct;
        private int timer;
        private String answer;
        
        public Answer(boolean correct, String answer)
        {
            this.answer = answer;
            this.correct = correct;
            updateImage(null);
        }
        
        private void updateImage(Color background)
        {
            Color c = background;
            if (c==null) c = new Color(0,0,0,0); //this line can be removed when Greenfoot 3.1.1 is released
            setImage(new GreenfootImage(answer, 20, Color.BLACK, c));
        }
        
        public void act()
        {
            if (Greenfoot.mousePressed(this))
            {
                timer = 55;
                updateImage(correct?Color.GREEN:Color.RED);
            }
            
            if (timer>0)
            {
                timer--;
                if (timer==0) result(correct);
            }
        }
    }
}
Super_Hippo Super_Hippo

2017/2/10

#
You can also change a few things and have the questions and answers in one array. I think this might be clever, so if you have a lot of questions, you still easily see which answer options are related to each.
import greenfoot.*;

public class QuestionWorld extends World
{
    private String[][] questions = //first one=question, second one=correct answer
    {
        {"How high is the Eiffel Tower?", "324 m", "314 m", "334 m", "344 m"},
        {"Which of the following is a fruit?", "Apple", "Tomato", "Cucumber", "Pumpkin"},
        {"What is the cube root of 166375?", "55", "65", "75", "85"}
    };
    
    private Actor question = new Actor() {};
    private Actor[] answer;
    
    private int lastQuestion = -1;
    
    private int score = 0;
    
    public QuestionWorld()
    {    
        super(600, 400, 1);
        GreenfootImage bg = new GreenfootImage(getWidth(), getHeight());
        bg.setColor(new Color(0,255,255));
        bg.fill();
        setBackground(bg);
        addObject(question, getWidth()/2, 100);
        createNewQuestion();
        Greenfoot.start();
    }
    
    private void createNewQuestion()
    {
        if (answer != null)
        {
            for (int i=0; i<4; i++) removeObject(answer[i]);
        }
        answer = new Answer[4];
        int random;
        do
        {
            random = Greenfoot.getRandomNumber(questions.length);
        }
        while(random==lastQuestion);
        lastQuestion = random;
        
        question.setImage(new GreenfootImage(questions[random][0], 30, Color.BLACK, new Color(0,0,0,0)));
        question.setLocation(100+question.getImage().getWidth()/2, question.getY());
        int[] randomPosition = {-1, -1, -1, -1};
        for (int i=0; i<4; i++)
        {
            whileLoop: while (true)
            {
                int r = Greenfoot.getRandomNumber(4);
                for (int j=0; j<i; j++)
                {
                    if (randomPosition[j] == r) continue whileLoop;
                }
                randomPosition[i] = r;
                break;
            }
            answer[i] = new Answer(i==0, questions[random][i+1]);
            addObject(answer[i], 100+answer[i].getImage().getWidth()/2, 200+randomPosition[i]*30);
        }
        
    }
    
    private void result(boolean correct)
    {
        score += correct?1:-1;
        createNewQuestion();
    }
    
    
    private class Answer extends Actor
    {
        private boolean correct;
        private int timer;
        private String answer;
        
        public Answer(boolean correct, String answer)
        {
            this.answer = answer;
            this.correct = correct;
            updateImage(null);
        }
        
        private void updateImage(Color background)
        {
            Color c = background;
            if (c==null) c = new Color(0,0,0,0); //this line can be removed when Greenfoot 3.1.1 is released
            setImage(new GreenfootImage(answer, 20, Color.BLACK, c));
        }
        
        public void act()
        {
            if (Greenfoot.mousePressed(this))
            {
                timer = 55;
                updateImage(correct?Color.GREEN:Color.RED);
            }
            
            if (timer>0)
            {
                timer--;
                if (timer==0) result(correct);
            }
        }
    }
}
You need to login to post a reply.