Hi all,
I'm working on improving a platform game and I'm implementing levels into it. If this is the case, how do I modify the code where "game overs" and "game wins" if necessary? (I already know I want to try and put in the line below somewhere in the code):
(Here's the code for my current world, and below that is the code for the player, though you only need to concern yourself with the UnlockDoor and CoinCollection_MonsterKill_or_KeyCollection methods.)
Greenfoot.setWorld(new DungeonEscape_LV2()); //Or whatever level the player progresses to
import greenfoot.*;
public class DungeonEscape_LV1 extends World
{
int WorldWidth = getWidth();
int WorldHeight = getHeight();
private int Score = 0;
private int Count = 60;
private int DisplayCount = 60;
public DungeonEscape_LV1() {
super(950, 750, 1, false);
addObject(new Door(), 50, WorldHeight - 60);
addObject(new DungeonMonster(), WorldWidth - 330, WorldHeight - 60);
addObject(new Coin(), 250, WorldHeight - 70);
addObject(new Coin(), 350, WorldHeight - 70);
addObject(new Coin(), 450, WorldHeight - 70);
addObject(new Coin(), 700, WorldHeight - 230);
addObject(new DungeonMonster(), 130, WorldHeight - 230);
addObject(new Platform(), 180, WorldHeight - 150);
addObject(new Platform(), 540, WorldHeight - 150);
addObject(new ThePlayer(), (WorldWidth / 2) + 100, WorldHeight - 420);
addObject(new Platform(), (WorldWidth / 2) + 100, WorldHeight - 340);
addObject(new Platform(), 130, (150 + 70));
addObject(new Coin(), 50, 145);
addObject(new Coin(), 150, 145);
addObject(new Coin(), 250, 145);
addObject(new DungeonMonster(), WorldWidth - 170, 100);
addObject(new Key(), WorldWidth - 50, 95);
addObject(new Platform(), WorldWidth - 170, 180);
}
public void act() {
UserInfo myInfo = UserInfo.getMyInfo();
// myInfo.setScore(0);
// myInfo.store();
showText("Score: " + Score, 50, 25);
Count--;
if (Count == 0) {
DisplayCount--;
Count = 60;
}
showText("Timer: " + DisplayCount, WorldWidth - 100, 25);
showText("Best Time: " + myInfo.getScore(), WorldWidth - 75, 50);
if (DisplayCount <= 0) {
Greenfoot.stop();
}
}
public void Coin_ScoreIncrease() {
Score = Score + 50;
}
public void MonsterKill_ScoreIncrease() {
Score = Score + 100;
}
public void BestTimes() {
UserInfo myInfo = UserInfo.getMyInfo();
if (UserInfo.isStorageAvailable()) {
if (DisplayCount > myInfo.getScore()) {
myInfo.setScore(DisplayCount);
myInfo.store();
}
}
Greenfoot.stop();
}
}import greenfoot.*;
public class ThePlayer extends Actor
{
int VerticalSpeed = 0;
int Gravity = 1;
int JumpCount = 0;
int KeyUnlocks = 0;
private int HeightToGround = getImage().getHeight()/2; //50
private int SideWidth = getImage().getWidth()/2; //50
int PlatformHeight = 50;
boolean OnGround;
GreenfootSound CoinPickup = new GreenfootSound("Coin Pickup.mp3");
private GreenfootImage Right = new GreenfootImage("ThePlayer (Facing Right).png");
private GreenfootImage Left = new GreenfootImage("ThePlayer (Facing Left).png");
public void act()
{
HorizontalMovement();
VerticalMovement();
UnlockDoor();
CoinCollection_MonsterKill_or_KeyCollection();
}
public void HorizontalMovement() {
int WorldWidth = getWorld().getWidth();
int X = getX();
int DirectionX = 0;
setRotation(0);
if (Greenfoot.isKeyDown("Left")) {
DirectionX--;
}
if (Greenfoot.isKeyDown("Right")) {
DirectionX++;
}
setLocation (X + (DirectionX * 5), getY());
if (DirectionX == -1) {
setImage(Left);
}
if (DirectionX == 1) {
setImage(Right);
}
if (getX() < SideWidth) {
setLocation(SideWidth, getY());
}
if (getX() > WorldWidth - SideWidth) {
setLocation(WorldWidth - SideWidth, getY());
}
if (getOneIntersectingObject(Platform.class) != null) {
setLocation(X - DirectionX, getY());
}
}
public void VerticalMovement() {
int WorldHeight = getWorld().getHeight();
int Y = getY();
VerticalSpeed += Gravity;
setLocation (getX(), Y + VerticalSpeed);
int dy = (int)Math.signum(VerticalSpeed);
if (getOneIntersectingObject(null) != null) {
if (dy > 0) {
setLocation (getX(), Y - dy - 1);
OnGround = true;
VerticalSpeed = 0;
}
else if (dy <= 0) {
setLocation(getX(), Y + dy + 1);
OnGround = false;
}
}
if (getY() > WorldHeight - HeightToGround) {
setLocation (getX(), WorldHeight - HeightToGround);
VerticalSpeed = 0;
OnGround = true;
}
if (Greenfoot.isKeyDown("Up") && OnGround == true) {
VerticalSpeed = -20;
OnGround = false;
}
}
public void UnlockDoor() {
if (Greenfoot.isKeyDown("Down")) {
Actor Door = getOneIntersectingObject(Door.class);
if (Door != null && KeyUnlocks == 1) {
DungeonEscape_LV1 myWorld = (DungeonEscape_LV1)getWorld();
Greenfoot.playSound("Game Win.mp3");
}
}
}
public void CoinCollection_MonsterKill_or_KeyCollection() {
DungeonEscape_LV1 myWorld = (DungeonEscape_LV1)getWorld();
Actor Coin = getOneIntersectingObject(Coin.class);
Actor Key = getOneIntersectingObject(Key.class);
if (Coin != null) {
CoinPickup.play();
myWorld.removeObject(Coin);
myWorld.Coin_ScoreIncrease();
}
if (Key != null) {
CoinPickup.play();
myWorld.removeObject(Key);
KeyUnlocks = 1;
}
Actor Dead_DungeonMonster = getOneObjectAtOffset(0, HeightToGround + 1, DungeonMonster.class); //From above
Actor Killing_DungeonMonster_1 = getOneObjectAtOffset(0, -(HeightToGround + 5), DungeonMonster.class); //From below
Actor Killing_DungeonMonster_2 = getOneObjectAtOffset((SideWidth + 5), 0, DungeonMonster.class); //From the left
Actor Killing_DungeonMonster_3 = getOneObjectAtOffset(-(SideWidth + 5), 0, DungeonMonster.class); //From the right
if (Dead_DungeonMonster != null) {
myWorld.removeObject(Dead_DungeonMonster);
myWorld.MonsterKill_ScoreIncrease();
VerticalSpeed = -10;
Greenfoot.playSound("Monster Kill.mp3");
}
if (Killing_DungeonMonster_1 != null) {
getWorld().removeObject(getWorld().getObjects(ThePlayer.class).get(0));
Greenfoot.stop();
Greenfoot.playSound("Game Over.mp3");
}
if (Killing_DungeonMonster_2 != null) {
getWorld().removeObject(getWorld().getObjects(ThePlayer.class).get(0));
Greenfoot.stop();
Greenfoot.playSound("Game Over.mp3");
}
if (Killing_DungeonMonster_3 != null) {
getWorld().removeObject(getWorld().getObjects(ThePlayer.class).get(0));
Greenfoot.stop();
Greenfoot.playSound("Game Over.mp3");
}
}
}


