Question: This is a gaming programming assignment in Java. Can you please help. I have all the pictures, can you just add the code into the

This is a gaming programming assignment in Java. Can you please help. I have all the pictures, can you just add the code into the StarfishCollector .

(Add two sprites the shark and the lose message) Please give it your best shot. Will leave an excellent rating

In the StarfishCollector.java file, add a new Sprite object named shark, using the image shark.png from the images directory. (You should add this code after the water sprite is created but before the starfish sprites are created.) Set the position of shark to be near the middle of the screen. Since the positions of the starfish are randomly generated, if they overlap the shark, they need to be moved so that the player will still be able to collect the starfish and win the game. To do this, after each starfish is added to the starfish group, check if that starfish overlaps the shark, and if so, add 100 to the x-coordinate of that starfishs position. Also add a new Sprite object called loseMessage, using the image youLose.png, set its position near the center of the screen, and set its visibility to false (just like the winMessage object).

//Gurkirat Singh 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 = 100; 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!