Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mygdx.game;
- import android.os.Bundle;
- import com.badlogic.gdx.backends.android.AndroidApplication;
- import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
- public class AndroidLauncher extends AndroidApplication {
- protected void onCreate (Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
- initialize(new FlappyCoal(), config);
- }
- }
- package com.mygdx.game;
- import com.badlogic.gdx.ApplicationAdapter;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- public class FlappyCoal extends ApplicationAdapter {
- private Map MAP;
- private Stephan STEPHAN;
- private Counter COUNTER;
- private SpriteBatch batch;
- public void create () {
- creating();
- }
- public void render () {
- Gdx.gl.glClearColor(0, 0, 0, 0);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- batch.begin();
- MAP.renderMap(batch, STEPHAN);
- STEPHAN.move(MAP);
- COUNTER.renderCounter(batch, MAP, STEPHAN);
- STEPHAN.renderStephan(batch, MAP, STEPHAN);
- try{
- batch.end();
- }
- catch(Exception e){}
- }
- public void creating () {
- MAP = new Map();
- STEPHAN = new Stephan(this);
- COUNTER = new Counter();
- batch = new SpriteBatch();
- COUNTER.createCounter();
- STEPHAN.createStephan();
- MAP.createMap(STEPHAN);
- }
- }
- package com.mygdx.game;
- import java.util.Timer;
- import java.util.TimerTask;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.audio.Sound;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Rectangle;
- public class Stephan {
- private boolean timerExists = false;
- private boolean death;
- private final int DELAY = 0;
- private final int SPEED = 5;
- private float count;
- private Timer moving;
- private TimerTask moving2;
- private Texture stephanHawking_alive = new Texture("bird/bird_alive.png");
- private Texture stephanHawking_dead = new Texture("bird/bird_dead.png");
- private Sprite stephanHawking2;
- private float x;
- private float y;
- private float[] y2;
- private Rectangle stephanHawk;
- private Sound fly;
- private final FlappyCoal myGdxGame;
- public Stephan(FlappyCoal myGdxGame){
- this.myGdxGame = myGdxGame;
- }
- public void createStephan(){
- y2 = new float[]{
- Gdx.graphics.getHeight()/1080, Gdx.graphics.getHeight()/18, Gdx.graphics.getHeight()/17.7f, Gdx.graphics.getHeight()/14.4f, Gdx.graphics.getHeight()/14.2f,
- Gdx.graphics.getHeight()/13.5f, Gdx.graphics.getHeight()/13.3f, Gdx.graphics.getHeight()/12.7f, Gdx.graphics.getHeight()/12.56f, Gdx.graphics.getHeight()/11.37f,
- Gdx.graphics.getHeight()/11.25f, Gdx.graphics.getHeight()/10.8f, Gdx.graphics.getHeight()/10.69f, Gdx.graphics.getHeight()/10.29f, Gdx.graphics.getHeight()/10.19f,
- Gdx.graphics.getHeight()/9.82f, Gdx.graphics.getHeight()/9.73f, Gdx.graphics.getHeight()/9.39f, Gdx.graphics.getHeight()/9.31f, Gdx.graphics.getHeight()/8.3f,
- Gdx.graphics.getHeight()/8.25f, Gdx.graphics.getHeight()/6.75f, Gdx.graphics.getHeight()/6.71f
- };
- death = false;
- count = 0;
- fly = Gdx.audio.newSound(Gdx.files.internal("bird/fly.wav"));
- if(!timerExists){
- timerExists = true;
- moving = new Timer();
- moving2 = new TimerTask(){
- public void run(){
- if(count>0){
- count++;
- }
- if(count>=y2[0] && count<=y2[1]){
- y+=3;
- }
- if(count>=y2[2] && count<=y2[3]){
- y+=1;
- }
- if(count>=y2[4] && count<=y2[5]){
- y+=0.5f;
- }
- if(count>=y2[6] && count<=y2[7]){
- y+=0.3f;
- }
- if(count>=y2[8] && count<=y2[9]){
- y-=0.1f;
- }
- if(count>=y2[10] && count<=y2[11]){
- y-=0.3f;
- }
- if(count>=y2[12] && count<=y2[13]){
- y-=0.5f;
- }
- if(count>=y2[14] && count<=y2[15]){
- y-=1;
- }
- if(count>=y2[16] && count<=y2[17]){
- y-=2;
- }
- if(count>=y2[18] && count<=y2[19]){
- y-=3;
- }
- if(count>=y2[20] && count<=y2[21]){
- y-=3.5f;
- }
- if(count>=y2[22]) {
- y -= 4;
- }
- }
- };
- moving.schedule(moving2, DELAY, SPEED);
- }
- stephanHawking2 = new Sprite(stephanHawking_alive);
- y = Gdx.graphics.getHeight()/2-Gdx.graphics.getHeight()/13.5f/2;
- x = Gdx.graphics.getWidth()/2-Gdx.graphics.getWidth()/24/2;
- createRec();
- }
- public void renderStephan(SpriteBatch batch, Map MAP, Stephan STEPHAN){
- System.out.println(count);
- batch.draw(stephanHawking2, x, y, stephanHawking2.getOriginX(), stephanHawking2.getOriginY(), Gdx.graphics.getWidth()/24,
- Gdx.graphics.getHeight()/13.5f, stephanHawking2.getScaleX(), stephanHawking2.getScaleY(), stephanHawking2.getRotation());
- stephanHawk.setX(x);
- stephanHawk.setY(y);
- if(y<=-Gdx.graphics.getHeight()/7.2f){
- death = true;
- y = -Gdx.graphics.getHeight()/7.2f;
- if(Gdx.input.justTouched()){
- myGdxGame.creating();
- }
- }
- }
- public void setCount1(){
- count = 1;
- }
- public boolean getDeath(){
- return death;
- }
- public void move(Map MAP){
- if(MAP.getMove()){
- if(Gdx.input.justTouched()){
- if(y<=Gdx.graphics.getHeight()){
- count = Gdx.graphics.getHeight()/1080;
- fly.play(0.5f);
- }
- }
- }
- }
- public void setHawking(){
- stephanHawking2.setTexture(stephanHawking_dead);
- }
- private void createRec(){
- stephanHawk = new Rectangle();
- stephanHawk.setX(x);
- stephanHawk.setY(y);
- stephanHawk.setHeight(Gdx.graphics.getHeight()/13.5f);
- stephanHawk.setWidth(Gdx.graphics.getWidth()/24);
- }
- public Rectangle getStephan(){
- return stephanHawk;
- }
- }
- package com.mygdx.game;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- public class Counter {
- private String text;
- private BitmapFont font;
- private int count;
- private float x;
- private float y;
- public void createCounter(){
- text = "Score: %s";
- x = Gdx.graphics.getWidth()/64;
- y = Gdx.graphics.getHeight()/1.029f;
- font = new BitmapFont(Gdx.files.internal("text/font.fnt"));
- font.getData().setScale((float)Gdx.graphics.getWidth()/1920, (float)Gdx.graphics.getHeight()/1080);
- font.setColor(Color.BLACK);
- count = 0;
- }
- public void renderCounter(SpriteBatch batch, Map MAP, Stephan STEPHAN){
- count = MAP.getCount();
- if(STEPHAN.getDeath()){
- x = Gdx.graphics.getWidth()/3.84f;
- y = Gdx.graphics.getHeight()/2;
- text = "Score: %s (Tab to restart)";
- }
- font.draw(batch, String.format(text, count), x, y);
- }
- }
- package com.mygdx.game;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.audio.Sound;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.Sprite;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Rectangle;
- public class Map {
- private boolean timerExists = false;
- private float one;
- private float two;
- private int pipeCount;
- private int count;
- private boolean move;
- private final int NUMBER_OF_PIPES = 10;
- private final int SPEED = 5;
- private final int DELAY = 0;
- private Timer timer;
- private TimerTask timerTask;
- private int x1;
- private int x2;
- private float[] y1;
- private float[] y2;
- private float width;
- private float height;
- private int xPipe1;
- private int xPipe2;
- private Texture background;
- private Sprite background2;
- private List<Sprite> ground;
- private List<Sprite> pipes1Sprite;
- private List<Sprite> pipes2Sprite;
- private List<Rectangle> pipes1SpriteRec;
- private List<Rectangle> pipes2SpriteRec;
- private Rectangle groundRec;
- private Rectangle stephanHawk;
- private Sound death;
- private Sound pipe;
- private boolean collision;
- public boolean getMove(){
- return move;
- }
- public int getCount(){
- return count;
- }
- private void collisionDetection(Stephan STEPHAN){
- if(!collision){
- if(stephanHawk.overlaps(groundRec)){
- collision(STEPHAN);
- STEPHAN.setCount1();
- }
- for(int i = 0; i<NUMBER_OF_PIPES; i++){
- if(stephanHawk.overlaps(pipes1SpriteRec.get(i))){
- collision(STEPHAN);
- STEPHAN.setCount1();
- }
- if(stephanHawk.overlaps(pipes2SpriteRec.get(i))){
- collision(STEPHAN);
- STEPHAN.setCount1();
- }
- }
- }
- }
- private void collision(Stephan STEPHAN){
- move = false;
- collision = true;
- STEPHAN.setHawking();
- death.play(0.5f);
- }
- public void createMap(Stephan STEPHAN){
- collision = false;
- pipe = Gdx.audio.newSound(Gdx.files.internal("pipe/pipe.wav"));
- death = Gdx.audio.newSound(Gdx.files.internal("bird/death.wav"));
- one = Gdx.graphics.getHeight()/1.8f;
- two = Gdx.graphics.getHeight()/1.35f;
- move = true;
- stephanHawk = STEPHAN.getStephan();
- count = 0;
- pipeCount = 0;
- x1 = -Gdx.graphics.getWidth()/96;
- x2 = x1+Gdx.graphics.getWidth();
- y1 = new float[NUMBER_OF_PIPES];
- y2 = new float[NUMBER_OF_PIPES];
- width = Gdx.graphics.getWidth()/7.356f;
- height = Gdx.graphics.getHeight()/1.0344f;
- background = new Texture("others/background.png");
- createPipes();
- createGround();
- if(!timerExists){
- timerExists = true;
- timer = new Timer();
- timerTask = new TimerTask(){
- public void run(){
- if(move){
- x1-=2;
- x2-=2;
- xPipe1-=2;
- xPipe2-=2;
- }
- }
- };
- timer = new Timer();
- timer.schedule(timerTask, DELAY, SPEED);
- }
- }
- public void renderMap(SpriteBatch batch, Stephan STEPHAN){
- if(STEPHAN.getDeath()){
- for(int i = 0; i<NUMBER_OF_PIPES; i++){
- pipes1Sprite.get(i).setTexture(new Texture(Gdx.files.internal("pipe/pipeB.png")));
- pipes2Sprite.get(i).setTexture(new Texture(Gdx.files.internal("pipe/pipe2B.png")));
- }
- for(int i = 0; i<2; i++){
- ground.get(i).setTexture(new Texture(Gdx.files.internal("others/ground2.png")));
- }
- background = new Texture(Gdx.files.internal("others/background2.png"));
- }
- stephanHawk = STEPHAN.getStephan();
- respawnPipes();
- respawnGround(batch);
- batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- batch.draw(ground.get(0), x1, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- batch.draw(ground.get(1), x2, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- for(int i = 0; i<NUMBER_OF_PIPES/2; i++){
- batch.draw(pipes1Sprite.get(i), (float) xPipe1+(i*Gdx.graphics.getWidth()/4.27f), y1[i], pipes1Sprite.get(i).getOriginX(), pipes1Sprite.get(i).getOriginY(), width,
- height, pipes1Sprite.get(i).getScaleX(), pipes1Sprite.get(i).getScaleY(), pipes1Sprite.get(i).getRotation());
- pipes1Sprite.get(i).setX((float) xPipe1+(i*Gdx.graphics.getWidth()/4.27f));
- batch.draw(pipes2Sprite.get(i), (float) xPipe1+(i*Gdx.graphics.getWidth()/4.27f), y2[i], pipes2Sprite.get(i).getOriginX(), pipes2Sprite.get(i).getOriginY(), width,
- height, pipes2Sprite.get(i).getScaleX(), pipes2Sprite.get(i).getScaleY(), pipes2Sprite.get(i).getRotation());
- pipes1Sprite.get(i).setX((float) xPipe1+(i*Gdx.graphics.getWidth()/4.27f));
- }
- for(int i = NUMBER_OF_PIPES/2; i<NUMBER_OF_PIPES; i++){
- batch.draw(pipes1Sprite.get(i), (float) xPipe2+((i-5)*Gdx.graphics.getWidth()/4.27f), y1[i], pipes1Sprite.get(i).getOriginX(), pipes1Sprite.get(i).getOriginY(), width,
- height, pipes1Sprite.get(i).getScaleX(), pipes1Sprite.get(i).getScaleY(), pipes1Sprite.get(i).getRotation());
- pipes1Sprite.get(i).setX((float) xPipe2+((i-5)*Gdx.graphics.getWidth()/4.27f));
- batch.draw(pipes2Sprite.get(i), (float) xPipe2+((i-5)*Gdx.graphics.getWidth()/4.27f), y2[i], pipes2Sprite.get(i).getOriginX(), pipes2Sprite.get(i).getOriginY(), width,
- height, pipes2Sprite.get(i).getScaleX(), pipes2Sprite.get(i).getScaleY(), pipes2Sprite.get(i).getRotation());
- pipes1Sprite.get(i).setX((float) xPipe2+((i-5)*Gdx.graphics.getWidth()/4.27f));
- }
- for(int i = 0; i<NUMBER_OF_PIPES; i++){
- if(pipes1Sprite.get(i).getX()>=Gdx.graphics.getWidth()/2.4 && pipes1Sprite.get(i).getX()<=Gdx.graphics.getWidth()/2.3){
- if(i==pipeCount){
- pipe.play(0.5f);
- count++;
- if(pipeCount==NUMBER_OF_PIPES-1){
- pipeCount = 0;
- }
- else{
- pipeCount++;
- }
- }
- }
- }
- collisionDetection(STEPHAN);
- }
- private void defineVPipes(){
- for(int i = 0; i<NUMBER_OF_PIPES; i++){
- if(i<5){
- pipes1SpriteRec.get(i).setX(xPipe1+(i*(Gdx.graphics.getWidth()/4.27f)));
- pipes1SpriteRec.get(i).setY(y1[i]);
- pipes1SpriteRec.get(i).setWidth(width);
- pipes1SpriteRec.get(i).setHeight(height);
- pipes2SpriteRec.get(i).setX(xPipe1+(i*(Gdx.graphics.getWidth()/4.27f)));
- pipes2SpriteRec.get(i).setY(y2[i]);
- pipes2SpriteRec.get(i).setWidth(width);
- pipes2SpriteRec.get(i).setHeight(height);
- }
- else{
- pipes1SpriteRec.get(i).setX(xPipe1+(i*(Gdx.graphics.getWidth()/4.27f)));
- pipes1SpriteRec.get(i).setY(y1[i]);
- pipes1SpriteRec.get(i).setWidth(width);
- pipes1SpriteRec.get(i).setHeight(height);
- pipes2SpriteRec.get(i).setX(xPipe1+(i*(Gdx.graphics.getWidth()/4.27f)));
- pipes2SpriteRec.get(i).setY(y2[i]);
- pipes2SpriteRec.get(i).setWidth(width);
- pipes2SpriteRec.get(i).setHeight(height);
- }
- }
- }
- private void defineVGround(){
- groundRec.setHeight(Gdx.graphics.getHeight()/5.5f);
- groundRec.setWidth(Gdx.graphics.getWidth());
- groundRec.setX(0);
- groundRec.setY(0);
- }
- private void createPipes(){
- pipes1Sprite = new ArrayList<Sprite>();
- pipes2Sprite = new ArrayList<Sprite>();
- pipes1SpriteRec = new ArrayList<Rectangle>();
- pipes2SpriteRec = new ArrayList<Rectangle>();
- float a = Gdx.graphics.getWidth()/4.27f;
- for(int i = 0; i<NUMBER_OF_PIPES; i++){
- int y = new Random().nextInt((int)a)+0;
- y -= Gdx.graphics.getHeight()/5.4f;
- y1[i] = y-one;
- y2[i] = y+two;
- pipes1Sprite.add(new Sprite(new Texture("pipe/pipe.png")));
- pipes2Sprite.add(new Sprite(new Texture("pipe/pipe2.png")));
- pipes1SpriteRec.add(new Rectangle());
- pipes2SpriteRec.add(new Rectangle());
- pipes1SpriteRec.get(i).setWidth(pipes1Sprite.get(i).getWidth());
- pipes1SpriteRec.get(i).setHeight(pipes1Sprite.get(i).getHeight());
- pipes2SpriteRec.get(i).setWidth(pipes2Sprite.get(i).getWidth());
- pipes2SpriteRec.get(i).setHeight(pipes2Sprite.get(i).getHeight());
- }
- xPipe1 = (int)(Gdx.graphics.getWidth()/0.96f);
- xPipe2 = xPipe1+(int)(Gdx.graphics.getWidth()/0.85f);
- defineVPipes();
- }
- private void createGround(){
- ground = new ArrayList<Sprite>();
- groundRec = new Rectangle();
- for(int i = 0; i<2; i++){
- ground.add(new Sprite(new Texture("others/ground.png")));
- }
- defineVGround();
- }
- private void respawnPipes(){
- float a = Gdx.graphics.getWidth()/4.27f;
- if(xPipe1<=-(int)(Gdx.graphics.getWidth()/0.87f)){
- xPipe1 = (int)(Gdx.graphics.getWidth()/0.834f);
- for(int i = 0; i<NUMBER_OF_PIPES/2; i++){
- int y = new Random().nextInt((int)a)+0;
- y -= Gdx.graphics.getHeight()/5.4f;
- y1[i] = y-one;
- y2[i] = y+two;
- }
- }
- if(xPipe2<=-(int)(Gdx.graphics.getWidth()/0.87f)){
- xPipe2 = (int)(Gdx.graphics.getWidth()/0.834f);
- for(int i = NUMBER_OF_PIPES/2; i<NUMBER_OF_PIPES; i++){
- int y = new Random().nextInt((int)a)+0;
- y -= Gdx.graphics.getHeight()/5.4f;
- y1[i] = y-one;
- y2[i] = y+two;
- }
- }
- defineVPipes();
- }
- private void respawnGround(SpriteBatch batch){
- if(x1<=-Gdx.graphics.getWidth()){
- x1+=Gdx.graphics.getWidth();
- x2+=Gdx.graphics.getWidth();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement