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

2014/1/21

Need Help Switching between Players in a Turn-Based Game

ryanward214 ryanward214

2014/1/21

#
This is what I have so far, but I'm unsure of what to do next.
public void changeTurns()
    {
        List <Players> players = getObjects(Players.class); 
        for(int i = 0; i < players.size(); i++)
        {
            
        }
    }
Players have the ability to move a certain number of times and then their turn ends.
ryanward214 ryanward214

2014/1/21

#
By the way this code is in the world class, and here is my player class for additional reference:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.awt.Color; 
public class Player extends Players
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private boolean keyPressed = true;
    private int moves = 7;
    public void act()
    {
        if(moves >= 0)
        {
            move();
        }
        updateMoves();
    }
    public void move()  
    {
        int dx = 0; 
        int dy = 0;        
        if (getOneIntersectingObject(Wall.class) != null) 
        {
            setLocation(getX()-dx, getY()-dy); 
        }
        if (Greenfoot.isKeyDown("up") )
        {
            if(keyPressed == true)
            {
                setLocation(getX(), getY() - 1); 
                dy--;
                moves--;
            }
            keyPressed = false;
        }
        else if (Greenfoot.isKeyDown("down"))
        {
            if(keyPressed == true)
            {
                setLocation(getX(), getY() + 1); 
                dy++;
                moves--;
            }
            keyPressed = false;
        }
        else if (Greenfoot.isKeyDown("left"))
        {
            if(keyPressed == true)
            {
                setLocation(getX() - 1, getY()); 
                dx--; 
                moves--;
            }
            keyPressed = false; 
        }
        else if (Greenfoot.isKeyDown("right")) 
        {
            if(keyPressed == true)
            {
                setLocation(getX() + 1, getY()); 
                dx++;
                moves--;
            }
            keyPressed = false; 
        }
        else
        {
            keyPressed = true;
        } 
    }
    public void updateMoves()
    {
        GreenfootImage counter = new GreenfootImage("" + moves, 20, Color.white, Color.black);
        if(keyPressed == true)
        {
            getImage().drawImage(counter, getX(), getY());      
        }
        else
        {
            keyPressed = false;
        }
    }
    public void die()
    {
        World world = getWorld(); 
        Actor rock = getOneIntersectingObject(MovableWall.class);
        if(rock != null && (getY() == 0 || getY() == getWorld().getHeight()))
            world.removeObject(this); 
    }
  }
You need to login to post a reply.