Hey guys,
so I am basically making a very simple game where two cars have to chase "food"(Ladung) and have to avoid getting hit by a meteor. If a certain score is hit by , say car1, I want to call a method from car2 where the game stops. I know this is a complicated way to do it but its for school :)
Car1 code:
Car2 code (basically the same but with var names changed)
Hope you can help me,
thanks in advance.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Rocket here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Car extends Actor
{
int load = 0;
int health = 10;
int score = 1;
/**
* Act - do whatever the Rocket wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
steuerung();
touching();
punktestand();
endgame();
}
public int getLoad()
{
return score;
}
public void steuerung()
{
if (Greenfoot.isKeyDown("w"))
{
setLocation(getX(), getY()-8);
}
if (Greenfoot.isKeyDown("s"))
{
setLocation(getX(), getY()+8);
}
if (Greenfoot.isKeyDown("d"))
{
setLocation(getX()+4, getY());
}
if (Greenfoot.isKeyDown("a"))
{
setLocation(getX()-4, getY());
}
}
public void touching()
{
if(isTouching(Meteor.class))
{
health = health - 1;
removeTouching(Meteor.class);
}
if(isTouching(Ladung.class) && load < 10)
{
load++;
removeTouching(Ladung.class);
}
}
public void punktestand()
{
if(isTouching(Droppoint.class))
{
score = score + load;
load = load - load;
}
}
public void endgame()
{
Actor actor = getOneIntersectingObject(Actor.class);
Car2 car2 = (Car2) actor;
int car2score = car2.getLoad();
if(car2score > 20)
{
getWorld().showText("Player 2 wins!",300, 200);
Greenfoot.stop();
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Rocket here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Car2 extends Actor
{
int load2 = 0;
int health2 = 10;
int score2 = 1;
/**
* Act - do whatever the Rocket wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
steuerung();
touching();
punktestand();
endgame();
}
public int getLoad()
{
return score2;
}
public void steuerung()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-8);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+8);
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+4, getY());
}
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-4, getY());
}
}
public void touching()
{
if(isTouching(Meteor.class))
{
health2 = health2 - 1;
removeTouching(Meteor.class);
}
if(isTouching(Ladung.class) && load2 < 10)
{
load2++;
removeTouching(Ladung.class);
}
}
public void punktestand()
{
if(isTouching(Droppoint.class))
{
score2 = score2 + load2;
load2 = load2 - load2;
}
}
public void endgame()
{
Actor actor = getOneIntersectingObject(Actor.class);
Car car = (Car) actor;
int carscore = car.getLoad();
if(carscore > 20)
{
getWorld().showText("Player 1 wins!",300, 200);
Greenfoot.stop();
}
}
}
