Soccer Jugglers

Game can be Found on Greenfoot.org

Purpose of the Game

Keep the ball in the air as long as possible.  By kicking the ball, it will send it upward.  Look out, a second ball will come after a little while.  If you can keep both up, you’ve proven yourself worthy of a third ball.

This game should be played with two players, although one is optional, it is very difficult when additional balls are added.

Controls

Player 1 (left):

q – kick ball

a move -left

d move -right 

Player 2(right):

shift- kick ball

left arrow- move left

right arrow- move right

 

Players

player1 player2 player11 player22

 

player 1                                player 2

Game Content

soccer

image shows game in Greenfoot.

1

 

Ball starts above Player  1

2

Players work together to keep the ball up.

As the ball gets close to the ground user must keep it up by hitting correct  control.

3

Additional balls will be added as game goes on.

Having second Player helps with this situation.

 

 

Play Soccer Jugglers!

If Game does not load, download Greenfoot from the website and copy code.

 

**Sorry about the bug with the soccer ball.  I know it occasionally gets stuck on the players foot**

 

Coding

Stadium

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Stadium here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Stadium extends World
{

/**
* Constructor for objects of class Stadium.
*
*/
int count = 0;
private int time = 0;
public Player1 P;
public Player2 X;
public Score S;
public SoccerBall Ball = null;

public Stadium()
{
// Create a new world with 600×400 cells with a cell size of 1×1 pixels.
//while (count < 10000)

super(600, 400, 1);
P = new Player1();
X = new Player2();
Ball = new SoccerBall();
//Ball2 = new SoccerBall2();
S = new Score();
addObject(P,136,315);
addObject(X,410,315);
addObject(Ball,153,115);
addObject(S, 458, 122);
//count ++;
}
//if (count = 10000)
//{
//addObject(Ball2, 400, 115);
//}

public int getPlayerHeight()
{
return P.getImage().getHeight();
}
public int getPlayerWidth()
{
return P.getImage().getWidth();
}
public int getTime()
{
return time;
}

public void incTime()
{
time++;
}
}

 

Moving Object (Extends Actor) **Actor Extends Stadium

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MovingObject here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MovingObject extends Actor
{

public double verticalvector = 0;
public double grav = .05;

/**
* Act – do whatever the MovingObject wants to do. This method is called whenever
* the ‘Act’ or ‘Run’ button gets pressed in the environment.
*/
int w = 0;
public void act()
{
movement();
gravity();
}
public void movement()
{
// If q is down the players leg comes out, and the player is able to send the ball back into the air
//else the ball will fall to the ground.
if (Greenfoot.isKeyDown(“q”))
{
setImage(“player2.png”);
}
else
{
setImage(“player1.png”);
}
//Left and right movement for player
if (Greenfoot.isKeyDown(“a”))
{
setLocation(getX()-3,getY());
}
if (Greenfoot.isKeyDown(“d”))
{
setLocation(getX()+3,getY());
}

}

public void gravity()
{
//Gravity works with verticalvector to bring the ball down. By changing the balls
//x location, the ball falls.
if (grav == 0)
{
grav = 1;
}
if (getY() < getWorld().getHeight()-20)
{
verticalvector += grav;
setLocation(getX(),getY()+(int)verticalvector);
}
else
{
setLocation(getX(),getWorld().getHeight()-20);
verticalvector = 0;
}
}
}

 

SoccerBall (Extends Moving Object)

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class SoccerBall here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class SoccerBall extends MovingObject
{
int score = 0;
public boolean below;
int random;
int s;

public void act()
{
//If the ball gets to the edge of the world, it changes directions, and appears to bounce of edge of world.
gravity();
checkIfHit();
if (getX() <= 15)
{
s = 1;
}
if (getX() >= 585)
{
s = -1;
}
}

public void gravity()
{
//If the ball hits the gorund, its position is resety and the game restarts.
if (getY() < getWorld().getHeight()-20)
{
verticalvector += grav;
setLocation(getX()+s,getY()+(int)verticalvector);
}
if (getY() >= getWorld().getHeight()-40)
{
setLocation(getX(),getWorld().getHeight()-40);
//verticalvector = 1;
}
//System.out.println(verticalvector);
}

public boolean checkIfHit()
{
//All code below works to check if the ball should return up. If the players leg is equal the the x position of the ball and q
// is pressed, the ball will return up until it reaches a maximum height. After which the ball will fall again and repeat
// Balls movement up and down is controlled by verticalvector int this section.
Stadium S = (Stadium) getWorld();
int player_leg_height = (int)((S.getPlayerHeight()*3.0/4.0));
//int player_width = (int)((S.getPlayerWidth()));
Actor Alligned = getOneIntersectingObject(Player1.class);
Actor Alligned2 = getOneIntersectingObject(Player2.class);
if ((S.getHeight()-getY() == player_leg_height)) //&& (S.getWidth == player_Width))
{
score = score;
}
//Checks to see if ball is alligned with either players, if alligned with player and “q” is pressed down, the ball
// will be kicked, if alligned with player2 and space is down, the ballm will be kicked.
if ((S.getHeight()-getY()) < player_leg_height && Greenfoot.isKeyDown(“q”)&& (Alligned != null) || ((S.getHeight()-getY()) < player_leg_height && Greenfoot.isKeyDown(“shift”)&& (Alligned2 != null)))
{
verticalvector *= -1;
score += 1;
s = Greenfoot.getRandomNumber(2);

//System.out.println(S.getHeight()-getY());
}
if (s == 0)
{
s = -1;
}
//if ball hits “ground” the game resets
if (getY() > 355)
{
Greenfoot.setWorld(new Stadium());
score = 0;
}
return false;
}

public int getScore()
{
return score;
}
}

 

Player1 (Extends MovingObject)

 

import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Phillip here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player1 extends MovingObject
{

/**
* Act – do whatever the Player1 wants to do. This method is called whenever
* the ‘Act’ or ‘Run’ button gets pressed in the environment.
*/

int count = 0;
public void act()
{
// Add your action code here.
movement();
count ++;
if (count == 800)
{
count = 0;
getWorld().addObject(new SoccerBall2(),400,115);
}

}

}

 

Score (Extends Actor)

public void act()
{
score = ((SoccerBall) getWorld().getObjects(SoccerBall.class).get(0)).getScore();
setImage(new GreenfootImage(“Score: ” + score+” “, 20, Color.white, Color.black));
}

 

Player2 and SoccerBall2:

*8 Both codes replicate previous ones of Player1 and SoccerBall.  Only changes include different keys pressed to move player.

 

code1

 

code1
This part of the code was the most difficult. When the ball is aligned with the player and the y and x coordinates are correct as well the the correct key is pressed the balls vertical vector will change sending it upward.

 

Leave a Reply

Your email address will not be published. Required fields are marked *