So I have 3 obstacles: Enemy, spike and CactusEnemy. I want it so whenever the sprite hits any three of those the lives counter decreases and when it reaches zero I want it to display a gameOver message or display an image.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class sprite here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class sprite extends Actor
{
private int speed = 1; //Speed of sprite movement
private int vSpeed = 0; //Fall speed of sprite
private int acceleration = 1;
private int jumpStrength = 19; //How high the sprite can jump
private boolean jumping;
private boolean facingRight;
private boolean isKeyPressed;
public static boolean isDead;
private boolean spaceDown;
private Scroller scroll;
boolean touchingObstacles;
//Knight sprite right movement animation
private GreenfootImage knightRight1 = new GreenfootImage("knightRight1.png");
private GreenfootImage knightRight2 = new GreenfootImage("knightRight2.png");
private GreenfootImage knightRight3 = new GreenfootImage("knightRight3.png");
private GreenfootImage knightRight4 = new GreenfootImage("knightRight4.png");
private GreenfootImage knightRight5 = new GreenfootImage("knightRight5.png");
private GreenfootImage knightRight6 = new GreenfootImage("knightRight6.png");
private GreenfootImage knightRight7 = new GreenfootImage("knightRight7.png");
private GreenfootImage knightRight8 = new GreenfootImage("knightRight8.png");
private GreenfootImage knightRight9 = new GreenfootImage("knightRight9.png");
private GreenfootImage knightRight10 = new GreenfootImage("knightRight10.png");
private GreenfootImage knightRight11 = new GreenfootImage("knightRight11.png");
private GreenfootImage knightRight12 = new GreenfootImage("knightRight12.png");
private GreenfootImage knightRight13 = new GreenfootImage("knightRight13.png");
private GreenfootImage knightRight14 = new GreenfootImage("knightRight14.png");
private GreenfootImage knightRight15 = new GreenfootImage("knightRight15.png");
private GreenfootImage knightRight16 = new GreenfootImage("knightRight16.png");
private GreenfootImage knightRight17 = new GreenfootImage("knightRight17.png");
private GreenfootImage knightRight18 = new GreenfootImage("knightRight18.png");
private int frame = 1;
//Knight srpite left movement animation
private GreenfootImage knightLeft1 = new GreenfootImage("knightLeft1.png");
private GreenfootImage knightLeft2 = new GreenfootImage("knightLeft2.png");
private GreenfootImage knightLeft3 = new GreenfootImage("knightLeft3.png");
private GreenfootImage knightLeft4 = new GreenfootImage("knightLeft4.png");
private GreenfootImage knightLeft5 = new GreenfootImage("knightLeft5.png");
private GreenfootImage knightLeft6 = new GreenfootImage("knightLeft6.png");
private GreenfootImage knightLeft7 = new GreenfootImage("knightLeft7.png");
private GreenfootImage knightLeft8 = new GreenfootImage("knightLeft8.png");
private GreenfootImage knightLeft9 = new GreenfootImage("knightLeft9.png");
private GreenfootImage knightLeft10 = new GreenfootImage("knightLeft10.png");
private GreenfootImage knightLeft11 = new GreenfootImage("knightLeft11.png");
private GreenfootImage knightLeft12 = new GreenfootImage("knightLeft12.png");
private GreenfootImage knightLeft13 = new GreenfootImage("knightLeft13.png");
private GreenfootImage knightLeft14 = new GreenfootImage("knightLeft14.png");
private GreenfootImage knightLeft15 = new GreenfootImage("knightLeft15.png");
private GreenfootImage knightLeft16 = new GreenfootImage("knightLeft16.png");
private GreenfootImage knightLeft17 = new GreenfootImage("knightLeft17.png");
private GreenfootImage knightLeft18 = new GreenfootImage("knightLeft18.png");
private int frame2 = 1;
public int getHeight()
{
return getImage().getHeight();
}
public void act()
{
//Main program where multiple methods make the game functional
checkKeys(); //Calls checkKeys() function
checkFall(); //Calls checkFall() function
platformAbove(); //Calls platformAbove() function
checkRightWalls();
checkObstacles();
}
public void stopWalking()
{
setLocation(getX(), getY());
}
private void checkKeys()
{
isKeyPressed = false;
if (Greenfoot.isKeyDown("a")&&(Greenfoot.isKeyDown("d")))
{
stopWalking();
}
else if (Greenfoot.isKeyDown("d"))
{
moveRight();
isKeyPressed = true;
facingRight = true;
}
else if (Greenfoot.isKeyDown("a"))
{
moveLeft();
isKeyPressed = true;
facingRight = false;
}
if(Greenfoot.isKeyDown("w")&& jumping == false)
{
isKeyPressed = true;
jump();
}
if(!spaceDown && Greenfoot.isKeyDown("space"))
{
spaceDown = true;
shruikenRight right = new shruikenRight();
shruikenLeft left = new shruikenLeft();
if(facingRight == true)
{
getWorld().addObject(right, getX(), getY());
}
if(facingRight == false)
{
getWorld().addObject(left, getX(), getY());
}
}
if(spaceDown && !Greenfoot.isKeyDown("space"))
{
spaceDown = false;
}
if (!isKeyPressed)
{
stopWalking();
}
}
public void jump()
{
vSpeed = vSpeed - jumpStrength;
fall();
}
public void moveRight()
{
setLocation(getX() + speed, getY());
animateRight();
}
public void animateRight()
{
if(frame == 1)
{
setImage(knightRight1);
}
else if(frame == 2)
{
setImage(knightRight2);
}
else if(frame == 3)
{
setImage(knightRight3);
}
else if(frame == 4)
{
setImage(knightRight4);
}
else if(frame == 5)
{
setImage(knightRight5);
}
else if(frame == 6)
{
setImage(knightRight6);
}
else if(frame == 7)
{
setImage(knightRight7);
}
else if(frame == 8)
{
setImage(knightRight8);
}
else if(frame == 9)
{
setImage(knightRight9);
}
else if(frame == 10)
{
setImage(knightRight10);
}
else if(frame == 11)
{
setImage(knightRight11);
}
else if(frame == 12)
{
setImage(knightRight12);
}
else if(frame == 13)
{
setImage(knightRight13);
}
else if(frame == 14)
{
setImage(knightRight14);
}
else if(frame == 15)
{
setImage(knightRight15);
}
else if(frame == 16)
{
setImage(knightRight16);
}
else if(frame == 17)
{
setImage(knightRight17);
}
else if(frame == 18)
{
setImage(knightRight18);
frame = 1;
return;
}
frame ++;
}
public void moveLeft()
{
setLocation(getX() - speed, getY());
animateLeft();
}
public void animateLeft()
{
if(frame2 == 1)
{
setImage(knightLeft1);
}
else if(frame2 == 2)
{
setImage(knightLeft2);
}
else if(frame2 == 3)
{
setImage(knightLeft3);
}
else if(frame2 == 4)
{
setImage(knightLeft4);
}
else if(frame2 == 5)
{
setImage(knightLeft5);
}
else if(frame2 == 6)
{
setImage(knightLeft6);
}
else if(frame2 == 7)
{
setImage(knightLeft7);
}
else if(frame2 == 8)
{
setImage(knightLeft8);
}
else if(frame2 == 9)
{
setImage(knightLeft9);
}
else if(frame2 == 10)
{
setImage(knightLeft10);
}
else if(frame2 == 11)
{
setImage(knightLeft11);
}
else if(frame2 == 12)
{
setImage(knightLeft12);
}
else if(frame2 == 13)
{
setImage(knightLeft13);
}
else if(frame2 == 14)
{
setImage(knightLeft14);
}
else if(frame2 == 15)
{
setImage(knightLeft15);
}
else if(frame2 == 16)
{
setImage(knightLeft16);
}
else if(frame2 == 17)
{
setImage(knightLeft17);
}
else if(frame2 == 18)
{
setImage(knightLeft18);
frame2 = 1;
return;
}
frame2 ++;
}
private void fall()
{
setLocation(getX(), getY() + vSpeed);
if (vSpeed <=9)
{
vSpeed = vSpeed + acceleration;
}
jumping = true;
}
private void checkFall()
{
if(onGround()){
vSpeed = 0;
}
else{
fall();
}
}
public boolean onGround()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int)(spriteHeight / 2 + 5);
Actor ground = getOneObjectAtOffset(0, getHeight()/2, block2.class);
Actor ground2 = getOneObjectAtOffset(0, getHeight()/2, boxPlatform.class);
if(ground == null)
{
return false;
}
else
{
moveToGround(ground);
return true;
}
}
public boolean platformAbove()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int)(spriteHeight / -2);
Actor ceiling = getOneObjectAtOffset(0, yDistance, block2.class);
if(ceiling != null)
{
vSpeed = 1;
bopHead(ceiling);
return true;
}
else
{
return false;
}
}
public void bopHead(Actor ceiling)
{
int ceilingHeight = ceiling.getImage().getHeight();
int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
}
public boolean checkRightWalls()
{
int spriteWidth = getImage().getHeight();
int xDistance = (int)(spriteWidth/2);
Actor rightWall = getOneObjectAtOffset(xDistance, 0, block2.class);
if(rightWall == null)
{
return false;
}
else
{
stopByRightWall(rightWall);
return true;
}
}
public void stopByRightWall(Actor rightWall)
{
int wallWidth = rightWall.getImage().getWidth();
int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2;
setLocation(newX -5, getY());
}
public void moveToGround(Actor ground)
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
jumping = false;
}
public boolean canSee(Class clss){
Actor actor = getOneIntersectingObject(clss);
return actor != null;
}
public void checkObstacles()
{
if(canSee(OBSTACLES.class))
{
Greenfoot.setWorld(new MyWorld());
}
Actor obstacles = getOneObjectAtOffset(0,0,OBSTACLES.class);
if (obstacles != null)
{
if(touchingObstacles == false)
{
((MyWorld)getWorld()).getLives().subtractLives();
touchingObstacles = true;
}
} else
{
touchingObstacles = true;
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class MyWorld extends World
{
private Scroller scroll;
public int scene;
public int spriteStartX;
public int spriteStartY;
score score = new score();
lives lives = new lives();
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1300, 860, 1,false);
Greenfoot.setSpeed(50);
setPaintOrder(block2.class, sprite.class);
levelOne();
getScore();
getLives();
}
public void levelOne()
{
sprite sprite = new sprite();
addObject(sprite,41,753);
block2 block2 = new block2();
spike spike = new spike();
boxPlatform boxPlatform = new boxPlatform();
for (int x = 15; x < 1500; x += 47)
{
for (int y = 830; y <= 860; y += 47)
{
addObject( new block2(), x, y);
}
}
for (int x = 1510; x < 1690; x += 47)
{
for (int y = 840; y <= 870; y += 47)
{
addObject( new spike(), x, y);
}
}
for (int x = 1700; x < 2500; x += 47)
{
for (int y = 830; y <= 860; y += 47)
{
addObject( new block2(), x, y);
}
}
for (int x = 2510; x < 2750; x += 47)
{
for (int y = 840; y <= 870; y += 47)
{
addObject( new spike(), x, y);
}
}
for (int x = 2730; x < 3600; x += 47)
{
for (int y = 830; y <= 860; y += 47)
{
addObject( new block2(), x, y);
}
}
for (int x = 3610; x < 3790; x += 47)
{
for (int y = 840; y <= 870; y += 47)
{
addObject( new spike(), x, y);
}
}
for (int x = 3800; x < 4600; x += 47)
{
for (int y = 830; y <= 860; y += 47)
{
addObject( new block2(), x, y);
}
}
for (int x = 1500; x < 1700; x += 47)
{
for (int y = 650; y <= 650; y += 47)
{
addObject( new boxPlatform(), x, y);
}
}
for (int x = 2510; x < 2700; x += 47)
{
for (int y = 650; y <= 650; y += 47)
{
addObject( new boxPlatform(), x, y);
}
}
for (int x = 3400; x < 3625; x += 47)
{
for (int y = 650; y <= 650; y += 47)
{
addObject( new boxPlatform(), x, y);
}
}
for (int x = 3775; x < 4000; x += 47)
{
for (int y = 650; y <= 650; y += 47)
{
addObject( new boxPlatform(), x, y);
}
}
addObject(new CactusEnemy(), 1600, 590);
addObject(new CactusEnemy(), 2600, 590);
addObject(new CactusEnemy(), 3500, 590);
addObject(new CactusEnemy(), 3875, 590);
addObject(new CactusEnemy(), 4200, 760);
addObject(new Enemy(), 2025, 773);
addObject(new Enemy(), 3300, 773);
addObject(new Enemy(), 3850, 773);
addObject(new boxPlatform(), 1600, 450);
addObject(new coins(), 1600, 385);
addObject(new coins(), 2540, 735);
addObject(new coins(), 3000, 555);
addObject(new coins(), 3590, 750);
addObject(new coins(), 4125, 420);
addObject(new portal(), 4540, 740);
addObject(score, 75, 50);
addObject(lives, 1225, 50);
}
public void worldBGScene()
{
GreenfootImage bg = new GreenfootImage ("BG.png");
}
public void scrollSpeed(int amount)
{
scroll.checkKeyPress(amount);
}
public score getScore()
{
return score;
}
public lives getLives()
{
return lives;
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class lives here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class lives extends Actor
{
private int lives = 3;
public lives()
{
updateImage();
}
private void updateImage()
{
setImage(new GreenfootImage("Lives: " + lives, 20, Color.WHITE, Color.BLACK));
}
public void subtractLives()
{
lives--;
updateImage();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class spike here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class spike extends OBSTACLES
{
public spike()
{
GreenfootImage myImage = getImage();
int myNewHeight = (int)myImage.getHeight()/4;
int myNewWidth = (int)myImage.getWidth()/4;
myImage.scale(myNewWidth, myNewHeight);
}
public void act()
{
scrollingMethods();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Enemy here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Enemy extends OBSTACLES
{
private static int health = 3;
private int movingCounter = 0;
public Enemy()
{
}
public void act()
{
scrollingMethods();
if (movingCounter < 100) {
setLocation(getX() + 3, getY());//move right;
}
else if (movingCounter < 200) {
setLocation(getX() - 3, getY());//move left;
}
else {
movingCounter = 0;
}
movingCounter++;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class CactusEnemy here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class CactusEnemy extends OBSTACLES
{
public void act()
{
scrollingMethods();
}
}
