////////////////////////////////////////////////////////////////////////////// /** * * Code at the end of lecture 9/30/09 * Car stops after hitting more than 5 barrels. * */ ////////////////////////////////////////////////////////////////////////////// import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Car here. * * @author (your name) * @version (a version number or a date) */ public class Car extends Vehicle { /** * Instance variable declaration. * * private type name; */ private int numHits; public Car() { numHits = 0; } /** * 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() { checkWorldEdge(); checkCollision(); if(Greenfoot.isKeyDown("j")) { turn(-15); } if(Greenfoot.isKeyDown("k")) { turn(15); } move(); } /** * checkWorldEdge - checks to see if the car is at the edge of the world. * If it is, turns the car away from the edge. */ public void checkWorldEdge() { if(atWorldEdge()) { turn(-75); } } /** * checkColision - checks to see if the car can see and is therefore about * to collide with another car or an obstacle in the road. If so, stops * the simulation. */ public void checkCollision() { if(canSee(Car.class)) { Greenfoot.stop(); } if(canSee(Obstacle.class)) { //if(we are showing the car image) // setImage(new GreenfootImage("flower2.png")); //otherwise // setImage(new GreenfootImage("car.png")); numHits = numHits + 1; if(numHits > 5) { Greenfoot.stop(); } } } } ////////////////////////////////////////////////////////////////////////////// /** * * Code at the end of lecture 10/2/09 * Car alternates images whenever it hits a barrel. * */ ////////////////////////////////////////////////////////////////////////////// import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Car here. * * @author (your name) * @version (a version number or a date) */ public class Car extends Vehicle { /** * Instance variable declaration. * * private type name; */ private int numHits; private GreenfootImage carImage; private GreenfootImage flowerImage; public Car() { numHits = 0; carImage = new GreenfootImage("car.png"); flowerImage = new GreenfootImage("flower2.png"); setImage(carImage); } /** * 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() { checkWorldEdge(); checkCollision(); if(Greenfoot.isKeyDown("j")) { turn(-15); } if(Greenfoot.isKeyDown("k")) { turn(15); } move(); } /** * checkWorldEdge - checks to see if the car is at the edge of the world. * If it is, turns the car away from the edge. */ public void checkWorldEdge() { if(atWorldEdge()) { turn(-75); } } /** * checkColision - checks to see if the car can see and is therefore about * to collide with another car or an obstacle in the road. If so, stops * the simulation. */ public void checkCollision() { if(canSee(Car.class)) { Greenfoot.stop(); } if(canSee(Obstacle.class)) { //if(we are showing the car image) // setImage(new GreenfootImage("flower2.png")); //otherwise // setImage(new GreenfootImage("car.png")); if(getImage() == carImage) { setImage(flowerImage); } else { setImage(carImage); } numHits = numHits + 1; if(numHits > 5) { Greenfoot.stop(); } } } }