I am recreating the game tapper for my final project. I have a class called customerSpawn that adds customers to the world.the bartender adds the drinks to the world and then serves them. I want to make it so if two drinks are coming towards a customer it removes the first one but ignores the second.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Customer here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Customer extends Actor
{
public int timer = 0;
public int drinksTimer = 0;
static boolean drinks;
public boolean Walks = true;
//customer walks images
private GreenfootImage c1 = new GreenfootImage("Customer11walking.png");
private GreenfootImage c2 = new GreenfootImage("Customer12walking.png");
private GreenfootImage c3 = new GreenfootImage("Customer13walking.png");
private GreenfootImage c4 = new GreenfootImage("Customer14walking.png");
//customer drinks images
private GreenfootImage cD1 = new GreenfootImage("cD1.png");
private GreenfootImage cD2 = new GreenfootImage("cD2.png");
private GreenfootImage cD3 = new GreenfootImage("cD3.png");
private GreenfootImage cD4 = new GreenfootImage("cD4.png");
private GreenfootImage cD5 = new GreenfootImage("cD5.png");
private int frame = 1;
private int animationCounter = 0;
private int nextImage = 0;
public int speed;
int actCounter = 0;
public void WalksAnimation()
{
if(frame == 20)
{
setImage(c1);
}
else if(frame == 40)
{
setImage(c2);
}
else if(frame == 60)
{
setImage(c3);
}
else if(frame == 80)
{
setImage(c4);
frame = 1;
return;
}
frame ++;
}
public void DrinksAnimation()
{
if(frame == 1)
{
setImage(cD1);
}
else if(frame == 2)
{
setImage(cD2);
}
else if(frame == 3)
{
setImage(cD3);
}
else if(frame == 4)
{
setImage(cD4);
}
else if(frame == 5)
{
setImage(cD5);
return;
}
}
public void speed()
{
if (Walks == true)
{
speed = 0;
move(speed);
}
else if (Walks == false)
{
speed = -1;
move(speed);
}
}
public void act()
{
Walks = true;
speed();
removeDrinksCustomer();
WalksAnimation();
{
switch(Greenfoot.getRandomNumber(2))
{
case 1:
if(Greenfoot.getRandomNumber(100)> 1)
{
Walks = false;
}
break;
case 2:
if(Greenfoot.getRandomNumber(10)> 1)
{
speed = 0;
SendDrinkBack();
Walks = true;
}
break;
}
}
}
public void removeDrinksCustomer()
{
if (Customer.drinks=true &&(isTouching(CustomerSpawn.class)))
{
{
actCounter++;
if(actCounter == 40)
{
timer++;
actCounter = 0;
}
}
if(timer ==3)
{
Actor t;
t = getOneObjectAtOffset(0, 0, CustomerSpawn.class);
if(t!=null)
{
((Bar)getWorld()).addScore(50);
World w = getWorld();
w.removeObject(t);
getWorld().removeObject(this);
timer=0;
}
}
}
}
public void SendDrinkBack()
{
{
actCounter++;
if(actCounter == 40)
{
timer++;
actCounter = 0;
}
}
if(timer == 4)
{
Empty empty = new Empty();
getWorld().addObject(empty, getX(),getY());
timer=0;
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class CustomerSpawn here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class CustomerSpawn extends Timer
{
public int numCustomerTimer;
int actCounter = 0;
public void act()
{
//customer
{
actCounter++;
if(actCounter == 10)
{
numCustomerTimer++;
actCounter = 0;
}
}
for (int i = 0; i < 1 ; i++)
{
switch(Greenfoot.getRandomNumber(4))
{
case 1:
if(Greenfoot.getRandomNumber(100)> 1 && numCustomerTimer == 59)
{
Customer c = new Customer();
getWorld().addObject(c,this.getX(),this.getY());
numCustomerTimer =0;
}
break;
case 2:
if(Greenfoot.getRandomNumber(100)> 1 && numCustomerTimer == 80)
{
Customer c2 = new Customer();
getWorld().addObject(c2,this.getX(),this.getY());
numCustomerTimer =0;
}
break;
case 3:
if(Greenfoot.getRandomNumber(100)> 1 && numCustomerTimer == 77)
{
Customer c3 = new Customer();
getWorld().addObject(c3,this.getX(),this.getY());
numCustomerTimer = 0;
}
break;
default:Greenfoot.getRandomNumber(4);
}
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bartender here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bartender extends Actor
{
// Declare arrays of GreenfootImages for animation
private GreenfootImage[] walkingRight;
private GreenfootImage[] walkingLeft;
// Declare GreenfootImages for standing
private GreenfootImage standingRight;
private GreenfootImage standingLeft;
// Two booleans for controlling the flow of the action
private boolean walking;
private boolean facingLeft;
// Integers for controlling the speed of the movement and animation
private int animationCounter;
private int animationDelay;
private int animationDelayCounter;
private int speed;
//pour drink
private GreenfootImage B1 = new GreenfootImage("B2.png");
private GreenfootImage B2 = new GreenfootImage("B3.png");
private int frame = 1;
private int animationCounter2 = 0;
private int nextImage = 0;
int timer;
private boolean isKeyPressed;
public Bartender man;
static int B1X;
static int B1Y;
static int B2X;
static int B2Y;
static int B3X;
static int B3Y;
static int B4X;
static int B4Y;
int NumKeyPressed;
int actCounter = 0;
/**
* Constructor for WalkingMan imports the necessary images, sets
* values for variables and sets this Actor's image to begin.
*/
public Bartender ()
{
// Three Strings together to build file name
String fileName;
String fileNamePrefix = "Left";
String fileNameSuffix = ".png";
// Initialize arrays
walkingRight = new GreenfootImage[3];
walkingLeft = new GreenfootImage[3];
// Use a loop to populate the two image arrays
for (int i = 0; i < walkingRight.length; i++)
{
// Build the file name: Eg. right02.png
fileName = fileNamePrefix + (i+1) + fileNameSuffix;
// Use the file for the right facing sprite
walkingRight[i] = new GreenfootImage(fileName);
// Build the left facing sprite by flipping the
// right facing sprite
walkingLeft[i] = new GreenfootImage (walkingRight[i]);
walkingLeft[i].mirrorHorizontally();
}
// import the right standing image
standingRight = new GreenfootImage ("B1.png");
// create the left standing image from the right standing image
standingLeft = new GreenfootImage (standingRight);
standingLeft.mirrorHorizontally();
// Set starting values for control variables
walking = false;
facingLeft = true;
// How fast the Actor will move
speed = 3;
// How many acts as a delay between changing frames
animationDelay = 8;
// Start delay counter at 0
animationDelayCounter = 0;
// Start the animation counter at 0. The animation counter keeps
// track of what frame the animation is on, and gets reset if the
// player changes direction. It is only updated every
// *animationDelayCounter* acts, so the animation isn't too fast.
animationCounter = 0;
// Set starting image
this.setImage (standingRight);
}
public void act()
{
checkKeys();
limits();
// animationDelayCounter is used to avoid the animation happening
// too fast. Each act it increases by 1 until it hits *animationDelay*.
animationDelayCounter++;
if(animationDelayCounter == animationDelay)
{
animationCounter++;
animationDelayCounter = 0;
}
// Animation counter controls which frame is currently being displayed.
// If it gets beyond the size of our array of images, reset it to zero
if (animationCounter > walkingRight.length -1)
animationCounter = 0;
checkServe();
if((isTouching(Empty.class)))
{
removeTouching(Empty.class);
((Bar)getWorld()).addScore(20);
}
}
/**
* Called by WalkingWorld, causes the player to move Right.
*/
public void walkRight()
{
// Check if direction is changing, and if so, reset counter
if (facingLeft)
animationCounter = 0;
// Set control booleans to not facing right and walking
walking = true;
facingLeft = false;
// Set the appropriate image. Note that animationCounter is
// controlled by the act() method.
setImage (walkingLeft[animationCounter]);
// Move the actor
setLocation (getX() + speed, getY());
}
/**
* Called by WalkingWorld, causes the player to move Left.
*/
public void walkLeft()
{
// Check if direction is changing, and if so, reset counter
if (!(facingLeft))
animationCounter = 0;
// Set control booleans to facing right and walking
walking = true;
facingLeft = true;
// Set the appropriate image. Note that animationCounter is
// controlled by the act() method.
setImage (walkingRight[animationCounter]);
// Move the actor
setLocation (getX() - speed, getY());
}
/**
* Called by WalkingWorld, causes the player to stand still.
* This method gets called when the World decides that neither
* directional button has been pushed.
*/
public void stopWalking()
{
// Stop walking
walking = false;
// Reset animation counter
animationCounter = 0;
// Reset animation delay caounter
animationDelayCounter = 0;
// Set appropriate image based on which way player was most recently
// moving
if (facingLeft)
this.setImage (standingRight);
else
this.setImage (standingLeft);
}
//temp serve code
private void serve()
{
setImage("B4.png");
Drink drink = new Drink();
getWorld().addObject(drink, getX(),getY());
drink.setLocation(getX()-1,getY()-20);
}
public void PourDrink()
{
if(frame == 1)
{
setImage(B1);
}
else if(frame == 2)
{
setImage(B2);
return;
}
}
public void checkServe()
{
if (Greenfoot.isKeyDown("space")&& (isTouching(Tap.class)))
{
timer++;
PourDrink();
if (timer >20 && Greenfoot.isKeyDown("space"))
{
serve();
timer = 0;
}
}
}
private void checkKeys()
{
// boolean to check if either direction was pressed
isKeyPressed = false;
if (Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("space") &&(isTouching(S1.class)||(isTouching(S2.class)||(isTouching(S3.class)||(isTouching(S4.class))))))
{
walkRight();
isKeyPressed = true;
}
if (Greenfoot.isKeyDown("left")&& !Greenfoot.isKeyDown("space")&&(isTouching(S1.class)||(isTouching(S2.class)||(isTouching(S3.class)||(isTouching(S4.class))))))
{
walkLeft();
isKeyPressed = true;
}
if (Greenfoot.isKeyDown("up")&& !Greenfoot.isKeyDown("space") &&(isTouching(S1.class)||(isTouching(S2.class)||(isTouching(S3.class)||(isTouching(S4.class))))))
{
NumKeyPressed++;
Up();
isKeyPressed = true;
}
if (Greenfoot.isKeyDown("down")&& !Greenfoot.isKeyDown("space")&&(isTouching(S1.class)||(isTouching(S2.class)||(isTouching(S3.class)||(isTouching(S4.class))))))
{
NumKeyPressed++;
Down();
isKeyPressed = true;
}
// In the event that no button is pressed, call the stopWalking method
if (!(isKeyPressed)&&(isTouching(S1.class)||(isTouching(S2.class)||(isTouching(S3.class)||(isTouching(S4.class))))))
{
stopWalking();
}
/*
if ((!isTouching(S1.class)||(!isTouching(S2.class)||(!isTouching(S3.class)||(!isTouching(S4.class))))))
{
speed = 0;
}
else if ((isTouching(S1.class)||(isTouching(S2.class)||(isTouching(S3.class)||(isTouching(S4.class))))))
{
speed = 3;
}
*/
}
public void coordinates()
{
B1X = 623;
B1Y=508;
B2X = 611;
B2Y=365;
B3X = 555;
B3Y=253;
B4X = 512;
B4Y=136;
}
public void limits()
{
if (isTouching(S1.class))
{
if (getX()>700)setLocation(700, getY());
if (getX()<40)setLocation(40, getY());
}
if (isTouching(S2.class))
{
if (getX()>640)setLocation(640, getY());
if (getX()<90)setLocation(90, getY());
}
if (isTouching(S3.class))
{
if (getX()>590)setLocation(590, getY());
if (getX()<140)setLocation(140, getY());
}
if (isTouching(S4.class))
{
if (getX()>540)setLocation(540, getY());
if (getX()<190)setLocation(190, getY());
}
}
public void Up()
{
if (isTouching(S1.class) && (Greenfoot.isKeyDown("up"))&& NumKeyPressed ==12)
{
//animate spawn
//set x and y cordinates for 2
coordinates();
setLocation(B2X, B2Y);
NumKeyPressed = 0;
//if (getX() > 660)setLocation(660, 400);
}
if (isTouching(S2.class) && (Greenfoot.isKeyDown("up"))&& NumKeyPressed ==12)
{
//animate spawn
//set x and y cordinates for 3
coordinates();
setLocation(B3X, B3Y);
NumKeyPressed = 0;
//setLocation(getX(), getY());
}
if (isTouching(S3.class) && (Greenfoot.isKeyDown("up"))&& NumKeyPressed ==12)
{
//animate spawn
//set x and y cordinates for 4
coordinates();
setLocation(B4X, B4Y);
NumKeyPressed = 0;
//setLocation(getX(), getY());
}
if (isTouching(S4.class) && (Greenfoot.isKeyDown("up"))&& NumKeyPressed ==12)
{
//animate spawn
//set x and y cordinates for 1
coordinates();
setLocation(B1X, B1Y);
NumKeyPressed = 0;
//setLocation(getX(), getY());
}
}
public void Down()
{
if (isTouching(S1.class) && (Greenfoot.isKeyDown("down"))&& NumKeyPressed ==12)
{
//animate spawn
//set x and y cordinates for 4
coordinates();
setLocation(B4X, B4Y);
NumKeyPressed = 0;
//if (getX() > 660)setLocation(660, 400);
}
if (isTouching(S2.class) && (Greenfoot.isKeyDown("down"))&& NumKeyPressed ==12)
{
//animate spawn
//set x and y cordinates for 1
coordinates();
setLocation(B1X, B1Y);
NumKeyPressed = 0;
//setLocation(getX(), getY());
}
if (isTouching(S3.class) && (Greenfoot.isKeyDown("down"))&& NumKeyPressed ==12)
{
//animate spawn
//set x and y cordinates for 2
coordinates();
setLocation(B2X, B2Y);
NumKeyPressed = 0;
//setLocation(getX(), getY());
}
if (isTouching(S4.class) && (Greenfoot.isKeyDown("down"))&& NumKeyPressed ==12)
{
//animate spawn
//set x and y cordinates for 3
coordinates();
setLocation(B3X, B3Y);
NumKeyPressed = 0;
//setLocation(getX(), getY());
}
isKeyPressed = true;
}
}

