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

2017/7/28

Squares, colors, and loops

1
2
IDaivdJr IDaivdJr

2017/8/1

#
Yes it works now, Thanks! I'll let you know if i need something else.
IDaivdJr IDaivdJr

2017/8/2

#
So now how do i implement the levels to the game. I want: if the user clicks the square that flashed, it goes to the next level.
Super_Hippo Super_Hippo

2017/8/2

#
You can add this to the world:
public void nextLevel()
{
    Greenfoot.setWorld(new MyWorld(++level));
}
and this to the Grid's act method:
if (Greenfoot.mousePressed(this))
{
    if (flashing>0) ((MyWorld) getWorld()).nextLevel();
    else Greenfoot.stop();
}
You will notice that you get the next level whenever you click one which flashes/flashed. The problem is that at higher levels, more than one is flashing so you have to think about how you want to make it so that you have to click all of them.
IDaivdJr IDaivdJr

2017/8/5

#
Can i change the code so one square flashes at a time?
danpost danpost

2017/8/5

#
IDaivdJr wrote...
Can i change the code so one square flashes at a time?
It is your project. You can do what you want. How do you think you can accomplish that?
IDaivdJr IDaivdJr

2017/8/5

#
I know that im going to have to use booleans and for loops in MyWorld. Im just not quite sure how to do it.
danpost danpost

2017/8/5

#
IDaivdJr wrote...
I know that im going to have to use booleans and for loops in MyWorld. Im just not quite sure how to do it.
I am not sure about needing booleans and for loops. I think the Grid square code needs modifying so that you can change the flashing state of each one. Also, maybe the world should set each flashing and detect the clicks on each (detect for a click on the last one set flashing and clicks not on that particular one). You will also need a counter in the world that is initially set to the level number. It should count down when a click is detected on the last flashing grid. When decremented, if not zero, set a new Grid flashing and save a reference to it (a reference to the Grid obj should be maintained in the world class which is also used to detect the click on it); if zero, go to the next level. Does that sound more like what you are wanting?
IDaivdJr IDaivdJr

2017/8/5

#
So after the first level, i would like the squares to flash one after another, not all at the same time. Once its the users turn, i want it so when the user clicks on the square it flashes, then it lets the user click on the next square, and yes i would like it to not be in a particular order.
danpost danpost

2017/8/5

#
I am not actually sure what you are trying to do. If you have only one square flashing at a time, it does not seem that it matters that you have levels. It is like there would be no difference to have each level flash one grid each; unless you are wanting that a new grid flashes after 5 seconds regardless of any click on the last flashing grid -- or something like that. Maybe you should explain, completely, and in detail, exactly how you want your game to work. It is difficult to give correct advise otherwise (as well as taking much longer to get it how you want).
danpost danpost

2017/8/5

#
So, you are wanting a random sequence of flashes that the user must duplicate, in the correct order? -- like Simon Sez?
IDaivdJr IDaivdJr

2017/8/5

#
Okay, so level 1 there will only be one square flashing, once it is done flashing the user has to click on the square that flashes. Then it goes to level two, i want to add a score board and a level counter on the screen but i can do that at a later time. So, on level two, i want one square to flash for about five seconds and stop, then the next square will flash for five seconds and then it will stop. The user will have to click on the squares that were flashing in no particular order if possible. third level there will be three squares, fourth level four squares, fifth level five squares and the procedure will be the same as levels 2,3. Sixth level i would like again if possible only five squares flashing, and yes one at a time. Do you think its more efficient for all squares to flash at the same time or one after another.
danpost danpost

2017/8/5

#
IDaivdJr wrote...
Do you think its more efficient for all squares to flash at the same time or one after another.
Well, there are advantages and drawbacks either way. If all at one, you save time; but, allow pattern recognition to aid the user in clicking the appropriate squares. If seperately, it would be time consuming to flash the squares (especially when flashing each for 5 seconds, which seems like an exagerated amount of time to begin with); but, hides the pattern, making it more difficult to solve (however, limiting to 5 squares favors the user as well). With Simon Sez, each grid flashes just one for a fairly short time and the user must duplicate the sequence, in order. Also, the sequence remains the same, but increases in length as the levels increase. Looks like you are not employing either of these, in that you are employing sets, not sequences; and the sets for one level is not dependent on the set of the previous level. At any rate, to continue with what you currently have. Currently, your Grid objects are created with the possibility to flash for 5 seconds when added into the world. Obviously, this is not what is wanted. You want to be able to control when a particular Grid object is to flash -- and that needs to be controlled by the world. So, let us first describe a Grid object whose color can be either red or gray:
import greenfoot.*;
import java.awt.Color;

public class Grid extends Actor
{
    public Grid()
    {
        setImage(new GreenfootImage(50, 50));
        setFlash(false);
    }
    
    public void setFlash(boolean flashColor)
    {
        Color color = flashColor ? new Color(200, 24, 32) : Color.gray;
        GreenfootImage image = getImage();
        image.setColor(color);
        image.fill();
        image.setColor(Color.black);
        image.drawRect(2, 2, 46, 46);
    }
}
The world can now control the color of a Grid object, 'grid', using either:
grid.setFlash(true); // to use flash color (red)
// or
grid.setFlash(false); // to use normal color (gray)
You will have 25 Grid objects in your world, of which a certain number of them will be chosen to flash. A List object should be sufficient to maintain references to those which are chosen. An int field can be introduced to track how many Grid object remain to be clicked on. There will be two states for you worlds -- the grid-flashing state and the user-clicking state. Obviously, the one must come before the other and something (a field value) is needed to be able to determine which is the current state. Since it will be necessary to "iterate" through the chosen Grid objects to have them flash, the value of the iterator would do nicely. For example, if its value is less than the level number, then work on flashing Grid objects; else detect clicks (something along those lines).
You need to login to post a reply.
1
2