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

2018/1/5

Moving two cars differently with different keys on the keyboard

dice dice

2018/1/5

#
Hello, I tried to write the code which separates the movements of two cars implementing a new constractor calling Key. But it give me a error. I want car2 to move using "w", "s", "a", "d", as car1 moves "up", "down" ,"left", "right". Somehow on MyWorld class
  Car car2 = new Car(new String("car3.png"), new String("car4.png"), new String(Key));
Give me an error, so as there are runtime errors for Car class Here is I've got;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Class that models objects of type "car"
 * 
 * 
 * @1.0
 */
public class Car extends Actor
{
    // Attributes of the class Car
    private static int numberWheels = 4;                // Number of wheels                           
    private boolean areHeadlightsOn = false;            // State of headlights (on => true; off => false)
    
    
    private String Key;
    private String image1;
    private String image2;
    
    private boolean isKeyDown = false;
    
    /**
     * Constructor
     */
    public Car(String file1, String file2, String Key){
        setImage1(file1); // Sets the attribute image1 to file1
        setImage2(file2); // Sets the attribute image2 to file2
        setKey(Key);
        setImage(file1);
        // Sets the image that appears in the screen at the beginning to the one in file1
    }
    
   
    
    /**
     * Method that is being always invoked when the program is running
     */
    public void act(){
        moveForward();
        moveBack();
        turnLeft();
        turnRight();
        turnHeadlightsOn();
        turnHeadlightsOff();
    }    

    /**
     * Move the car forward every time the key "up" is pressed
     */
    public void moveForward(){
        if (Greenfoot.isKeyDown(getKey())){
            move(5);
        }
    }
    
    /**
     * Move the car back every time the key "down" is pressed
     */
    public void moveBack(){
        if (Greenfoot.isKeyDown(getKey())){
            move(-1);
        }
    }    
    
    /**
     * Turn the car to the left every time the key "left" is pressed
     */    
    public void turnLeft(){
        if (Greenfoot.isKeyDown(getKey())){ 
            turn(-4);
        }
    }
    
    /**
     * Turn the car to the right every time the key "right" is pressed
     */      
    public void turnRight(){
        if (Greenfoot.isKeyDown(getKey())){ 
            turn(4);
        }
    }
    
    /**
     * Turn the headlights on when the key "space" is pressed.
     * The attribute isKeyDown is used to prevent setting the headlights on and off when the key "space" is pressed (and remains pressed for some time)
     */
    public void turnHeadlightsOn(){
        if (Greenfoot.isKeyDown("space") && !getIsKeyDown()){
            if (getAreHeadlightsOn() == false){
                setAreHeadlightsOn(true);
                setImage(getImage2());
                setIsKeyDown(true);
            }
            

        }
        if (!Greenfoot.isKeyDown("space") && getIsKeyDown()){
             setIsKeyDown(false);
        }           
    }   
    
   
    /**
     * Turn the headlights off when the key "space" is pressed.
     * The attribute isKeyDown is used to prevent setting the headlights off and on when the key "space" is pressed (and remains pressed for some time)
     */  
    public void turnHeadlightsOff(){
        if (Greenfoot.isKeyDown("space") && !getIsKeyDown()){
            if (getAreHeadlightsOn() == true){
                setAreHeadlightsOn(false);
                setImage(getImage1());
                setIsKeyDown(true);
            }

        }
        if (!Greenfoot.isKeyDown("space") && getIsKeyDown()){
            setIsKeyDown(false);
        }           
    }      
    
   
    public void setAreHeadlightsOn(boolean areHeadlightsOn){
        this.areHeadlightsOn = areHeadlightsOn;
    }
    
    public boolean getAreHeadlightsOn(){
        return this.areHeadlightsOn;
    }     
    
    public void setImage1(String image1){
        this.image1 = image1;
    }
    
    public String getImage1(){
        return this.image1;
    }    
    
    public void setImage2(String image2){
        this.image2 = image2;
    }
    
    public String getImage2(){
        return this.image2;
    }       
    
    public void setIsKeyDown(boolean isKeyDown){
        this.isKeyDown = isKeyDown;
    }
    
    public boolean getIsKeyDown(){
        return this.isKeyDown;
    }          
    
