Skip to content

Instantly share code, notes, and snippets.

@micache
Created June 26, 2019 12:37
Show Gist options
  • Save micache/574343ccc8a9a46b617aaa68c044ae23 to your computer and use it in GitHub Desktop.
Save micache/574343ccc8a9a46b617aaa68c044ae23 to your computer and use it in GitHub Desktop.
Wrong at line 122(Arrayindexoutofboundsexception)
package com.libgdxproject.firstproject;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.Screen;
import org.w3c.dom.css.Rect;
import java.util.Iterator;
public class firstproject implements Screen {
final Drop game;
private Texture dropImage;
private Texture bucketImage;
private Texture heartImage;
private Sound dropSound;
private Sound fallSound;
private Sound loseSound;
private Sound loseHeartSound;
private Music rainMusic;
private OrthographicCamera camera;
private SpriteBatch batch;
private Rectangle bucket;
private Array<Rectangle> hearts;
private Array<Rectangle> raindrops;
private long lastDropTime;
private int dropColleted=0,tong=0;
public firstproject(final Drop game) {
this.game = game;
raindrops = new Array<Rectangle>();
SpawnRaindrop();
bucket = new Rectangle();
bucket.x = 800/2 - 60/2;
bucket.y = 20;
bucket.width = 60;
bucket.height = 60;
camera = new OrthographicCamera();
camera.setToOrtho(false,800,400);
batch = new SpriteBatch();
dropImage = new Texture(Gdx.files.internal("droplet.png"));
bucketImage = new Texture(Gdx.files.internal("bucket.png"));
heartImage = new Texture(Gdx.files.internal("live.png"));
dropSound = Gdx.audio.newSound(Gdx.files.internal("dropsound.wav"));
rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rainsound.mp3"));
fallSound = Gdx.audio.newSound(Gdx.files.internal("fall.wav"));
loseSound = Gdx.audio.newSound(Gdx.files.internal("loser.mp3"));
loseHeartSound = Gdx.audio.newSound(Gdx.files.internal("heartlose.wav"));
rainMusic.setLooping(true);
}
@Override
public void render(float delta) {
hearts = new Array<Rectangle>();
Gdx.gl.glClearColor(0,0,0.2f,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.font.draw(game.batch,"Drop Collected :" + dropColleted,0,400);
game.batch.draw(bucketImage,bucket.x,bucket.y,bucket.width,bucket.height);
for (int i=760; i>=660; i-=50) {
Rectangle heart = new Rectangle();
heart.x = i;
heart.y = 360;
heart.width = 30;
heart.height = 30;
hearts.add(heart);
game.batch.draw(heartImage, heart.x, heart.y, heart.width, heart.height);
}
for (Rectangle raindrop: raindrops) {
game.batch.draw(dropImage,raindrop.x,raindrop.y);
}
game.batch.end();
if (Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(),Gdx.input.getY(),0);
camera.unproject(touchPos);
bucket.x = touchPos.x - 60 / 2;
}
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) bucket.y += 200 * Gdx.graphics.getDeltaTime();
if (bucket.x < 0) bucket.x = 0;
if (bucket.x > 800-60) bucket.x = 800-60;
if (TimeUtils.nanoTime() - lastDropTime > 1000000000) SpawnRaindrop();
Iterator<Rectangle> iter = raindrops.iterator();
Iterator<Rectangle> it = hearts.iterator();
while (iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
if (raindrop.y + 60 < 0) {
iter.remove();
fallSound.play();
it.remove();
loseHeartSound.play();
if (!it.hasNext()) loseSound.play();
}
if (raindrop.overlaps(bucket)) {
dropColleted++;
dropSound.play();
iter.remove();
}
}
}
private void SpawnRaindrop() {
Rectangle raindrop = new Rectangle();
raindrop.x = MathUtils.random(0,800-60);
raindrop.y = 480;
raindrop.width = 60;
raindrop.height = 60;
raindrops.add(raindrop);
lastDropTime = TimeUtils.nanoTime();
}
@Override
public void resize(int width,int height) {
}
@Override
public void show() {
rainMusic.play();
}
@Override
public void dispose() {
dropImage.dispose();
bucketImage.dispose();
dropSound.dispose();
rainMusic.dispose();
fallSound.dispose();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment