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/7/28

#
I have a 5x5 grid of squares. They are all the same color right now. I want to change the color of the square. First, i want to change one square. Then two squares, then 3 squares at one at a time randomly. Any help?
davmac davmac

2017/7/28

#
I suggest you start by reading this guide, which explains how to ask questions with more likelihood of getting a helpful response. It should give you an idea of how better to explain what you need.
IDaivdJr IDaivdJr

2017/7/28

#
public class Grid extends Actor
{
    GreenfootImage img = new GreenfootImage (75, 75);
    
    public void act()
    {
        //need help here 
        for(int  i = 0; i < 1; i++){
            for (int r = 0; r < 5; r++){
                for (int t =  0; t < 5; t++){
                    //img.setColor(new Color(200, 24, 32));
                    img.drawRect(0, 0, 50, 50);
                    //img.fillRect(0, 0, 50, 50);
                    //setImage(img);
                }
            }
        }
        if (Greenfoot.mousePressed(this))
        {
            img.setColor(new Color(200, 24, 32)); // makes a square red 
            img.fillRect(0, 0, 50, 50);
            img.setColor(java.awt.Color.black);
            img.drawRect(0, 0, 50, 50);
            setImage(img);
        }
    }
    
    public Grid() 
    {
        img.setColor(java.awt.Color.gray); // makes the squares gray
        img.fillRect(0, 0, 50, 50);
        img.setColor(java.awt.Color.black); // makes the outline of squares black 
        img.drawRect(0, 0, 50, 50);
        setImage(img);
    }   
}
Im trying to create a game with levels. In the first level i just want one square to change color, second level two squares and so on.
Super_Hippo Super_Hippo

2017/7/29

#
Try this class:
import greenfoot.*;
public class MyWorld extends World
{
    private int level;
    
    public MyWorld() {this(1);}
    
    public MyWorld(int lvl)
    {
        super(500, 500, 1);
        level = lvl;
        int[][] grid = new int[5][5];
        for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
        {
            grid[i][j] = 0;
        }
        
        for (int num=0; num<level; num++)
        {
            int fill = Greenfoot.getRandomNumber(grid.length*grid[0].length);
            if (grid[fill/grid.length][fill%grid[0].length] == 0)
            {
                grid[fill/grid.length][fill%grid[0].length] = 1;
            }
            else num--;
        }
        
        GreenfootImage img = new GreenfootImage(getWidth(), getHeight());
        for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
        {
            if (grid[i][j]==1) img.setColor(Color.RED);
            else img.setColor(Color.BLACK);
            img.fillRect(i*getWidth()/grid.length, j*getHeight()/grid[i].length, getWidth()/grid.length, getHeight()/grid[i].length);
        }
        setBackground(img);
    }
}
IDaivdJr IDaivdJr

2017/7/30

#
Okay, so i like the idea of this, but it doesn't set up a grid of squares 5x5. Like my code does. Below is my MyWorld source code. i hope you can make some changes to it.
private static final int HEIGHT = 50;
    private static final int WIDTH = 50;
    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        for(int  i = 0; i < 1; i++){
            for (int r = 0; r < 5; r++){
                for (int t =  0; t < 5; t++){
                    Grid g = new Grid();
                    addObject(g,75 + t*WIDTH,50+ i*HEIGHT*1+r*HEIGHT);
                }
            }
        }    
Super_Hippo Super_Hippo

2017/7/31

#
The code I gave you basically creates a background image for the world in which one (or more for higher level input) squares are red and the rest red. Your code creates 125 grids while a lot of them are at the same position (when sum of i and r is the same) or "outside the world" (=at the right edge when the sum of i and r is 7 or 8).
IDaivdJr IDaivdJr

2017/7/31

#
When i used your code, there was a black background with a red square. What i want is a grid of squares 5x.5. what i want right now is for just one square of the grid to be a different color. i want it to blink for like 5 seconds and then let the user click the square that was different.
Super_Hippo Super_Hippo

2017/7/31

#
Okay, so if you want to place 25 actors representing these squares, you will only need two for loops, not three. You can use the code I gave you and instead of creating and drawing the image in the end, you will create the grids and pass 0 or 1 to them to let them know if they should blink or not (for example).
IDaivdJr IDaivdJr

2017/7/31

#
I'm a little confused, can you put the code. And then how do i modify it in the Grid class?
Super_Hippo Super_Hippo

2017/7/31

#
import greenfoot.*;
public class MyWorld extends World
{
    private int level;
    
    public MyWorld() {this(1);}
    
    public MyWorld(int lvl)
    {
        super(500, 500, 1);
        level = lvl;
        int[][] grid = new int[5][5];
        for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
        {
            grid[i][j] = 0;
        }
        
        for (int num=0; num<level; num++)
        {
            int fill = Greenfoot.getRandomNumber(grid.length*grid[0].length);
            if (grid[fill/grid.length][fill%grid[0].length] == 0)
            {
                grid[fill/5][fill%5] = 1;
            }
            else num--;
        }
        
        /*GreenfootImage img = new GreenfootImage(getWidth(), getHeight());
        for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
        {
            if (grid[i][j]==1) img.setColor(Color.RED);
            else img.setColor(Color.BLACK);
            img.fillRect(i*getWidth()/grid.length, j*getHeight()/grid[i].length, getWidth()/grid.length, getHeight()/grid[i].length);
        }
        setBackground(img);*/
        
        for (int i=0; i<grid.length; i++) for (int j=0; j<grid[i].length; j++)
        {
            addObject(new Grid(grid[i][j], getWidth()/grid.length, getHeight()/grid[i].length), i*getWidth()/grid.length+getWidth()/grid.length/2, j*getHeight()/grid[i].length+getHeight()/grid[i].length/2);
        }
    }
}
import greenfoot.*;
public class Grid extends Actor
{
    private int flashing;
    
    public Grid(int i, int wid, int hei)
    {
        flashing = i;
        GreenfootImage img = new GreenfootImage(wid, hei);
        img.setColor(Color.BLACK);
        img.fill();
        setImage(img);
    }
    
    public void act()
    {
        if (flashing>0 && flashing<5*50)
        {
            if (++flashing%25==0)
            {
                getImage().setColor(flashing%50==25?Color.RED:Color.BLACK);
                getImage().fill();
            }
        }
    }
}
IDaivdJr IDaivdJr

2017/7/31

#
Okay nice, but it is not setting up a 5x5 grid of squares.
Super_Hippo Super_Hippo

2017/7/31

#
It does. Well, looks I don't know what you mean with 5x5 grid of squares then =/
IDaivdJr IDaivdJr

2017/7/31

#
in greenfoot, there is only a black background and a red flashing square. i want the grid to be visible.
Super_Hippo Super_Hippo

2017/8/1

#
Like this?
import greenfoot.*;
public class Grid extends Actor
{
    private int flashing;
    
    public Grid(int i, int wid, int hei)
    {
        flashing = i;
        GreenfootImage img = new GreenfootImage(wid, hei);
        img.setColor(Color.GRAY);
        img.fill();
        img.setColor(Color.BLACK);
        img.fillRect(1,1,img.getWidth()-2, img.getHeight()-2);
        setImage(img);
    }
    
    public void act()
    {
        if (flashing>0 && flashing<5*50)
        {
            if (++flashing%25==0)
            {
                getImage().setColor(flashing%50==25?Color.RED:Color.BLACK);
                getImage().fillRect(1,1,getImage().getWidth()-2, getImage().getHeight()-2);
            }
        }
    }
}
Super_Hippo Super_Hippo

2017/8/1

#
Line 23 of the MyWorld class has to be this:
grid[fill/grid.length][fill%grid[0].length] = 1;
as it was given before. Only changed it here and not in my code, so last time I copied the wrong one.
There are more replies on the next page.
1
2