Question: profile-image Video Game Development TYPE OUT ANSWERS, DO NOT HANDWRITE In Android Studio, using the code below (2 classes), modify DragonAnimationDemo4 to add and draw

profile-image Video Game Development

TYPE OUT ANSWERS, DO NOT HANDWRITE

In Android Studio, using the code below (2 classes), modify DragonAnimationDemo4 to add and draw two or more cloud sprites (or just textures) that move slowly across the top portion of the screen.

*Make sure to show where you made the changes.*

CODE -

--------------------------------------------------------------

DragonAnimationDemo4.java -

/** * This demo, based on DragonAnimationDemo3, uses DragonSprite (a Sprite subclass) * for the dragon sprite. * * This would serve as a game core mechanic demo, if the game was a duck hunt type game * (https://en.wikipedia.org/wiki/Duck_Hunt), but with dragons instead of ducks. */

package com.gamefromscratch.graphicsdemo;

import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Pixmap; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Vector3;

public class DragonAnimationDemo4 implements ApplicationListener { private SpriteBatch batch; private float elapsedTime; // Make a dragon sprite private DragonSprite dragonSprite; // Can't match mouse click position to dragon sprite rectangle without camera OrthographicCamera camera; // Needed to unproject mouse click position in render Vector3 mousePosition; // Cursor change code per http://tiny.cc/a061tz // (original: http://www.martinrohwedder.dk/2016/02/libgdx-tutorial-how-to-change-the-mouse-cursor-image/) Pixmap cursorPixmap; @Override public void create() { batch = new SpriteBatch(); // Construct dragon sprite dragonSprite = new DragonSprite(); // Create the camera & mousePosition vector camera = new OrthographicCamera(); camera.setToOrtho(false, 1000, 1000); // Same width & height in DesktopLauncher mousePosition = new Vector3(); // Start dragonSprite off screen dragonSprite.setPosition(-100, 500); // alienXhairs.png, when loaded as a Pixmap, must have dimensions that are a power of 2, // e.g, 32, 64, 128 ... cursorPixmap = new Pixmap( Gdx.files.internal("cursor/alienXhairs.png") ); Gdx.graphics.setCursor( Gdx.graphics.newCursor(cursorPixmap, cursorPixmap.getWidth() / 2, cursorPixmap.getHeight() / 2) ); }

@Override public void dispose() { batch.dispose(); cursorPixmap.dispose(); }

@Override public void render() { Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Tell the camera to update its matrices. camera.update(); // Tell the SpriteBatch to render in the // coordinate system specified by the camera. batch.setProjectionMatrix(camera.combined); batch.begin(); dragonSprite.draw(batch); batch.end();

elapsedTime += Gdx.graphics.getDeltaTime();

// Move dragonSprite across screen horizontally, or vertically... if (dragonSprite.isAlive()) { dragonSprite.setX(dragonSprite.getX() + 5); // If dragonSprite leaves screen horizontally, it will come around again if (dragonSprite.getX() > 1100) { dragonSprite.setPosition(-100, 500); } } else { dragonSprite.setY(dragonSprite.getY() - 10); } // Set the next frame for the dragonSprite dragonSprite.update(elapsedTime); // If mouse click on dragon, kill dragon if (Gdx.input.isTouched()) { // Camera unprojection of mouse click position explained at // https://cssegit.monmouth.edu/jchung/libgdx.wiki/wikis/a-simple-game mousePosition.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(mousePosition); if (dragonSprite.getBoundingRectangle().contains(mousePosition.x, mousePosition.y) && dragonSprite.isAlive()) { dragonSprite.kill(); } } }

@Override public void resize(int width, int height) { }

@Override public void pause() { }

@Override public void resume() { } } DragonSprite.java -

/** * DragonSprite class to be used with DragonAnimationDemo3 * * Uses dragon_small texture atlas. * * See important note below on setBounds(). */

package com.gamefromscratch.graphicsdemo;

import com.badlogic.gdx.Gdx; import com.badlogic.gdx.audio.Sound; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.Array;

public class DragonSprite extends Sprite { private TextureAtlas textureAtlas; private Animation animation; // Save flying dragon sprite frames in an Array for later: private Array flyingFrames; // Store dead dragon sprite frame for later: private TextureRegion deadDragon; // Dragon sprite status: private boolean alive; private Sound dragonDeathSound;

public DragonSprite() { textureAtlas = new TextureAtlas(Gdx.files.internal("za_dragon/dragon_small.atlas")); // When constructing the animation from dragon.atlas, tell textureAtlas to // find only regions (frames) that are "flying", which should omit the // "dead" frame: flyingFrames = textureAtlas.findRegions("flying"); animation = new Animation(1/5f, flyingFrames); // Store "dead" dragon frame from textureAtlas: deadDragon = textureAtlas.findRegion("dead"); // ==> A Sprite is not drawable until its texture (region) and bounds are set // (per http://bit.ly/2fzJove). So, the next line is essential: setBounds(0, 0, deadDragon.getRegionWidth(), deadDragon.getRegionHeight()); // Could have also used // setBounds(0, 0, flyingFrames.get(0).getRegionWidth(), flyingFrames.get(0).getRegionHeight()); // since all frames of the dragon sprite sheet are the same width and height. // Dragon sprite will start with the first flying frame (a TextureRegion) in the Array: setRegion(flyingFrames.get(0)); // Using super.setRegion() alive = true; // Choose one of two death sounds randomly if (MathUtils.random(0, 1) == 0) { dragonDeathSound = Gdx.audio.newSound(Gdx.files.internal("za_dragon/dragon_death1.wav")); } else { dragonDeathSound = Gdx.audio.newSound(Gdx.files.internal("za_dragon/dragon_death2.wav")); } } public boolean isAlive() { return alive; } public void kill() { alive = false; setColor(Color.ORANGE); dragonDeathSound.play(); } public void update(float elapsedTime) { // Set the next frame for the DragonSprite: if (alive) { // Set DragonSprite's next frame from the animation: // setRegion(animation.getKeyFrame(elapsedTime, true)); // getKeyFrame() may return Object in some instances, so would need // to type cast to TextureRegion: setRegion((TextureRegion) animation.getKeyFrame(elapsedTime, true)); } else { setRegion(deadDragon); } }

}

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 Programming Questions!