soumya.__.khanna wrote...
It's still doing the same thingdanpost wrote...
should only be in your Hero class.import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Hero here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Hero extends Actor
{
public static GreenfootImage[]IDLE =
{new GreenfootImage("Idle0.png"),
new GreenfootImage("Idle1.png"),
new GreenfootImage("Idle2.png"),
new GreenfootImage("Idle3.png"),
new GreenfootImage("Idle4.png"),
new GreenfootImage("Idle5.png"),
new GreenfootImage("Idle6.png"),
new GreenfootImage("Idle7.png")};
public static GreenfootImage[] GROUNDLEFT =
{new GreenfootImage("Left1.png"),
new GreenfootImage("Left2.png"),
new GreenfootImage("Left3.png")};
public static GreenfootImage[] GROUNDRIGHT =
{new GreenfootImage("Right1.png"),
new GreenfootImage("Right2.png"),
new GreenfootImage("Right3.png")};
public boolean triggerReleased; //Once weapon has been released
public int attackCycle; //For the hero to attack
public static int LifeLine;
public int speed; //Refers to the heros' speed, and health
public int frame;
public int actCounter;
public int skipRate;
public int floor = 448;
public GreenfootImage[] animation;
//ints
//doubles to allow decimals
public double gravity, gravityIncrement;
//Booleans
public boolean groundLevel;
public boolean changeAnimation;
public boolean movingRight;
public boolean movingLeft;
public boolean damageTaken = false; //Hero has not taken any damage
public int damageTakenTimer = 0;
public void act()
{
updateAmimation();
actCounter++;
attack();
triggerReleased = true; //Trigger is released
marioMover();
checkCeiling();
floatingMario();
}
public Hero()
{
this.LifeLine = 5;
frame = 0; //initial frame
skipRate = 75; //speed of animaation
actCounter = 0; //initial act
speed = 3; //How fast it moves
gravity = 0; //Don't sink
gravityIncrement = .1; //Velocity
}
public void updateAmimation()
{
if(animate())
{
setImage(animation[frame]);
frame ++;
if( frame >= animation.length)
{
frame = 0;
}
}
}
public boolean animate()
{
return actCounter % skipRate == 0;
}
public void fireWeapons(FireBall fireThis)
{
fireThis.setRotation (getRotation());
getWorld().addObject (fireThis,getX(),getY());
fireThis.move(getImage().getWidth()/2);
}
public void attack()
{
if(attackCycle <= 0) //To avoid rapid shooting
{
if(Greenfoot.isKeyDown("M"))
{
fireWeapons(new FireBall(3, 3)); //Shoot the weapons depending on whether or not key is pressed
}
else
{
triggerReleased = true; //Otherwise, trigger is released.
}
attackCycle = 10; // n being the cooldown in act cycles.
}
else if(attackCycle > 0)
{
attackCycle--; // decrement by 1 per act cycle.
}
}
public void marioMover()
{
if (Greenfoot.isKeyDown("left")) //Go in the left direction(accoring to speed), if left key is down
{
setLocation(getX() - speed, getY());
movingLeft = true;
}
if (Greenfoot.isKeyDown("right")) //Move right
{
setLocation(getX() + speed, getY());
movingRight = true;
}
gravity(); //Fall from the top, and after jumps
}
public void gravity()
{
setLocation(getX(), getY() +(int) gravity); //Stay at the same x value, but fall with gravity
gravity = gravity + gravityIncrement; //Adding velocity
}
public void onFloor(Actor block)
{
int blockHeight = block.getImage().getHeight(); //Height of the block
int blockTop = block.getY() - blockHeight/2; //Height of a part of the block
setLocation(getX(), blockTop-getCustomHeight()); //Change to this location
skipRate = skipRate; //Reset jumping animation to normal speed
groundLevel = true;
}
private void checkCeiling()
{
try
{
Actor ceiling = getOneObjectAtOffset(0, -getCustomHeight(), Block.class);//Create information of platforms at offset
if(ceiling != null)//Check for platform(double negative)
{
groundLevel = false;
goToCeiling(ceiling);
}
}
catch(IllegalStateException ex)
{
//yay
}
}
private void goToCeiling(Actor ceiling) //Check for what is on top of Mario's head
{
int ceilingHeight = ceiling.getImage().getHeight();
int ceilingBottom = ceiling.getY() + ceilingHeight/2;
setLocation(getX(), ceilingBottom+getCustomHeight()); //Set this location
}
public void floatingMario()
{
if (getY() >= floor) //Don't go past the floor
{
setLocation(getX(), floor);
//marioDeath();
}
}
//Getters and Setters
public int getSpeed()
{
return speed; //Get information from speed
}
public void setSpeed(int s)
{
speed = s;
}
public int getHeight()
{
return getImage().getHeight();
}
public int getCustomHeight()
{
return getImage().getHeight()/4;
}
public int getWidth()
{
return getImage().getWidth();
}
public int getCustomWidth()
{
return getImage().getWidth()/4;
}
public static int getLives()
{
return LifeLine;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class SwimmingMario here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SwimmingMario extends Hero
{
//Arrays
private static GreenfootImage[] RIGHT =
{new GreenfootImage("SwimLeft0.gif"),
new GreenfootImage("SwimLeft1.gif"),
new GreenfootImage("SwimLeft2.gif"),
new GreenfootImage("SwimLeft3.gif"),};
private static GreenfootImage[] LEFT =
{new GreenfootImage("SwimRight0.gif"),
new GreenfootImage("SwimRight1.gif"),
new GreenfootImage("SwimRight2.gif"),
new GreenfootImage("SwimRight3.gif"),};
public SwimmingMario()
{
super();
animation = RIGHT; //Animation when staying the way it is
}
public void act()
{
super.act();
marioAnimator();
whatIsGround();
jumpingJumping();
checkContact();
}
public void marioAnimator()
{
if(!groundLevel)
{
if (Greenfoot.isKeyDown("space") && movingLeft)
{
animateLogic(LEFT, 9);
}
else if (Greenfoot.isKeyDown("space") && movingRight)
{
animateLogic(RIGHT, 9);
}
else if (Greenfoot.isKeyDown("left") && animation != LEFT)
{
animateLogic(LEFT, 10);
}
else if (Greenfoot.isKeyDown("right") && animation != RIGHT)
{
animateLogic(RIGHT, 10);
}
}
else
{
if (Greenfoot.isKeyDown("left") && animation != GROUNDLEFT)
{
animateLogic(GROUNDLEFT, 10);
}
else if (Greenfoot.isKeyDown("right") && animation != GROUNDRIGHT)
{
animateLogic(GROUNDRIGHT, 10);
}
else if (!Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("left") && animation != IDLE)
{
animateLogic(IDLE, 50);
}
}
}
public void animateLogic(GreenfootImage[] animation, int skipRate)
{
this.animation = animation; //The animation sequence to play
this.skipRate = skipRate ; //How fast the animation will be in each case
actCounter = skipRate - 1;
frame = 0;
}
private void whatIsGround() //Check for the floor that Mario is on
{
Actor block = getOneObjectAtOffset(0, getCustomHeight(), Block.class);//Create information of platforms at offset
if(block == null)//Check for no platform
{
groundLevel = false; //is not on ground
}
else
{
//groundLevel = true; //is on ground
onFloor(block); //initiate method on Floor
gravity = 0; //No sinking
jumpingJumping(); //Allow for jumping
}
}
public void jumpingJumping()
{
if(Greenfoot.isKeyDown("space") && (!groundLevel || groundLevel)) //regular jump
{
gravity = -4;
groundLevel = false;
changeAnimation = true;
}
}
public void marioDeath()
{
getWorld().removeObject(this);
}
public void checkContact()
{
if (isTouching(Enemies.class) && damageTaken == false)
{
Enemies tempEnemy = (Enemies)getOneIntersectingObject(Enemies.class);
decreaseLives();
getWorld().removeObject(this);
//rip();
damageTaken = true;
damageTakenTimer = 50;
//Greenfoot.delay(150);
//setLocation(getWorld().getWidth()/9, getWorld().getHeight()-floatHeight);
}
if (damageTaken == true)
{
//Prevent dying continuously when colliding
damageTakenTimer--;
if(damageTakenTimer <= 0)
{
damageTaken = false;
}
}
if(this.LifeLine == 0)
{
Greenfoot.setWorld(new Death());
}
}
public void decreaseLives()
{
this.LifeLine -= 1;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The parent class of worlds
*
* @soumya.__.khanna (Soumya Khanna)
* @I'llLookItUp (Jun 17th, 2020)
*/
public class Worlds extends World
{
public int width = getWidth(); //Width of the world --Accessed in children classes
public Hero mario = new Hero(); //Declaring the Swimming Mario Class
//Number of hearts based on the life of Mario
public Life life0;
public Life life1;
public Life life2;
public Life life3;
public Life life4;
public Score score; //Adding the score board
public void act()
{
checkLife(mario.getLives());
}
public Worlds()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(2200, 576, 1, false);
}
public void lifeCounter(int x, int y)
{
//Declaring all lives
life0 = new Life();
life1 = new Life();
life2 = new Life();
life3 = new Life();
life4 = new Life();
//Adding lifes evenly spaced around
addObject(life0, x, y);
addObject(life1, x + 30, y);
addObject(life2, x + 60, y);
addObject(life3, x + 90, y);
addObject(life4, x + 120, y);
}
public void checkLife(int numLives) //Please add comments once it starts to work!!!
{
//Make lifeCounter represent Mario's lives
if(mario.getLives() == 4)
{
removeObject(life4);
}
else if(mario.getLives() == 3)
{
removeObject(life3);
}
else if(mario.getLives() == 2)
{
removeObject(life2);
}
else if(mario.getLives() == 1)
{
removeObject(life1);
}
else if(mario.getLives() == 0)
{
removeObject(life0);
}
}
public Score getScore()
{
return score; //Information from scoreboard
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This is the underwater level world
*
* @soumya.__.khanna (Soumya Khanna)
* @IKnowIt (Jun 17th, 2020)
*/
public class WaterWorld extends Worlds
{
public void act()
{
super.act(); //To be access checkLives from world class
}
public WaterWorld()
{
//Placing all actors in the world
prepare();
}
public void prepare()
{
//Adds the floor blocks
addFloor();
//First row of blocks
topBlocks(getWidth() - 2248 ,getHeight()- 500); //Left chunck of blocks
topBlocks(getWidth() - 200,getHeight()- 500); //Right blocks
middleBlocks(getWidth()/2 ,getHeight()- 500); // Main chunk of blocks in the middle of the world
//Smaller chuncks of blocks on each side
sideBlocks(getWidth()/3 - 500 ,getHeight()- 310); //Left Side
sideBlocks(getWidth()/3 + 1300 ,getHeight()- 310); //Right Side
//Main hero
addObject(new SwimmingMario(), getWidth()/8, getHeight()/2);
//Adding Cheep Cheeps
addEnimies();
//Placing the hearts that represent Mario's life
lifeCounter(getWidth()/30, getHeight() - 50);
lifeCounter(getWidth() - 150, getHeight() - 50);
//Adding a score board in the center of the world
addObject(new Score(), getWidth()/2, getHeight()/2 + 150);
}
public void addFloor()
{
WaterFloor block = new WaterFloor(); //Declaring the WaterFloor class
int blockWidth = block.getImage().getWidth(); //Int for the width of the block
int blockHeight = block.getImage().getHeight(); //Int for height of the block
int numBlocksFloor = getWidth() / blockWidth; //The number of blocks on the floor / by width of the block so all Floor has blocks
int spacing = 2048 / numBlocksFloor; //The number of blocks divided by width of the floor
for (int i = 0; i <= numBlocksFloor; ++i)
{
addObject(new WaterFloor(), i*blockWidth, getHeight() - blockHeight/2); //First Row
addObject(new WaterFloor(), i*blockWidth, getHeight() - blockHeight/2 - 55); //Second Row
}
}
public void topBlocks(int xStart, int yStart) //Blocks on the top of the world
{
UnderwaterBlock rb = new UnderwaterBlock(); //Using underwater blocks
int blockWidth = rb.getImage().getWidth(); //Width of blocks
for(int a = 0; a < 20; a++) //20 blocks in a row
{
addObject(new UnderwaterBlock(), xStart+(a*blockWidth), yStart - 60); //Row One - Full row
addObject(new UnderwaterBlock(), xStart+((a-2)*blockWidth), yStart - 30); //Row Two - Full row - 3 blocks
addObject(new UnderwaterBlock(), xStart+((a-4)*blockWidth), yStart); //Row Three - Full row - 6 blocks
if(a % 4 == 0) //Every fourth block...
{
addObject(new CoinBlock(), xStart+((a-6)*blockWidth), yStart + 30); //Add a coin block!
}
else
{
addObject(new UnderwaterBlock(), xStart+((a-6)*blockWidth), yStart + 30); //Otherwise a regular block
}
}
}
public void middleBlocks(int xStart, int yStart) //Blocks in the middle of the world
{
UnderwaterBlock rb = new UnderwaterBlock();
int blockWidth = rb.getImage().getWidth();
for(int a = 0; a < 8; a++) //Add 8 blocks
{
if(a % 4 == 0) //Coin Blocks for...
{
addObject(new CoinBlock(), xStart+(a*blockWidth), yStart); //Top most row
addObject(new CoinBlock(), xStart+(a*blockWidth) - 200, yStart + 150); //Middle row towards the left
addObject(new CoinBlock(), xStart+(a*blockWidth) + 200, yStart + 300); //Last row towards the right
}
else //Otherwise regular blocks in all rows
{
addObject(new UnderwaterBlock(), xStart+(a*blockWidth), yStart);
addObject(new UnderwaterBlock(), xStart+(a*blockWidth) - 200, yStart + 150);
addObject(new UnderwaterBlock(), xStart+(a*blockWidth) + 200, yStart + 300);
}
}
}
public void sideBlocks(int xStart, int yStart)
{
UnderwaterBlock rb = new UnderwaterBlock();
int blockWidth = rb.getImage().getWidth();
for(int a = 0; a < 4; a++) //4 in a row
{
if(a % 4 == 0) //Coin Blocks
{
addObject(new CoinBlock(), xStart+(a*blockWidth), yStart);
addObject(new CoinBlock(), xStart+(a*blockWidth) - 175, yStart + 100);
}
else //Or regular blocks
{
addObject(new UnderwaterBlock(), xStart+(a*blockWidth), yStart); //Top Row
addObject(new UnderwaterBlock(), xStart+(a*blockWidth) - 175, yStart + 100); //Bottom Row
}
}
}
public void addEnimies()
{
int numberOfOnGounrd = 5; //5 Cheep Cheeps in one width
int spacing = 2048 / numberOfOnGounrd; //Spaced out evenly across the width of the world
int height = 576; //Height of the world
for(int i = 0; i < numberOfOnGounrd; i++)
{
addObject(new CheepCheep(), spacing / 2 + i * spacing, Greenfoot.getRandomNumber(576)); //Set one
addObject(new CheepCheep(), spacing / 2 + i * spacing, Greenfoot.getRandomNumber(576)); //Set Two
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Life here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Life extends Actor
{
public void act()
{
setLook();
}
public void setLook()
{
GreenfootImage life = new GreenfootImage(getImage());
setImage(life);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This is the game world where all actors will exist
*
* @soumya.__.khanna (Soumya Khanna)
* @5.0 (May 28th, 2019)
*/
public class GameWorld extends Worlds
{
public GameWorld()
{
//Initiating prepare, to add all actors
prepare();
}
public void prepare()
{
//Castle for mario to go into once defeating all turtles
addObject(new Castle(), getWidth() -120, getHeight() -240);
//Chuncks of Blocks in the middle
blockChunks(getWidth()/4 - 100, getHeight()/2 -65); //Left
blockChunks(getWidth()/4 + 950, getHeight()/2 -65); //Right
//Adding in the turtles
addEnimies();
//Top Blocks for Flying Koopas
topBlocks(getWidth() - 2460 ,getHeight()- 500);
topBlocks(getWidth() - 400 ,getHeight()- 500);
//Main hero
addObject(new Marioo(), getWidth()/2, getHeight() - 550);
//Hearts for life
//Adding in a scoreBorad in the middle of the world
addObject(new Score(), getWidth()/2, getHeight()/2 + 150);
}
public void blockChunks(int xStart, int yStart) //Blocks in the middle of the world
{
RegularBlock rb = new RegularBlock(); //Using brick blocks
int blockWidth = rb.getImage().getWidth(); //Width of blocks
for(int a = 0; a < 2; a++) //Two blocks of brick
{
addObject(new RegularBlock(), xStart+(a*blockWidth), yStart); //Half Brick blocks
addObject(new RegularBlock(), xStart+(a*blockWidth) - 280, yStart + 120); //Bottom two brick blocks
addObject(new CoinBlock(), xStart+(a*blockWidth + 60), yStart); //Half Coin blocks
}
for(int a = 0; a < 3; a++) //Middle Three Blocks
{
addObject(new RegularBlock(), xStart+(a*blockWidth) - 140, yStart + 60);
}
}
public void addEnimies()
{
int numberOfOnGounrd = 4; //4 Koopas in one width
int spacing = 2048 / numberOfOnGounrd; //Spaced out evenly across the width of the world
int numberOfFlying = 2; //2 Troopas in one width
int skySpacing = 2048 / numberOfFlying;
for(int i = 0; i < numberOfFlying; i++) //Adding flying Troopas
{
addObject(new Flying(), spacing / 2 + i * spacing, getHeight()- 250); //Bottom Row
addObject(new Flying(), spacing / 2 + i * spacing, getHeight()- 550); //Top Row
}
for(int i = 0; i < numberOfOnGounrd; i++)
{
addObject(new OnGround(), spacing / 2 + i * spacing, getHeight() - 100); //Adding the koopas on ground
}
}
public void topBlocks(int xStart, int yStart) //Top Line of Blocks
{
RegularBlock rb = new RegularBlock(); //Regular Blocks
int blockWidth = rb.getImage().getWidth(); //Width oF Blocks
for(int a = 0; a < 40; a++) //A Lotta Blocks
{
if(a % 4 == 0) //Every Four Blocks
{
addObject(new CoinBlock(), xStart+(a*blockWidth), yStart); //Add coin blocks
}
else //Otherwise
{
addObject(new RegularBlock(), xStart+(a*blockWidth), yStart); //Regular Blocks
}
}
}
}
Hero.LifeLine = 5;