    public void setKey(String Key){
        if (this.Key == "w") {this.Key = "up";}
        if (this.Key == "s") {this.Key = "down";}
        if (this.Key =="a") {this.Key = "left";}
        if (this.Key =="d") {this.Key = "right";}
    }
    public String getKey(){
        return this.Key;
    }
}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Class that creates a new world: MyWorld
 * 
 * @Java MOOC team, UC3M
 * @1.0
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(800, 600, 1); 
        setBackground("road.jpg");
        populateWorld();
        
        String Key;
       
        
        
        
        
        
    }
    
    public void populateWorld(){
        Car car1 = new Car(new String("car1.png"), new String("car2.png"), new String("up"));
        Car car2 = new Car(new String("car3.png"), new String("car4.png"), new String(Key));
        
        
        addObject(car1, 120, 70);
        addObject(car2, 580, 30);  
        
        
    }
}
danpost danpost

2018/1/6

#
You are only passing one key string to the constructor for each car -- and the value for the second car is 'null'. Also. with only one key passed and one field being set, you cannot expect four different values to be set (though, I guess the one will tell you what the other three will be).
danpost danpost

2018/1/6

#
Your Car class has an excessive number of simple methods which actually makes your code harder to follow and over-complicated. It can be greatly reduced in size. Also, the coding for controlling the headlights can be simplified. Just by removing some of those methods and the one simplification, the class which is 163 lines long above, is reduced to only 70 and certainly can be reduced more:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Class that models objects of type "car"
 * 
 * 
 * @1.0
 */
public class Car extends Actor
{
    // Attributes of the class Car
    private static int numberWheels = 4;                // Number of wheels                           
    private boolean areHeadlightsOn = false;            // State of headlights (on => true; off => false)
     
    private String Key;
    private String image1;
    private String image2;
     
    private boolean isKeyDown = false;
     
    /**
     * Constructor
     */
    public Car(String file1, String file2, String Key){
        image1 = file1; // Sets the attribute image1 to file1
        image2 = file2; // Sets the attribute image2 to file2
        setKey(Key);
        setImage(image1); // Sets the initial image to image1
    }
     
    /**
     * Method that is being always invoked when the program is running
     */
    public void act(){
        // Move the car forward every time the key "up" is pressed
        if (Greenfoot.isKeyDown(Key)){ // *************  getKey returned Key; what value has Key?
            move(5);
        }
        // Move the car back every time the key "down" is pressed
        if (Greenfoot.isKeyDown(Key)){ // *************  getKey returned Key; what value has Key?
            move(-1);
        }
        // Turn the car to the left every time the key "left" is pressed
        if (Greenfoot.isKeyDown(Key)){  // *************  getKey returned Key; what value has Key?
            turn(-4);
        }
        // Turn the car to the right every time the key "right" is pressed
        if (Greenfoot.isKeyDown(Key)){ // *************  getKey returned Key; what value has Key?
            turn(4);
        }
        // control headlights
        if (isKeyDown != Greenfoot.isKeyDown("space"))
        {
            isKeyDown = ! isKeyDown;
            if (isKeyDown)
            {
                areHeadlightsOn = ! areHeadlightsOn;
                setImage(areHeadlightsOn ? image2 : image1);
            }
        }
    }
    
    // ************   ??  what does this really do  ??   ********************
    public void setKey(String Key){
        if (this.Key == "w") {this.Key = "up";}
        if (this.Key == "s") {this.Key = "down";}
        if (this.Key =="a") {this.Key = "left";}
        if (this.Key =="d") {this.Key = "right";}
    }
}
dice dice

2018/1/6

#
I was able to make it work by increasing variables of constructor
Car(String file1, String file2, String Key_1, String Key_2, String Key_3, String Key_4);
and
Car car1 = new Car(new String("car1.png"), new String("car2.png"), new String("up"), new String("down"), new String("left"), new String("right"));

Car car2 = new Car(....., new String("w"), new String("s"), new String("a"), new String("d"));

 
and made
private Key1, Key2, Key3, Key4;

........

Key1 = Key_1;
Key2 = Key_2;
.......
Car(String file1, String file2, String Key_1, String Key_2, String Key_3, String Key_4);
..............
if (Greenfoot.isKeyDown(Key1)){
     move(5);
}
danpost danpost

2018/1/6

#
You can simplify by passing an array of strings:
Car car = new Car("car1.png", "car2.png", new String[] { "up", "down", "left", "right" });
..........
private String[] keys;
..........
public Car(String file1, String file2, String[] myKeys)
{
    keys = myKeys;
..........
if (Greenfoot.isKeyDown(keys[0])) {
     move(5);
}
dice dice

2018/1/6

#
Thanks for the advice!
You need to login to post a reply.