Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the shark should be looking directly at the turtle at all times; to accomplish this, first create a vector pointing from the shark to the

the shark should be looking directly at the turtle at all times; to accomplish this, first create a vector pointing from the shark to the turtle, by calculating the turtles position coordinates minus the sharks position coordinates. Then set the angle of the shark to the angle of this vector. Finally, you will need to set the shark to be flipped (not mirrored) when the turtle is to its left. code: https://github.com/stemkoski/B.A.G.E.L.-Java/tree/main/Part%2031

java code:

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_2

Step: 3

blur-text-image_3

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

Postgresql 16 Administration Cookbook Solve Real World Database Administration Challenges With 180+ Practical Recipes And Best Practices

Authors: Gianni Ciolli ,Boriss Mejias ,Jimmy Angelakos ,Vibhor Kumar ,Simon Riggs

1st Edition

1835460585, 978-1835460580

More Books

Students also viewed these Databases questions

Question

1. Compare and contrast a static, cycle, and single-use menu.

Answered: 1 week ago

Question

design a simple disciplinary and grievance procedure.

Answered: 1 week ago