Answered step by step
Verified Expert Solution
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started