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

2013/10/21

getColor (Project for School)

MoizRashid MoizRashid

2013/10/21

#
Hello, I have this computer science project in high school. I am a beginner, and am in grade 11. Here is my Greenfoot programs:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class You here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class You extends Actor
{
    /**
     * Act - do whatever the You wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
        
    }  
    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        if (Greenfoot.isKeyDown("up") && canMove())
        {
            move(1);
        }
        
        if(Greenfoot.isKeyDown("left") ) {
            setRotation(getRotation() - 5);
        }        
        if(Greenfoot.isKeyDown("right")) {
            setRotation(getRotation() + 5);
        }       
    }
    private boolean canMove()
    {
        if (getColor() !== Black))
        {
           return true; 
            
        }
        
        else
        {
            return false;
        }
        
    }
}
I want to make my character stop when it collides with the wall. The wall is a background image, in black, and the path is white. Can I do this? If I cant, how do I make an object, like a wall. I'm making a maze, and want to control the character through it. This game is in 2d.
Gevater_Tod4711 Gevater_Tod4711

2013/10/21

#
You just have to change line 41 to this:
if (getWorld().getBackground().getColorAt(getX(), getY()).equals(Color.black)) {
Then it should work.
MoizRashid MoizRashid

2013/10/21

#
Gevater_Tod4711 wrote...
You just have to change line 41 to this:
if (getWorld().getBackground().getColorAt(getX(), getY()).equals(Color.black)) {
Then it should work.
Thanks! It does stop now, but it gets stuck in the pixel, what do i so to stop that? Heres my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class You here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class You extends Actor
{
    /**
     * Act - do whatever the You wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
        
    }  
    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        if (Greenfoot.isKeyDown("up") && canMove())
        {
            move(1);
        }
        
        if(Greenfoot.isKeyDown("left") ) {
            setRotation(getRotation() - 5);
        }        
        if(Greenfoot.isKeyDown("right")) {
            setRotation(getRotation() + 5);
        }       
    }
    private boolean canMove()
    {
        if (getWorld().getBackground().getColorAt(getX()+5, getY()+5).equals(Color.black)) 
        {
           return false; 
            
        }
        
        else
        {
            return true;
        }
        
    }
}
danpost danpost

2013/10/21

#
It would be easier if you had actor objects for the walls; however, it is possible to determine the color of a pixel at any point in a GreenfootImage object (like the background of the world). Line 41 will almost assuredly always return false as ' Black ' is not defined anywhere (you probably meant ' Color.black ' or ' Color.BLACK ' ). The reason it would return false is that the two instances of color (the one you compare it to and the one from the image) must be the same instance to return true equality. Using the ' equals ' method is what you should use (as ' objectA.equals(objectB) ' or ' if (Color.black).equals(getBackground().getColorAt(getX(), getY())))' ). Using actors for your walls allows you to use the collision detection within the Actor class and would probably save a lot of coding.
MoizRashid MoizRashid

2013/10/21

#
danpost wrote...
It would be easier if you had actor objects for the walls; however, it is possible to determine the color of a pixel at any point in a GreenfootImage object (like the background of the world). Line 41 will almost assuredly always return false as ' Black ' is not defined anywhere (you probably meant ' Color.black ' or ' Color.BLACK ' ). The reason it would return false is that the two instances of color (the one you compare it to and the one from the image) must be the same instance to return true equality. Using the ' equals ' method is what you should use (as ' objectA.equals(objectB) ' or ' if (Color.black).equals(getBackground().getColorAt(getX(), getY())))' ). Using actors for your walls allows you to use the collision detection within the Actor class and would probably save a lot of coding.
How would I use actors as the walls? I tried making walls with drawLine, or drawRect, and it didnt work for me, what would I do?
Gevater_Tod4711 Gevater_Tod4711

2013/10/21

#
You could try to use this instead of line 41 in your code:
if (getWorld().getBackground().getColorAt(getX() + 5 * (Math.cos(Math.toRadians(getRotation()))), getY() + 5 * (Math.sin(Math.toRadians(getRotation())))).equals(Color.black)) { 
Now your actor should stop about five pixels before the wall.
danpost danpost

2013/10/21

#
Yeah, I missed the 'getWorld().' in front of 'getBackground()'. And I did not add the offsets; just wanted to explain generally.
MoizRashid MoizRashid

2013/10/21

#
Gevater_Tod4711 wrote...
You could try to use this instead of line 41 in your code:
if (getWorld().getBackground().getColorAt(getX() + 5 * (Math.cos(Math.toRadians(getRotation()))), getY() + 5 * (Math.sin(Math.toRadians(getRotation())))).equals(Color.black)) { 
Now your actor should stop about five pixels before the wall.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class You here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class You extends Actor
{
    /**
     * Act - do whatever the You wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
        
    }  
    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        if (Greenfoot.isKeyDown("up") && canMove())
        {
            move(1);
        }
        
        if(Greenfoot.isKeyDown("left") ) {
            setRotation(getRotation() - 5);
        }        
        if(Greenfoot.isKeyDown("right")) {
            setRotation(getRotation() + 5);
        }       
    }
    private boolean canMove()
    {
        if (getWorld().getBackground().getColorAt(getX() + 5 * (Math.cos(Math.toRadians(getRotation()))), getY() + 5 * (Math.sin(Math.toRadians(getRotation())))).equals(Color.black))
        {
           return false; 
            
        }
        
        else
        {
            return true;
        }
        
    }
}
It's still not working,
danpost danpost

2013/10/21

#
MoizRashid wrote...
How would I use actors as the walls? I tried making walls with drawLine, or drawRect, and it didnt work for me, what would I do?
You would create a subclass of Actor called 'Wall' and give it an image. Then you would modify the constructor to accept length and rotation values as arguments in the call. Use these in the constructor to modify/create the correct wall image and orientation. 'Wall' objects will have to behavior (just state). If you have problems with the image, post back supplying what code you have tried.
MoizRashid MoizRashid

2013/10/22

#
danpost wrote...
MoizRashid wrote...
How would I use actors as the walls? I tried making walls with drawLine, or drawRect, and it didnt work for me, what would I do?
You would create a subclass of Actor called 'Wall' and give it an image. Then you would modify the constructor to accept length and rotation values as arguments in the call. Use these in the constructor to modify/create the correct wall image and orientation. 'Wall' objects will have to behavior (just state). If you have problems with the image, post back supplying what code you have tried.
I have no clue what your talking about. I am a noob. How will i create objects for the walls? Please help
danpost danpost

2013/10/22

#
Create an Actor subclass, just like you created the 'You' class. Give it the name of 'Wall'. When you have done that post the code for that class here and we can work on its image. BTW, how thick are your walls (in pixels)?
MoizRashid MoizRashid

2013/10/23

#
danpost wrote...
Create an Actor subclass, just like you created the 'You' class. Give it the name of 'Wall'. When you have done that post the code for that class here and we can work on its image. BTW, how thick are your walls (in pixels)?
I fixed it, thanks for the help!! I am trying to create a power up for the player. I have two players, and want only one of them to be able to pick the power up, up. I have an Actor called power up and it is just a circle. When the power up is picked up the the player, i want the player to be able to click space bar whenever they want, and it will freeze the other player for 5 seconds and allow the player to go through the frozen player. The main player's goal is to get to the end of the maze, and the other players job is to eat the first player. How would I do this?
danpost danpost

2013/10/23

#
If you have a way to distinguish between the players in the player class you can qualify the picking up of the powerup to the specific player that is allowed to. Having an instance boolean field in the class to signify if the powerup is picked up or not can be used to control the rest.
You need to login to post a reply.