Advertisement
Guest User

Untitled

a guest
Nov 21st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 KB | None | 0 0
  1. package com.mygdx.game;
  2.  
  3. import com.badlogic.gdx.ApplicationAdapter;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.graphics.GL20;
  6. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  7.  
  8. public class MyGdxGame extends ApplicationAdapter {
  9. private Counter counter = new Counter();
  10. private Map map = new Map();
  11. private Player player = new Player();
  12. private SpriteBatch batch;
  13. public void create () {
  14. batch = new SpriteBatch();
  15. map.createMap();
  16. player.createPlayer();
  17. counter.createCounter();
  18. }
  19. public void render () {
  20. Gdx.gl.glClearColor(1, 1, 1, 1);
  21. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  22. batch.begin();
  23. map.renderMap(batch);
  24. player.renderPlayer(batch);
  25. counter.renderCounter(batch);
  26. batch.end();
  27. }
  28. }
  29.  
  30. package com.mygdx.game;
  31.  
  32. import com.badlogic.gdx.Gdx;
  33. import com.badlogic.gdx.Input.Keys;
  34. import com.badlogic.gdx.graphics.Texture;
  35. import com.badlogic.gdx.graphics.g2d.Sprite;
  36. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  37. import com.badlogic.gdx.math.Circle;
  38. import com.badlogic.gdx.math.Intersector;
  39. import com.badlogic.gdx.math.Rectangle;
  40.  
  41. public class Player {
  42. private Rectangle[] elements;
  43. private Map map;
  44. private static int speed = 5;
  45. public int getSpeed(){
  46. return speed;
  47. }
  48. public void setSpeed(){
  49. speed+=3;
  50. }
  51. private float width;
  52. private float height;
  53. private float x;
  54. private float y;
  55. private float radius;
  56. private Sprite player;
  57. private Circle playerShape;
  58. public void setElements(Rectangle[] elements){
  59. this.elements = elements;
  60. }
  61. public void createPlayer(){
  62. map = new Map();
  63. width = Gdx.graphics.getWidth()/19.2f;
  64. height = Gdx.graphics.getHeight()/10.8f;
  65. player = new Sprite(new Texture(Gdx.files.internal("player.png")));
  66. if(Gdx.graphics.getWidth()>Gdx.graphics.getHeight()){
  67. radius = width;
  68. }
  69. else{
  70. radius = height;
  71. }
  72. x = Gdx.graphics.getWidth()/2-radius/2;
  73. y = Gdx.graphics.getHeight()/2-radius/2;
  74. playerShape = new Circle();
  75. playerShape.setRadius(radius);
  76. playerShape.setPosition(x, y);
  77. }
  78. public void renderPlayer(SpriteBatch batch){
  79. batch.draw(player, x, y, player.getOriginX(), player.getOriginY(), width, height, player.getScaleX(), player.getScaleY(), player.getRotation());
  80. move();
  81. collisionDetection();
  82. }
  83. private void collisionDetection(){
  84. for(int i = 0; i<map.getNumber(); i++){
  85. if(Intersector.overlaps(playerShape, elements[i])){
  86. //Kollision
  87. }
  88. }
  89. }
  90. private void move(){
  91. if(Gdx.input.isKeyPressed(Keys.A)){
  92. x-=speed;
  93. }
  94. if(Gdx.input.isKeyPressed(Keys.D)){
  95. x+=speed;
  96. }
  97. }
  98. }
  99.  
  100.  
  101. package com.mygdx.game;
  102.  
  103. import java.util.Random;
  104. import java.util.Timer;
  105. import java.util.TimerTask;
  106.  
  107. import com.badlogic.gdx.Gdx;
  108. import com.badlogic.gdx.graphics.Texture;
  109. import com.badlogic.gdx.graphics.g2d.Sprite;
  110. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  111. import com.badlogic.gdx.math.Rectangle;
  112.  
  113. public class Map {
  114. private int z;
  115. private Random r;
  116. private float width;
  117. private float height;
  118. private final int numberOfElements = 20;
  119. private Player player;
  120. private Timer timer;
  121. private TimerTask timerTask;
  122. private int y[];
  123. private int x[];
  124. private Sprite[] elements;
  125. private Rectangle[] elementsShape;
  126. public void createMap(){
  127. z = 0;
  128. r = new Random();
  129. width = Gdx.graphics.getWidth()/7.68f;
  130. height = Gdx.graphics.getHeight()/4.32f;
  131. player = new Player();
  132. elements = new Sprite[numberOfElements];
  133. elementsShape = new Rectangle[numberOfElements];
  134. y = new int[numberOfElements];
  135. x = new int[numberOfElements];
  136. timer = new Timer();
  137. timerTask = new TimerTask(){
  138. public void run(){
  139. for(int i = 0; i<numberOfElements; i++){
  140. y[i]-=(player.getSpeed()-3);
  141. }
  142. }
  143. };
  144. timer.schedule(timerTask, 0, 10);
  145. spawnElements();
  146. }
  147. private void spawnElements(){
  148. for(int i = 0; i<numberOfElements; i++){
  149. Sprite sprite = new Sprite(new Texture(Gdx.files.internal("block.png")));
  150. y[i] = i*(Gdx.graphics.getHeight()/2)+(Gdx.graphics.getHeight()+Gdx.graphics.getHeight()/2);
  151. int a = (int) (Gdx.graphics.getWidth()-(width+30));
  152. int b = 30;
  153. x[i] = r.nextInt(a)+b;
  154. elements[i] = sprite;
  155. elementsShape[i] = new Rectangle();
  156. elementsShape[i].setSize(elements[i].getWidth(), elements[i].getHeight());
  157. elementsShape[i].setPosition(x[i], y[i]);
  158. }
  159. }
  160. public void renderMap(SpriteBatch batch){
  161. player.setElements(elementsShape);
  162. for(int i = 0; i<numberOfElements; i++){
  163. batch.draw(elements[i], x[i], y[i], elements[i].getOriginX(), elements[i].getOriginY(), width,
  164. height, elements[i].getScaleX(), elements[i].getScaleY(), elements[i].getRotation());
  165. counting();
  166. }
  167. }
  168. private void counting(){
  169. for(int i = z; i<numberOfElements; i++){
  170. if(y[i]<=Gdx.graphics.getHeight()/2-height/2){
  171. z++;
  172. //Score up
  173. if(z==numberOfElements-1){
  174. z = 0;
  175. player.setSpeed();
  176. spawnElements();
  177. }
  178. }
  179. }
  180. }
  181. public int getNumber(){
  182. return numberOfElements;
  183. }
  184. }
  185.  
  186.  
  187.  
  188. package com.mygdx.game;
  189.  
  190. import com.badlogic.gdx.Gdx;
  191. import com.badlogic.gdx.graphics.Color;
  192. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  193. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  194.  
  195. public class Counter {
  196. private float x;
  197. private float y;
  198. private BitmapFont font;
  199. private int levelCounter;
  200. private int scoreCounter;
  201. private String text;
  202. public void createCounter(){
  203. text = "Level: %s\nScore: %s";
  204. x = 10;
  205. y = 80;
  206. font = new BitmapFont(Gdx.files.internal("font.fnt"));
  207. font.setColor(Color.BLUE);
  208. font.getData().setScale((float)Gdx.graphics.getHeight()/2160, (float)Gdx.graphics.getWidth()/3840);
  209. levelCounter = 1;
  210. scoreCounter = 0;
  211. }
  212. public void renderCounter(SpriteBatch batch){
  213. font.draw(batch, String.format(text, levelCounter, scoreCounter), x, y);
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement