Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
  2.  
  3. Button arriba, abajo, derecha, izquierda;
  4. private Juego juego;
  5. private Bitmap personaje;
  6. private Sprite sprite;
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12.  
  13. juego = (Juego) findViewById(R.id.customView);
  14. personaje = BitmapFactory.decodeResource(getResources(), R.drawable.bad1);
  15. sprite = new Sprite(juego, personaje);
  16.  
  17. arriba = (Button)findViewById(R.id.arriba);
  18. abajo = (Button)findViewById(R.id.abajo);
  19. derecha = (Button)findViewById(R.id.derecha);
  20. izquierda = (Button)findViewById(R.id.izquierda);
  21.  
  22. arriba.setOnClickListener(this);
  23. arriba.setOnLongClickListener(this);
  24.  
  25. abajo.setOnClickListener(this);
  26. abajo.setOnLongClickListener(this);
  27.  
  28. derecha.setOnClickListener(this);
  29. derecha.setOnLongClickListener(this);
  30.  
  31. izquierda.setOnClickListener(this);
  32. izquierda.setOnLongClickListener(this);
  33. }
  34.  
  35.  
  36. @Override
  37. public void onClick(View v) {
  38. juego.parar();
  39. }
  40.  
  41. @Override
  42. public boolean onLongClick(View v) {
  43. juego.mover();
  44. switch (v.getId()){
  45.  
  46. case R.id.arriba:
  47. juego.arriba();
  48. break;
  49. case R.id.abajo:
  50. juego.abajo();
  51. break;
  52. case R.id.derecha:
  53. juego.derecha();
  54. break;
  55. case R.id.izquierda:
  56. juego.izquierda();
  57. break;
  58. }
  59. return false;
  60. }
  61. }
  62.  
  63. public class Juego extends SurfaceView{
  64.  
  65. private Bitmap personaje;
  66. private Sprite sprite;
  67.  
  68. public Juego(Context context, AttributeSet attrs) {
  69. super(context, attrs);
  70.  
  71. personaje = BitmapFactory.decodeResource(getResources(), R.drawable.bad1);
  72. sprite = new Sprite(this, personaje);
  73. }
  74.  
  75. public void mover(){
  76. sprite.mover();
  77. invalidate();
  78. }
  79.  
  80. public void parar(){
  81. sprite.parar();
  82. invalidate();
  83. }
  84.  
  85. public void arriba(){
  86. sprite.dir= 3;
  87. invalidate();
  88. }
  89.  
  90. public void abajo(){
  91. sprite.dir= 0;
  92. invalidate();
  93. }
  94.  
  95. public void derecha(){
  96. sprite.dir= 2;
  97. invalidate();
  98. }
  99.  
  100. public void izquierda(){
  101. sprite.dir= 1;
  102. invalidate();
  103. }
  104.  
  105. @Override
  106. protected void onDraw(Canvas canvas) {
  107. sprite.onDraw(canvas);
  108. invalidate();
  109. }
  110. }
  111.  
  112. private static final int Horizontal = 4;
  113. private static final int Vertical = 3;
  114. private int x = 0;
  115. private int y = 0;
  116. private Juego juego;
  117. private Bitmap personaje;
  118. private int currentFrame = 0;
  119. private int ancho;
  120. private int alto;
  121. int dir = 2;
  122. private int velocidadx = 0;
  123. private int velocidady = 0;
  124.  
  125. long fps;
  126. private long timeThisFrame;
  127. private long lastFrameChangeTime = 0;
  128. private int frameLengthInMilliseconds = 100;
  129. int cantFrames = 3;
  130. boolean playing = false;
  131.  
  132. public Sprite(Juego juego, Bitmap personaje){
  133. this.juego = juego;
  134. this.personaje = personaje;
  135. this.ancho = personaje.getWidth() / Vertical;
  136. this.alto = personaje.getHeight() / Horizontal;
  137. }
  138.  
  139. public void mover(){
  140. playing = true;
  141. }
  142. public void parar(){
  143. playing = false;
  144. }
  145.  
  146. private void update() {
  147.  
  148. if (playing) {
  149.  
  150. long startFrame = System.currentTimeMillis();
  151.  
  152. if (startFrame > lastFrameChangeTime + frameLengthInMilliseconds) {
  153. lastFrameChangeTime = startFrame;
  154. currentFrame++;
  155. if (currentFrame >= cantFrames) {
  156.  
  157. currentFrame = 0;
  158. }
  159. }
  160.  
  161. long startFrameTime = System.currentTimeMillis();
  162.  
  163. timeThisFrame = System.currentTimeMillis() - startFrameTime;
  164. if (timeThisFrame >= 1) {
  165. fps = 1000 / timeThisFrame;
  166. }
  167.  
  168. if (dir== 0 && y < juego.getHeight() - alto - velocidady) {
  169. velocidadx = 0;
  170. velocidady = 5;
  171. }
  172. if (dir== 1 && x > ancho){
  173. velocidadx = -5;
  174. velocidady = 0;
  175. }
  176. if (dir== 2 && x < juego.getWidth() - velocidadx){
  177. velocidadx = 5;
  178. velocidady = 0;
  179. }
  180. if (dir== 3 && y > alto){
  181. velocidadx = 0;
  182. velocidady = -5;
  183. }
  184.  
  185. x = x + velocidadx;
  186. y = y + velocidady;
  187. }
  188. }
  189.  
  190. public void onDraw(Canvas canvas) {
  191. update();
  192. int srcX = currentFrame * ancho;
  193. int srcY = alto * dir;
  194. Rect src = new Rect(srcX, srcY, srcX + ancho, srcY + alto);
  195. Rect dst = new Rect(x, y, x + ancho, y + alto);
  196. canvas.drawBitmap(personaje, src, dst, null);
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement