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

2018/3/26

''Simon Game'' - How can I make it, so the ''AI'' lets buttons light up?

1
2
danpost danpost

2018/3/27

#
JFox wrote...
Should I create a detectClick method for every individual button, so that they can individually swap out the images or should I put it like in the code i posted.
The getButton will only work properly with a one-character string. Okay, scratch the detectClick method for the time being. Maybe you should start in the act method -- from square one. Currently there is no sequence string; so, add a String field for it. Also, I do not think you have anything to control the phase yet either, so add a boolean field to toggle the phases. A third field you will need is the pointer for the character array of the sequence string. These three would be as follows:
1
2
3
String sequence = "";
int index;
boolean isPhaseOne;
Now that everything is in place, you can start coding the game itself, starting in the act method. What is the first thing that will need done at each act step? The actions for the different phases will need to be separated. So, the first build on the act method would be:
1
2
3
4
public void act()
{
    if (isPhaseOne) phaseOne(); else phaseTwo();
}
Next, you will create the phaseOne and phaseTwo methods. In both of these methods, you will need to ask if the conditions are right to change the phase and perform phase changing actions. You may not know what conditions to ask about quite yet. So, at least note that these codes will be inserted at a later time. Continue in the same manner, asking what needs to be done during each step under the given conditions and build the code. You can be very general at first, like I was with the act method, and get down to the gritty stuff later.
JFox JFox

2018/3/27

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import greenfoot.*;
 
public class Simon extends World
{
     
    Actor[] buttons = {new RedButton(), new BlueButton(), new YellowButton(), new GreenButton()};
    String btnSet = "RBYG";
    String sequence = "";
    int index;
    boolean isPhaseOne;
     
    public Simon()
    {   
        super(600, 600, 1);
        addObject(getButton("R"),420,179);
        addObject(getButton("G"),179,179);
        addObject(getButton("Y"),179,420);
        addObject(getButton("B"),420,420);
    }
 
    public void act()
    {
        if (isPhaseOne)
        phaseOne();
        else
        phaseTwo();
    }
     
    public void phaseOne()
    {
        if (Greenfoot.mouseClicked(this))
      {
        Greenfoot.stop();
        }
    }
     
    public void phaseTwo()
    {
        if (!Greenfoot.mouseClicked(this))
      {
         
        }
    }
     
    private Actor getButton(String color)
    {
     return buttons[btnSet.indexOf(color)];
    }
     
}
Ok so I added the three new lines of code + the new act method. Next I added the two new methods phaseOne and phaseTwo. For phaseOne I added that if (it) gets clicked, Greenfoot will stop, just for testing purposes. For the second method I thought it would make sense to add the oposite, so if nothing is clicked. So far so good? :D
danpost danpost

2018/3/27

#
JFox wrote...
<< Code Omitted >> Ok so I added the three new lines of code + the new act method. Next I added the two new methods phaseOne and phaseTwo. For phaseOne I added that if (it) gets clicked, Greenfoot will stop, just for testing purposes. For the second method I thought it would make sense to add the oposite, so if nothing is clicked. So far so good? :D
Well, no. You want to check for clicks in phase two -- not phase one. It really makes no sense to check the opposite in the other one. No mouse actions at all will be used during phase one.
JFox JFox

2018/3/27

#
Ah ok, I think I get what you mean. So is this better?
1
2
3
4
5
6
7
8
9
10
11
12
public void phaseOne()
    {
         
    }
      
    public void phaseTwo()
    {
        if (Greenfoot.mouseClicked(this))
      {
         Greenfoot.stop();
        }
    }
danpost danpost

2018/3/27

#
JFox wrote...
Ah ok, I think I get what you mean. So is this better? << Code Omitted >>
Getting better; but, it is not the world background you want to check for a click on -- it is one of the buttons. So, use null instead of this, which will then have it indicate that you need to check which button, if any, was clicked.
JFox JFox

2018/3/28

#
Ah, ok! So if I would type in (Greenfoot.mouseClicked(this)), then it would only work if I would press the world, because this is describing the world Simon. Makes sense! :D So I now fixed it:
1
2
3
4
5
6
7
8
9
10
11
12
public void phaseOne()
    {
          
    }
       
    public void phaseTwo()
    {
        if (Greenfoot.mouseClicked(null))
      {
         Greenfoot.stop();
        }
    }
and if I now press a button, Greenfoot stops. Perfect! So, I would guess that the next step would be to define what is going to happen, if something gets pressed? Or is there something else?
danpost danpost

2018/3/28

#
JFox wrote...
So, I would guess that the next step would be to define what is going to happen, if something gets pressed? Or is there something else?
Correct.
JFox JFox

2018/3/28

#
Hi, just wanted to say that I won't be able to work on the program for the next day /two days or so, just to let you know. I don't want to be rude and just stop posting without saying anything :)
JFox JFox

2018/4/3

#
Hey again, Im back haha so, over the last days I figured out how i could create the game and im now pretty far. However, I wanted to ask if there still is a possibility of storing a highscore, even if it is just until the reset button is pressed. I tried to use the Greenfoot Scoreboard, however it seems to be broken or something, because I always get a error message. Thanks for helping :) Edit: I want to add that I just want a text saying "Highscore: (score)", not a whole leaderboard.
Vercility Vercility

2018/4/3

#
It's not broken, greenfoot just changed how color works so you need to delete java.awt.color from the class and instead import greenfoot.color or something like that. Dan should know more.
danpost danpost

2018/4/3

#
Vercility wrote...
It's not broken, greenfoot just changed how color works so you need to delete java.awt.color from the class and instead import greenfoot.color or something like that..
You do not need to import anything extra. The:
1
import greenfoot.*;
code line will include importing the greenfoot.Color and greenfoot.Font classes. @JFox, add a static int highScore field to your world class and at game over compare to check if game score is higher than highScore value; if so, set a new highScore value. A GameOver actor can be used to display the high score (possibly along with the game score) after the check. You can also add (override) a public void started method in your world class to reset the game only if a GameOver object is in the world.
JFox JFox

2018/4/4

#
Ok, thanks for helping! It worked :)
You need to login to post a reply.
1
2