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

2019/5/12

How can i make the scenario restart when the car hits the wall?

Exy.evans Exy.evans

2019/5/12

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class car here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class car extends Actor
{
    /**
     * Act - do whatever the car wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(4);
        if (Greenfoot.isKeyDown("w"))
        {
            //Car should move forward and stop if it hits a wall
        }
                if (Greenfoot.isKeyDown("a"))
        {
             //Car should turn to the right and stop if it hits a wall
        }
                if (Greenfoot.isKeyDown("s"))
        {
             //Car should turn to the left and stop if it hits a wall
        }
                if (Greenfoot.isKeyDown("z"))
        {
             //Car should move backward and stop if it hits a wall
        }
    } 
    public void move(int moveAmt)
{
    // determine direction by keypress checking
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("right")) dx += 1;
    if (Greenfoot.isKeyDown("left")) dx -= 1;
    if (Greenfoot.isKeyDown("down")) dy += 1;
    if (Greenfoot.isKeyDown("up")) dy -= 1;
    // check for wall on each step of move in both vertical and horizontal directions
    for (int i = 0; i < moveAmt; i++)
   
    {
            setLocation(getX() + dx, getY());
        if (getOneIntersectingObject(wall1.class) != null) setLocation(getX() - dx, getY());
        setLocation(getX(), getY() + dy);
        if (getOneIntersectingObject(wall1.class) != null) setLocation(getX(), getY() - dy);

            
    }
}
public void hitwall1 ()
    {
                 
       if (isTouching(wall1.class))
        {
            Greenfoot.stop();
             //stop the game if the car hits the wall
        }           
                   
        }

}
Zamoht Zamoht

2019/5/12

#
You can probably use setWorld and then create a new instance of the current world you use.
You need to login to post a reply.