Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.57 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3. import android.os.Bundle;
  4.  
  5. import com.badlogic.gdx.backends.android.AndroidApplication;
  6. import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
  7.  
  8. public class AndroidLauncher extends AndroidApplication {
  9. protected void onCreate (Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
  12. initialize(new FlappyCoal(), config);
  13. }
  14. }
  15.  
  16.  
  17. package com.mygdx.game;
  18.  
  19. import com.badlogic.gdx.ApplicationAdapter;
  20. import com.badlogic.gdx.Gdx;
  21. import com.badlogic.gdx.graphics.GL20;
  22. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  23.  
  24. public class FlappyCoal extends ApplicationAdapter {
  25. private Map MAP;
  26. private Stephan STEPHAN;
  27. private Counter COUNTER;
  28. private SpriteBatch batch;
  29. public void create () {
  30. creating();
  31. }
  32. public void render () {
  33. Gdx.gl.glClearColor(0, 0, 0, 0);
  34. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  35. batch.begin();
  36. MAP.renderMap(batch, STEPHAN);
  37. STEPHAN.move(MAP);
  38. COUNTER.renderCounter(batch, MAP, STEPHAN);
  39. STEPHAN.renderStephan(batch, MAP, STEPHAN);
  40. try{
  41. batch.end();
  42. }
  43. catch(Exception e){}
  44. }
  45. public void creating () {
  46. MAP = new Map();
  47. STEPHAN = new Stephan(this);
  48. COUNTER = new Counter();
  49. batch = new SpriteBatch();
  50. COUNTER.createCounter();
  51. STEPHAN.createStephan();
  52. MAP.createMap(STEPHAN);
  53. }
  54. }
  55.  
  56.  
  57.  
  58. package com.mygdx.game;
  59.  
  60. import java.util.Timer;
  61. import java.util.TimerTask;
  62.  
  63. import com.badlogic.gdx.Gdx;
  64. import com.badlogic.gdx.Input.Keys;
  65. import com.badlogic.gdx.audio.Sound;
  66. import com.badlogic.gdx.graphics.Texture;
  67. import com.badlogic.gdx.graphics.g2d.Sprite;
  68. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  69. import com.badlogic.gdx.math.Rectangle;
  70.  
  71. public class Stephan {
  72. private boolean timerExists = false;
  73. private boolean death;
  74. private final int DELAY = 0;
  75. private final int SPEED = 5;
  76. private float count;
  77. private Timer moving;
  78. private TimerTask moving2;
  79. private Texture stephanHawking_alive = new Texture("bird/bird_alive.png");
  80. private Texture stephanHawking_dead = new Texture("bird/bird_dead.png");
  81. private Sprite stephanHawking2;
  82. private float x;
  83. private float y;
  84. private float[] y2;
  85. private Rectangle stephanHawk;
  86. private Sound fly;
  87. private final FlappyCoal myGdxGame;
  88. public Stephan(FlappyCoal myGdxGame){
  89. this.myGdxGame = myGdxGame;
  90. }
  91. public void createStephan(){
  92. y2 = new float[]{
  93. Gdx.graphics.getHeight()/1080, Gdx.graphics.getHeight()/18, Gdx.graphics.getHeight()/17.7f, Gdx.graphics.getHeight()/14.4f, Gdx.graphics.getHeight()/14.2f,
  94. 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,
  95. 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,
  96. 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,
  97. Gdx.graphics.getHeight()/8.25f, Gdx.graphics.getHeight()/6.75f, Gdx.graphics.getHeight()/6.71f
  98. };
  99. death = false;
  100. count = 0;
  101. fly = Gdx.audio.newSound(Gdx.files.internal("bird/fly.wav"));
  102. if(!timerExists){
  103. timerExists = true;
  104. moving = new Timer();
  105. moving2 = new TimerTask(){
  106. public void run(){
  107. if(count>0){
  108. count++;
  109. }
  110. if(count>=y2[0] && count<=y2[1]){
  111. y+=3;
  112. }
  113. if(count>=y2[2] && count<=y2[3]){
  114. y+=1;
  115. }
  116. if(count>=y2[4] && count<=y2[5]){
  117. y+=0.5f;
  118. }
  119. if(count>=y2[6] && count<=y2[7]){
  120. y+=0.3f;
  121. }
  122. if(count>=y2[8] && count<=y2[9]){
  123. y-=0.1f;
  124. }
  125. if(count>=y2[10] && count<=y2[11]){
  126. y-=0.3f;
  127. }
  128. if(count>=y2[12] && count<=y2[13]){
  129. y-=0.5f;
  130. }
  131. if(count>=y2[14] && count<=y2[15]){
  132. y-=1;
  133. }
  134. if(count>=y2[16] && count<=y2[17]){
  135. y-=2;
  136. }
  137. if(count>=y2[18] && count<=y2[19]){
  138. y-=3;
  139. }
  140. if(count>=y2[20] && count<=y2[21]){
  141. y-=3.5f;
  142. }
  143. if(count>=y2[22]) {
  144. y -= 4;
  145. }
  146. }
  147. };
  148. moving.schedule(moving2, DELAY, SPEED);
  149. }
  150. stephanHawking2 = new Sprite(stephanHawking_alive);
  151. y = Gdx.graphics.getHeight()/2-Gdx.graphics.getHeight()/13.5f/2;
  152. x = Gdx.graphics.getWidth()/2-Gdx.graphics.getWidth()/24/2;
  153. createRec();
  154. }
  155. public void renderStephan(SpriteBatch batch, Map MAP, Stephan STEPHAN){
  156. System.out.println(count);
  157. batch.draw(stephanHawking2, x, y, stephanHawking2.getOriginX(), stephanHawking2.getOriginY(), Gdx.graphics.getWidth()/24,
  158. Gdx.graphics.getHeight()/13.5f, stephanHawking2.getScaleX(), stephanHawking2.getScaleY(), stephanHawking2.getRotation());
  159. stephanHawk.setX(x);
  160. stephanHawk.setY(y);
  161. if(y<=-Gdx.graphics.getHeight()/7.2f){
  162. death = true;
  163. y = -Gdx.graphics.getHeight()/7.2f;
  164. if(Gdx.input.justTouched()){
  165. myGdxGame.creating();
  166. }
  167. }
  168. }
  169. public void setCount1(){
  170. count = 1;
  171. }
  172. public boolean getDeath(){
  173. return death;
  174. }
  175. public void move(Map MAP){
  176. if(MAP.getMove()){
  177. if(Gdx.input.justTouched()){
  178. if(y<=Gdx.graphics.getHeight()){
  179. count = Gdx.graphics.getHeight()/1080;
  180. fly.play(0.5f);
  181. }
  182. }
  183. }
  184. }
  185. public void setHawking(){
  186. stephanHawking2.setTexture(stephanHawking_dead);
  187. }
  188. private void createRec(){
  189. stephanHawk = new Rectangle();
  190. stephanHawk.setX(x);
  191. stephanHawk.setY(y);
  192. stephanHawk.setHeight(Gdx.graphics.getHeight()/13.5f);
  193. stephanHawk.setWidth(Gdx.graphics.getWidth()/24);
  194. }
  195. public Rectangle getStephan(){
  196. return stephanHawk;
  197. }
  198.  
  199. }
  200.  
  201.  
  202. package com.mygdx.game;
  203.  
  204. import com.badlogic.gdx.Gdx;
  205. import com.badlogic.gdx.graphics.Color;
  206. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  207. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  208.  
  209. public class Counter {
  210. private String text;
  211. private BitmapFont font;
  212. private int count;
  213. private float x;
  214. private float y;
  215. public void createCounter(){
  216. text = "Score: %s";
  217. x = Gdx.graphics.getWidth()/64;
  218. y = Gdx.graphics.getHeight()/1.029f;
  219. font = new BitmapFont(Gdx.files.internal("text/font.fnt"));
  220. font.getData().setScale((float)Gdx.graphics.getWidth()/1920, (float)Gdx.graphics.getHeight()/1080);
  221. font.setColor(Color.BLACK);
  222. count = 0;
  223. }
  224. public void renderCounter(SpriteBatch batch, Map MAP, Stephan STEPHAN){
  225. count = MAP.getCount();
  226. if(STEPHAN.getDeath()){
  227. x = Gdx.graphics.getWidth()/3.84f;
  228. y = Gdx.graphics.getHeight()/2;
  229. text = "Score: %s (Tab to restart)";
  230. }
  231. font.draw(batch, String.format(text, count), x, y);
  232. }
  233. }
  234.  
  235.  
  236.  
  237. package com.mygdx.game;
  238.  
  239. import java.util.ArrayList;
  240. import java.util.List;
  241. import java.util.Random;
  242. import java.util.Timer;
  243. import java.util.TimerTask;
  244.  
  245. import com.badlogic.gdx.Gdx;
  246. import com.badlogic.gdx.audio.Sound;
  247. import com.badlogic.gdx.graphics.Texture;
  248. import com.badlogic.gdx.graphics.g2d.Sprite;
  249. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  250. import com.badlogic.gdx.math.Rectangle;
  251.  
  252. public class Map {
  253. private boolean timerExists = false;
  254. private float one;
  255. private float two;
  256. private int pipeCount;
  257. private int count;
  258. private boolean move;
  259. private final int NUMBER_OF_PIPES = 10;
  260. private final int SPEED = 5;
  261. private final int DELAY = 0;
  262. private Timer timer;
  263. private TimerTask timerTask;
  264. private int x1;
  265. private int x2;
  266. private float[] y1;
  267. private float[] y2;
  268. private float width;
  269. private float height;
  270. private int xPipe1;
  271. private int xPipe2;
  272. private Texture background;
  273. private Sprite background2;
  274. private List<Sprite> ground;
  275. private List<Sprite> pipes1Sprite;
  276. private List<Sprite> pipes2Sprite;
  277. private List<Rectangle> pipes1SpriteRec;
  278. private List<Rectangle> pipes2SpriteRec;
  279. private Rectangle groundRec;
  280. private Rectangle stephanHawk;
  281. private Sound death;
  282. private Sound pipe;
  283. private boolean collision;
  284. public boolean getMove(){
  285. return move;
  286. }
  287. public int getCount(){
  288. return count;
  289. }
  290. private void collisionDetection(Stephan STEPHAN){
  291. if(!collision){
  292. if(stephanHawk.overlaps(groundRec)){
  293. collision(STEPHAN);
  294. STEPHAN.setCount1();
  295. }
  296. for(int i = 0; i<NUMBER_OF_PIPES; i++){
  297. if(stephanHawk.overlaps(pipes1SpriteRec.get(i))){
  298. collision(STEPHAN);
  299. STEPHAN.setCount1();
  300. }
  301. if(stephanHawk.overlaps(pipes2SpriteRec.get(i))){
  302. collision(STEPHAN);
  303. STEPHAN.setCount1();
  304. }
  305. }
  306. }
  307. }
  308. private void collision(Stephan STEPHAN){
  309. move = false;
  310. collision = true;
  311. STEPHAN.setHawking();
  312. death.play(0.5f);
  313. }
  314. public void createMap(Stephan STEPHAN){
  315. collision = false;
  316. pipe = Gdx.audio.newSound(Gdx.files.internal("pipe/pipe.wav"));
  317. death = Gdx.audio.newSound(Gdx.files.internal("bird/death.wav"));
  318. one = Gdx.graphics.getHeight()/1.8f;
  319. two = Gdx.graphics.getHeight()/1.35f;
  320. move = true;
  321. stephanHawk = STEPHAN.getStephan();
  322. count = 0;
  323. pipeCount = 0;
  324. x1 = -Gdx.graphics.getWidth()/96;
  325. x2 = x1+Gdx.graphics.getWidth();
  326. y1 = new float[NUMBER_OF_PIPES];
  327. y2 = new float[NUMBER_OF_PIPES];
  328. width = Gdx.graphics.getWidth()/7.356f;
  329. height = Gdx.graphics.getHeight()/1.0344f;
  330. background = new Texture("others/background.png");
  331. createPipes();
  332. createGround();
  333. if(!timerExists){
  334. timerExists = true;
  335. timer = new Timer();
  336. timerTask = new TimerTask(){
  337. public void run(){
  338. if(move){
  339. x1-=2;
  340. x2-=2;
  341. xPipe1-=2;
  342. xPipe2-=2;
  343. }
  344. }
  345. };
  346. timer = new Timer();
  347. timer.schedule(timerTask, DELAY, SPEED);
  348. }
  349. }
  350. public void renderMap(SpriteBatch batch, Stephan STEPHAN){
  351. if(STEPHAN.getDeath()){
  352. for(int i = 0; i<NUMBER_OF_PIPES; i++){
  353. pipes1Sprite.get(i).setTexture(new Texture(Gdx.files.internal("pipe/pipeB.png")));
  354. pipes2Sprite.get(i).setTexture(new Texture(Gdx.files.internal("pipe/pipe2B.png")));
  355. }
  356. for(int i = 0; i<2; i++){
  357. ground.get(i).setTexture(new Texture(Gdx.files.internal("others/ground2.png")));
  358. }
  359. background = new Texture(Gdx.files.internal("others/background2.png"));
  360. }
  361. stephanHawk = STEPHAN.getStephan();
  362. respawnPipes();
  363. respawnGround(batch);
  364. batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  365. batch.draw(ground.get(0), x1, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  366. batch.draw(ground.get(1), x2, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  367. for(int i = 0; i<NUMBER_OF_PIPES/2; i++){
  368. batch.draw(pipes1Sprite.get(i), (float) xPipe1+(i*Gdx.graphics.getWidth()/4.27f), y1[i], pipes1Sprite.get(i).getOriginX(), pipes1Sprite.get(i).getOriginY(), width,
  369. height, pipes1Sprite.get(i).getScaleX(), pipes1Sprite.get(i).getScaleY(), pipes1Sprite.get(i).getRotation());
  370. pipes1Sprite.get(i).setX((float) xPipe1+(i*Gdx.graphics.getWidth()/4.27f));
  371. batch.draw(pipes2Sprite.get(i), (float) xPipe1+(i*Gdx.graphics.getWidth()/4.27f), y2[i], pipes2Sprite.get(i).getOriginX(), pipes2Sprite.get(i).getOriginY(), width,
  372. height, pipes2Sprite.get(i).getScaleX(), pipes2Sprite.get(i).getScaleY(), pipes2Sprite.get(i).getRotation());
  373. pipes1Sprite.get(i).setX((float) xPipe1+(i*Gdx.graphics.getWidth()/4.27f));
  374. }
  375. for(int i = NUMBER_OF_PIPES/2; i<NUMBER_OF_PIPES; i++){
  376. 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,
  377. height, pipes1Sprite.get(i).getScaleX(), pipes1Sprite.get(i).getScaleY(), pipes1Sprite.get(i).getRotation());
  378. pipes1Sprite.get(i).setX((float) xPipe2+((i-5)*Gdx.graphics.getWidth()/4.27f));
  379. 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,
  380. height, pipes2Sprite.get(i).getScaleX(), pipes2Sprite.get(i).getScaleY(), pipes2Sprite.get(i).getRotation());
  381. pipes1Sprite.get(i).setX((float) xPipe2+((i-5)*Gdx.graphics.getWidth()/4.27f));
  382. }
  383. for(int i = 0; i<NUMBER_OF_PIPES; i++){
  384. if(pipes1Sprite.get(i).getX()>=Gdx.graphics.getWidth()/2.4 && pipes1Sprite.get(i).getX()<=Gdx.graphics.getWidth()/2.3){
  385. if(i==pipeCount){
  386. pipe.play(0.5f);
  387. count++;
  388. if(pipeCount==NUMBER_OF_PIPES-1){
  389. pipeCount = 0;
  390. }
  391. else{
  392. pipeCount++;
  393. }
  394. }
  395. }
  396. }
  397. collisionDetection(STEPHAN);
  398. }
  399. private void defineVPipes(){
  400. for(int i = 0; i<NUMBER_OF_PIPES; i++){
  401. if(i<5){
  402. pipes1SpriteRec.get(i).setX(xPipe1+(i*(Gdx.graphics.getWidth()/4.27f)));
  403. pipes1SpriteRec.get(i).setY(y1[i]);
  404. pipes1SpriteRec.get(i).setWidth(width);
  405. pipes1SpriteRec.get(i).setHeight(height);
  406. pipes2SpriteRec.get(i).setX(xPipe1+(i*(Gdx.graphics.getWidth()/4.27f)));
  407. pipes2SpriteRec.get(i).setY(y2[i]);
  408. pipes2SpriteRec.get(i).setWidth(width);
  409. pipes2SpriteRec.get(i).setHeight(height);
  410. }
  411. else{
  412. pipes1SpriteRec.get(i).setX(xPipe1+(i*(Gdx.graphics.getWidth()/4.27f)));
  413. pipes1SpriteRec.get(i).setY(y1[i]);
  414. pipes1SpriteRec.get(i).setWidth(width);
  415. pipes1SpriteRec.get(i).setHeight(height);
  416. pipes2SpriteRec.get(i).setX(xPipe1+(i*(Gdx.graphics.getWidth()/4.27f)));
  417. pipes2SpriteRec.get(i).setY(y2[i]);
  418. pipes2SpriteRec.get(i).setWidth(width);
  419. pipes2SpriteRec.get(i).setHeight(height);
  420. }
  421. }
  422. }
  423. private void defineVGround(){
  424. groundRec.setHeight(Gdx.graphics.getHeight()/5.5f);
  425. groundRec.setWidth(Gdx.graphics.getWidth());
  426. groundRec.setX(0);
  427. groundRec.setY(0);
  428. }
  429. private void createPipes(){
  430. pipes1Sprite = new ArrayList<Sprite>();
  431. pipes2Sprite = new ArrayList<Sprite>();
  432. pipes1SpriteRec = new ArrayList<Rectangle>();
  433. pipes2SpriteRec = new ArrayList<Rectangle>();
  434. float a = Gdx.graphics.getWidth()/4.27f;
  435. for(int i = 0; i<NUMBER_OF_PIPES; i++){
  436. int y = new Random().nextInt((int)a)+0;
  437. y -= Gdx.graphics.getHeight()/5.4f;
  438. y1[i] = y-one;
  439. y2[i] = y+two;
  440. pipes1Sprite.add(new Sprite(new Texture("pipe/pipe.png")));
  441. pipes2Sprite.add(new Sprite(new Texture("pipe/pipe2.png")));
  442. pipes1SpriteRec.add(new Rectangle());
  443. pipes2SpriteRec.add(new Rectangle());
  444. pipes1SpriteRec.get(i).setWidth(pipes1Sprite.get(i).getWidth());
  445. pipes1SpriteRec.get(i).setHeight(pipes1Sprite.get(i).getHeight());
  446. pipes2SpriteRec.get(i).setWidth(pipes2Sprite.get(i).getWidth());
  447. pipes2SpriteRec.get(i).setHeight(pipes2Sprite.get(i).getHeight());
  448. }
  449. xPipe1 = (int)(Gdx.graphics.getWidth()/0.96f);
  450. xPipe2 = xPipe1+(int)(Gdx.graphics.getWidth()/0.85f);
  451. defineVPipes();
  452. }
  453. private void createGround(){
  454. ground = new ArrayList<Sprite>();
  455. groundRec = new Rectangle();
  456. for(int i = 0; i<2; i++){
  457. ground.add(new Sprite(new Texture("others/ground.png")));
  458. }
  459. defineVGround();
  460. }
  461. private void respawnPipes(){
  462. float a = Gdx.graphics.getWidth()/4.27f;
  463. if(xPipe1<=-(int)(Gdx.graphics.getWidth()/0.87f)){
  464. xPipe1 = (int)(Gdx.graphics.getWidth()/0.834f);
  465. for(int i = 0; i<NUMBER_OF_PIPES/2; i++){
  466. int y = new Random().nextInt((int)a)+0;
  467. y -= Gdx.graphics.getHeight()/5.4f;
  468. y1[i] = y-one;
  469. y2[i] = y+two;
  470. }
  471. }
  472. if(xPipe2<=-(int)(Gdx.graphics.getWidth()/0.87f)){
  473. xPipe2 = (int)(Gdx.graphics.getWidth()/0.834f);
  474. for(int i = NUMBER_OF_PIPES/2; i<NUMBER_OF_PIPES; i++){
  475. int y = new Random().nextInt((int)a)+0;
  476. y -= Gdx.graphics.getHeight()/5.4f;
  477. y1[i] = y-one;
  478. y2[i] = y+two;
  479. }
  480. }
  481. defineVPipes();
  482. }
  483. private void respawnGround(SpriteBatch batch){
  484. if(x1<=-Gdx.graphics.getWidth()){
  485. x1+=Gdx.graphics.getWidth();
  486. x2+=Gdx.graphics.getWidth();
  487. }
  488. }
  489. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement