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

2024/11/4

Fixing text displaying + Players duplicating when moved

trulydevious trulydevious

2024/11/4

#
I am currently making a board game type thing in Greenfoot. So, I've done most of the coding, but I need help with a couple of things. I'm trying to figure out how to set the text to be on top of the actors, that way it's not being blocked. Also the players are duplicating when I make them to move and that's not what I want it to do, I just can't find out what I'm doing wrong. Board class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Board extends World
{
    private String[] boardSpaces = new String[119];
    private String[] texts = {"Move -1", "Move 1", "Move 2", "Move 3"};
    private Timer textTimer = new Timer();
    private boolean textDisplayed = false;
    private GreenfootImage textImage;
    int randomIndex = Greenfoot.getRandomNumber(texts.length);
    String randomText = texts[randomIndex];
            
    /**
     * Constructor for objects of class Board.
     * 
     */
    public Board()
    {    
        // Create a new world with 20x20 cells with a cell size of 40x40 pixels.
        super(20, 20, 45);
        setPaintOrder(Player1.class, Player2.class, Space.class);
        initializeBoardSpaces();
        prepare();
        textTimer = new Timer();
    }
    
    public void act(){
        if(Greenfoot.mouseClicked(this)){
            displayRandomText();
        }
        
        if(textDisplayed && textTimer.millisElapsed() >= 10000){
            removeText();
        }
    }
    
    private void initializeBoardSpaces(){
        for(int i = 0; i < boardSpaces.length; i++){
            String[] colors = {"Red", "Purple", "Yellow", "Blue", "Orange", "Green"};
            boardSpaces[i] = colors[i % colors.length];
        }
    }

    private void prepare(){
        addPlayer();
        createBoard();
    }
    
    private void createBoard(){
        //Row 1 (17)
        Space space2 = new Space("Blue");
        addObject(space2, 2, 1);
        Space space3 = new Space("Yellow");
        addObject(space3, 3, 1);
        Space space4 = new Space("Purple");
        addObject(space4, 4, 1);
        Space space5 = new Space("Red");
        addObject(space5, 5, 1);
        Space space6 = new Space("Green");
        addObject(space6, 6, 1);
        Space space7 = new Space("Orange");
        addObject(space7, 7, 1);
        Space space8 = new Space("Blue");
        addObject(space8, 8, 1);
        Space space9 = new Space("Yellow");
        addObject(space9, 9, 1);
        Space space10 = new Space("Purple");
        addObject(space10, 10, 1);
        Space space11 = new Space("Red");
        addObject(space11, 11, 1);
        Space space12 = new Space("Green");
        addObject(space12, 12, 1);
        Space space13 = new Space("Orange");
        addObject(space13, 13, 1);
        Space space14 = new Space("Blue");
        addObject(space14, 14, 1);
        Space space15 = new Space("Yellow");
        addObject(space15, 15, 1);
        Space space16 = new Space("Purple");
        addObject(space16, 16, 1);
        Space space17 = new Space("Red");
        addObject(space17, 17, 1);
        Space space18 = new Space("Green");
        addObject(space18, 18, 1);

        //Down 1 (2)
        Space space19 = new Space("Orange");
        addObject(space19, 18, 2);
        Space space20 = new Space("Blue");
        addObject(space20, 18, 3);

        //Row 2 (18)
        Space space21 = new Space("Yellow");
        addObject(space21, 17, 3);
        Space space22 = new Space("Purple");
        addObject(space22, 16, 3);
        Space space23 = new Space("Pink");
        addObject(space23, 15, 3);
        Space space24 = new Space("Red");
        addObject(space24, 14, 3);
        Space space25 = new Space("Green");
        addObject(space25, 13, 3);
        Space space26 = new Space("Orange");
        addObject(space26, 12, 3);
        Space space27 = new Space("Blue");
        addObject(space27, 11, 3);
        Space space28 = new Space("Yellow");
        addObject(space28, 10, 3);
        Space space29 = new Space("Purple");
        addObject(space29, 9, 3);
        Space space30 = new Space("Red");
        addObject(space30, 8, 3);
        Space space31 = new Space("Green");
        addObject(space31, 7, 3);
        Space space32 = new Space("Orange");
        addObject(space32, 6, 3);
        Space space33 = new Space("Blue");
        addObject(space33, 5, 3);
        Space space34 = new Space("Yellow");
        addObject(space34, 4, 3);
        Space space35 = new Space("Purple");
        addObject(space35, 3, 3);
        Space space36 = new Space("Red");
        addObject(space36, 2, 3);
        Space space37 = new Space("Green");
        addObject(space37, 1, 3);
        
        //Down 2 (3)
        Space space38 = new Space("Orange");
        addObject(space38, 1, 4);
        Space space39 = new Space("Blue");
        addObject(space39, 1, 5);
        Space space40 = new Space("Yellow");
        addObject(space40, 1, 6);
        
        //Row 3 (10)
        Space space41 = new Space("Purple");
        addObject(space41, 2, 6);
        Space space42 = new Space("Red");
        addObject(space42, 3, 6);
        Space space43 = new Space("Green");
        addObject(space43, 4, 6);
        Space space44 = new Space("Pink");
        addObject(space44, 5, 6);
        Space space45 = new Space("Orange");
        addObject(space45, 6, 6);
        Space space46 = new Space("Blue");
        addObject(space46, 7, 6);
        Space space47 = new Space("Yellow");
        addObject(space47, 8, 6);
        Space space48 = new Space("Purple");
        addObject(space48, 9, 6);
        Space space49 = new Space("Red");
        addObject(space49, 10, 6);
        Space space50 = new Space("Green");
        addObject(space50, 11, 6);
        
        //Down 3 (2)
        Space space51 = new Space("Orange");
        addObject(space51, 11, 7);
        Space space52 = new Space("Blue");
        addObject(space52, 11, 8);
        
        //Row 4 (7)
        Space space53 = new Space("Yellow");
        addObject(space53, 12, 8);
        Space space54 = new Space("Purple");
        addObject(space54, 13, 8);
        Space space55 = new Space("Red");
        addObject(space55, 14, 8);
        Space space56 = new Space("Green");
        addObject(space56, 15, 8);
        Space space57 = new Space("Orange");
        addObject(space57, 16, 8);
        Space space58 = new Space("Blue");
        addObject(space58, 17, 8);
        Space space59 = new Space("Yellow");
        addObject(space59, 18, 8);
        
        //Down 4 (2)
        Space space60 = new Space("Purple");
        addObject(space60, 18, 9);
        Space space61 = new Space("Red");
        addObject(space61, 18, 10);
        
        //Row 5 (17)
        Space space62 = new Space("Pink");
        addObject(space62, 17, 10);
        Space space63 = new Space("Green");
        addObject(space63, 16, 10);
        Space space64 = new Space("Orange");
        addObject(space64, 15, 10);
        Space space65 = new Space("Blue");
        addObject(space65, 14, 10);
        Space space66 = new Space("Yellow");
        addObject(space66, 13, 10);
        Space space67 = new Space("Purple");
        addObject(space67, 12, 10);
        Space space68 = new Space("Red");
        addObject(space68, 11, 10);
        Space space69 = new Space("Green");
        addObject(space69, 10, 10);
        Space space70 = new Space("Orange");
        addObject(space70, 9, 10);
        Space space71 = new Space("Blue");
        addObject(space71, 8, 10);
        Space space72 = new Space("Yellow");
        addObject(space72, 7, 10);
        Space space73 = new Space("Purple");
        addObject(space73, 6, 10);
        Space space74 = new Space("Red");
        addObject(space74, 5, 10);
        Space space75 = new Space("Green");
        addObject(space75, 4, 10);
        Space space76 = new Space("Orange");
        addObject(space76, 3, 10);
        Space space77 = new Space("Blue");
        addObject(space77, 2, 10);
        Space space78 = new Space("Yellow");
        addObject(space78, 1, 10);
        
        //Down 5 (3)
        Space space79 = new Space("Pink");
        addObject(space79, 1, 11);
        Space space80 = new Space("Purple");
        addObject(space80, 1, 12);
        Space space81 = new Space("Red");
        addObject(space81, 1, 13);
        
        //Row 6 (6)
        Space space82 = new Space("Green");
        addObject(space82, 2, 13);
        Space space83 = new Space("Orange");
        addObject(space83, 3, 13);
        Space space84 = new Space("Blue");
        addObject(space84, 4, 13);
        Space space85 = new Space("Yellow");
        addObject(space85, 5, 13);
        Space space86 = new Space("Purple");
        addObject(space86, 6, 13);
        Space space87 = new Space("Red");
        addObject(space87, 7, 13);
        
        //Down 6 (2)
        Space space88 = new Space("Green");
        addObject(space88, 7, 14);
        Space space89 = new Space("Pink");
        addObject(space89, 7, 15);
        
        //Row 7 (11)
        Space space90 = new Space("Orange");
        addObject(space90, 8, 15);
        Space space91 = new Space("Blue");
        addObject(space91, 9, 15);
        Space space92 = new Space("Yellow");
        addObject(space92, 10, 15);
        Space space93 = new Space("Purple");
        addObject(space93, 11, 15);
        Space space94 = new Space("Red");
        addObject(space94, 12, 15);
        Space space95 = new Space("Green");
        addObject(space95, 13, 15);
        Space space96 = new Space("Orange");
        addObject(space96, 14, 15);
        Space space97 = new Space("Blue");
        addObject(space97, 15, 15);
        Space space98 = new Space("Yellow");
        addObject(space98, 16, 15);
        Space space99 = new Space("Purple");
        addObject(space99, 17, 15);
        Space space100 = new Space("Red");
        addObject(space100, 18, 15);
        
        //Down 7 (2)
        Space space101 = new Space("Green");
        addObject(space101, 18, 16);
        Space space102 = new Space("Orange");
        addObject(space102, 18, 17);
        
        //Row 8 (14)
        Space space103 = new Space("Blue");
        addObject(space103, 17, 17);
        Space space104 = new Space("Yellow");
        addObject(space104, 16, 17);
        Space space105 = new Space("Purple");
        addObject(space105, 15, 17);
        Space space106 = new Space("Red");
        addObject(space106, 14, 17);
        Space space107 = new Space("Pink");
        addObject(space107, 13, 17);
        Space space108 = new Space("Green");
        addObject(space108, 12, 17);
        Space space109 = new Space("Orange");
        addObject(space109, 11, 17);
        Space space110 = new Space("Blue");
        addObject(space110, 10, 17);
        Space space111 = new Space("Yellow");
        addObject(space111, 9, 17);
        Space space112 = new Space("Purple");
        addObject(space112, 8, 17);
        Space space113 = new Space("Red");
        addObject(space113, 7, 17);
        Space space114 = new Space("Green");
        addObject(space114, 6, 17);
        Space space115 = new Space("Orange");
        addObject(space115, 5, 17);
        Space space116 = new Space("Blue");
        addObject(space116, 4, 17);
        
        //Down 8 (1)
        Space space117 = new Space("Yellow");
        addObject(space117, 4, 18);
        
        //Row 9 (2)
        Space space118 = new Space("Purple");
        addObject(space118, 3, 18);
        Space space119= new Space("Red");
        addObject(space119, 2, 18);
    }

    public void addPlayer(){
        Player1 player1 = new Player1();
        addObject(player1, 1, 18);
        Player2 player2 = new Player2();
        addObject(player2, 0, 18);
    }

    public int getBoardLength(){
        return boardSpaces.length;
    }

    public String getBoardSpace(int index){
        return boardSpaces[index];
    }
    
    private void displayRandomText(){
        textImage = new GreenfootImage(randomText, 16, Color.BLACK, new Color(0, 0, 0, 0));
        
        int x = getWidth() * getCellSize() / 2 - textImage.getWidth() / 2;
        int y = getHeight() * getCellSize() / 2 - textImage.getHeight() / 2;
        
        getBackground().clear();
        getBackground().drawImage(textImage, x, y);
        
        textDisplayed = true;
        textTimer.mark();
    }
    
    private void removeText(){
        getBackground().clear();
        textDisplayed = false;
    }
}
Player classes (They are the same code just different actors, there are two player classes)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player1 extends Actor
{
    private boolean selected = false;

    /**
     * Act - do whatever the Player1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkSelection();
    }    
    
    private void checkSelection(){
        if(Greenfoot.mouseClicked(this)){
            selected = true;
        } else if(selected && Greenfoot.mouseClicked(null)){
            MouseInfo mouse = Greenfoot.getMouseInfo();
            if(mouse != null){
                int targetX = mouse.getX();
                int targetY = mouse.getY();
                setLocation(targetX, targetY);
            }
            selected = false;
        }
    }
}
danpost danpost

2024/11/4

#
trulydevious wrote...
I'm trying to figure out how to set the text to be on top of the actors, that way it's not being blocked.
If you draw the text onto the background image (as you are doing), the actors will always be above the text. However, you can accomplish this if you: 1) use an Actor type object to display the text; and 2) use the setPaintOrder method in your Board class constructor to give it priority in showing above other actors;
the players are duplicating when I make them to move and that's not what I want it to do, I just can't find out what I'm doing wrong. << Code Omitted >>
You are using the following line in your code::
getBackground().clear();
Instead, use:
GreenfootImage bg = getBackground();
bg.setColor(Color.WHITE);
bg.fill();
This way, the residue of previously drawn backgrounds will not remain in the image. The background image needs to be completely opaque (without any transparencies which the clear method introduces).
trulydevious trulydevious

2024/11/5

#
Thank you so much!
You need to login to post a reply.