////////////////////////////////////////////////////////////////////////////// /** * * Code at the end of lecture 9/21/09 * Inserted code for recognizing keyboard input. * */ ////////////////////////////////////////////////////////////////////////////// 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 { /** * 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(); if(canSee(Car.class)) { turn(34); } if(Greenfoot.getRandomNumber(100) < 25) { turn(Greenfoot.getRandomNumber(13) - 6); } move(); } public void checkWorldEdge() { if( atWorldEdge() ) { turn(-75); } } } ////////////////////////////////////////////////////////////////////////////// /** * * Code at the beginning of lecture 9/25/09 * Car responds to keyboard from user - stops simulation if it hits anything * inside the world. * */ ////////////////////////////////////////////////////////////////////////////// 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 { /** * 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)) { Greenfoot.stop(); } } } ////////////////////////////////////////////////////////////////////////////// /** * * Code at the end of lecture 9/25/09 * CarWorld inserts a Car and several obstacles (some at random locations). * Car will change its image when it hits and obstacle. * */ ////////////////////////////////////////////////////////////////////////////// import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class CarWorld here. * * @author (your name) * @version (a version number or a date) */ public class CarWorld extends World { /** * Constructor for objects of class CarWorld. * */ public CarWorld() { // Create a new world with 20x20 cells with a cell size of 10x10 pixels. super(560, 560, 1); addObject(new Car(), 100, 100); addObject(new Obstacle(), 200, 375); addObject(new Obstacle(), 150, 456); addObject(new Obstacle(), 45, 267); addObject(new Obstacle(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Obstacle(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Obstacle(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Obstacle(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); addObject(new Obstacle(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560)); } } 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 { /** * 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)) { setImage(new GreenfootImage("flower2.png")); } } }