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

2016/1/4

Make game stop when car hits wall.

reportersnotebook reportersnotebook

2016/1/4

#
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() { if (Greenfoot.isKeyDown("w")) { //Car should move forward and stop if it hits a wall move(3); } if (Greenfoot.isKeyDown("a")) { //Car should turn to the right and stop if it hits a wall turn(3); } if (Greenfoot.isKeyDown("s")) { //Car should turn to the left and stop if it hits a wall turn(-3); } if (Greenfoot.isKeyDown("z")) { //Car should move backward and stop if it hits a wall move(-3); } } public void canMove() { if (isTouching(wall1.class)) { //stop the game if the car hits the wall } } }
Super_Hippo Super_Hippo

2016/1/4

#
Add 'canMove();' at the end of the act-method to call the method and 'Greenfoot.stop();' in the method's if-block to stop the sceanario.
You need to login to post a reply.