Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java. when creating each starfish, set its width and height to a random number between 40 and 60; this should cause each starfish to look

java. when creating each starfish, set its width and height to a random number between 40 and 60; this should cause each starfish to look slightly different in the update method, rotate each of the starfish by a randomly generated value between -2 and 2; this should cause each starfish to look like it is wiggling

public class StarfishCollector extends Game
{
Sprite water;
Group starfishGroup;
Sprite turtle;
Sprite winMessage;
Group rockGroup;
Sprite shark;
public void initialize()
{
setTitle("Starfish Collector");
setWindowSize(800, 600);
water = new Sprite();
water.setTexture( new Texture("images/water.png") );
water.setPosition(400,300);
group.add( water );
rockGroup = new Group();
Texture rockTexture = new Texture("images/rock.png");
int rockCount = 3;
for (int i = 0; i < rockCount; i++)
{
Sprite rock = new Sprite();
double x = Math.random() * 600 + 100;
double y = Math.random() * 400 + 100;
rock.setPosition(x, y);
rock.setTexture(rockTexture);
rockGroup.add( rock );
}
group.add( rockGroup );
shark = new Sprite();
shark.setPosition(400,300);
shark.setTexture( new Texture("images/shark.png") );
group.add( shark );
starfishGroup = new Group();
Texture starfishTexture = new Texture("images/starfish.png");
int starfishCount = 20;
for (int i = 0; i < starfishCount; i++)
{
Sprite starfish = new Sprite();
double x = Math.random() * 600 + 100;
double y = Math.random() * 400 + 100;
starfish.setPosition(x, y);
starfish.setTexture(starfishTexture);
boolean rockOverlap;
do
{
rockOverlap = false;
x = Math.random() * 600 + 100;
y = Math.random() * 400 + 100;
starfish.setPosition(x, y);
for (Entity entity : rockGroup.getList())
{
Sprite rock = (Sprite)entity;
if (rock.overlaps(starfish))
rockOverlap = true;
}
} while( rockOverlap );
starfishGroup.add( starfish );
}
group.add(starfishGroup);
turtle = new Sprite();
turtle.setPosition(90, 90);
turtle.setTexture( new Texture("images/turtle.png") );
group.add(turtle);
// optional: add a transparent water layer on top
// to create an "underwater" effect
Sprite water2 = new Sprite();
water2.setTexture( new Texture("images/water.png") );
water2.setPosition(400,300);
water2.opacity = 0.30;
group.add( water2 );
winMessage = new Sprite();
winMessage.setPosition(400, 300);
winMessage.setTexture( new Texture("images/youWin.png") );
winMessage.visible = false;
group.add(winMessage);
}
public void update()
{
if (winMessage.visible)
return;
if (input.isKeyPressed("RIGHT"))
{
turtle.moveBy(2, 0);
turtle.setAngle(0);
}
if (input.isKeyPressed("LEFT"))
{
turtle.moveBy(-2, 0);
turtle.setAngle(180);
}
if (input.isKeyPressed("UP"))
{
turtle.moveBy(0, -2);
turtle.setAngle(270);
}
if (input.isKeyPressed("DOWN"))
{
turtle.moveBy(0, 2);
turtle.setAngle(90);
}
if ( turtle.position.x < shark.position.x )
shark.mirrored = true;
if ( turtle.position.x > shark.position.x )
shark.mirrored = false;
for ( Entity entity : starfishGroup.getList() )
{
Sprite starfish = (Sprite)entity;
if ( turtle.overlaps(starfish) )
starfishGroup.remove(starfish);
}
for ( Entity entity : rockGroup.getList() )
{
Sprite rock = (Sprite)entity;
turtle.preventOverlap(rock);
}
if (starfishGroup.size() == 0)
winMessage.visible = true;
turtle.boundToScreen(800, 600);
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Q.1. Taxonomic classification of peafowl, Tiger and cow ?

Answered: 1 week ago

Question

Q .1. Different ways of testing the present adulterants ?

Answered: 1 week ago

Question

Q.1. Health issues caused by adulteration data ?

Answered: 1 week ago