I have started writing a program with a jumping character who has to collect items and has to reach the end of a level (like Mario from Nintendo). If he hits a block with a "?", the block has to emit an item. How can give the block the information that he gets activated?
Here is my code:
MyWorld:
Player:
Mario:
Thank your for your help.
PS: I am a beginner in Greenfoot / Java so i would be glad to get some pieces of advise to ameliorate my program. ;-)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
private Actor Player1 = new Mario();
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1000, 600, 1);
setBackground("images/background_sky.png");
generierenDesBodens();
addObject(Player1, 100, 520);
}
public void act()
{
}
public void generierenDesBodens()
{
for(int i = 0; i<2; i++){
for(int j = 0;j < 20; j++)
{
addObject(new Floor(),j*32+16,584-32*i);
}
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
private int jumpStrength;
private int speed;
private int vSpeed;
private int acceleration;
private int timerOnGround;
private boolean triggeredJump;
private int higherJump;
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Player()
{
this.getImage().scale(32,32);
speed = 2;
vSpeed = 0;
jumpStrength = 8;
acceleration = 1;
timerOnGround = 0;
triggeredJump = false;
higherJump = 1;
}
public void act()
{
}
public void controlls(int Player) //here are the keybindings, int stands for the Player
{
if(Player==1)
{
if(Greenfoot.isKeyDown("shift"))
{
speed = 4;
}
else
{
speed = 2;
}
if(Greenfoot.isKeyDown("A"))
{
moveLeft();
}
if(Greenfoot.isKeyDown("D"))
{
moveRight();
}
if(Greenfoot.isKeyDown("space"))
{
higherJump();
}
if(Greenfoot.isKeyDown("space")&&onGround())
{
jump();
}
}
}
public boolean isNextToBlock(String Direction)
{
if(Direction=="left")
{
setLocation(getX() - 1, getY());
if(isTouching(Blocks.class))
{
setLocation(getX() + 1, getY());
return true;
}
else
{
setLocation(getX() + 1, getY());
return false;
}
}
if(Direction=="right")
{
setLocation(getX() + 1, getY());
if(isTouching(Blocks.class))
{
setLocation(getX() - 1, getY());
return true;
}
else
{
setLocation(getX() - 1, getY());
return false;
}
}
return false;
}
public boolean isBelowCeiling() //checks if the actor is at the ceiling
{
setLocation(getX(), getY() - 1);
if(isTouching(Blocks.class))
{
setLocation(getX(), getY() + 1);
return true;
}
else
{
setLocation(getX(), getY() + 1);
return false;
}
}
public void higherJump() //if the actor presses the jump key -> jump gets extended
{
if(triggeredJump == true && canExtendJump()){
vSpeed = vSpeed - higherJump;
}
}
public boolean canExtendJump() //checks if the actor is able to extend the jump
{
if(!onGround())
{
timerOnGround++;
}
if(timerOnGround < 20)
{
return true;
}
else
{
return false;
}
}
public void jump() //makes the actor jump
{
triggeredJump = true; //jump was made by pressing the key for it -> not normal falling
vSpeed = -jumpStrength;
fall();
}
public boolean onGround() //checks if the actor is on the ground
{
setLocation(getX(), getY() + 1);
if(isTouching(Blocks.class))
{
setLocation(getX(), getY() -1);
return true;
}
else
{
setLocation(getX(), getY() -1);
return false;
}
}
public boolean onGround2() //old version of checking if the actor is on the ground
{
Actor under = getOneObjectAtOffset(0, getImage().getHeight() / 2, Blocks.class);
return under != null;
}
public void checkFall() //checks if the actor has to fall or jump
{
if(onGround())
{
vSpeed = 0;
triggeredJump = false;
}
else
{
fall();
}
}
public void fall() //jumping and falling
{
if(vSpeed < 0) //actor jumps
{
for(int i = 0; i > vSpeed; i--)
{
if(isBelowCeiling())
{
vSpeed = -vSpeed;
}
else
{
setLocation(getX(), getY() - 1);
}
}
}
if(vSpeed > 0) //actor falls
{
for(int i = 0; i < vSpeed; i++)
{
setLocation(getX(), getY() + 1);
if(onGround())
{
i = 20000000;
timerOnGround = 0;
}
}
}
vSpeed = vSpeed + acceleration;
}
public void safeFall() //old version of jumping
{
setLocation(getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
}
public void moveLeft() //actor moves left
{
for(int i = 0; i < speed; i++)
{
if(!isNextToBlock("left"))
{
setLocation(getX() - 1, getY());
}
}
}
public void moveLeftSafe() //Safe of moveLeft
{
setLocation(getX() - speed, getY());
}
public void moveRight() //actor moves right
{
for(int i = 0; i < speed; i++)
{
if(!isNextToBlock("right"))
{
setLocation(getX() + 1, getY());
}
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Mario here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Mario extends Player
{
public Mario()
{
}
/**
* Act - do whatever the Mario wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
controlls(1);
checkFall();
}
}
