Advertisement
Guest User

need

a guest
Mar 6th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1.  
  2. import java.util.*;
  3. import com.googlecode.lanterna.*;
  4. import com.googlecode.lanterna.terminal.*;
  5. import com.googlecode.lanterna.input.*;
  6.  
  7. public class Snake {
  8. private Terminal term;
  9. private int cursor_x = 10, cursor_y = 10;
  10. ArrayList<Position> sis = new ArrayList<Position>();
  11. char snake[] = { '*', '*', '*' };
  12.  
  13. public Snake() {
  14. term = TerminalFacade.createTerminal();
  15. term.enterPrivateMode();
  16. int valor = 2;
  17. while (true) {
  18. Key k = term.readInput();
  19. if (k != null) {
  20. switch (k.getKind()) {
  21. case Escape:
  22. term.exitPrivateMode();
  23. return;
  24. case ArrowLeft:
  25. cursor_x -= 1;
  26. valor = 1;
  27. break;
  28. case ArrowRight:
  29. cursor_x += 1;
  30. valor = 2;
  31. break;
  32. case ArrowDown:
  33. cursor_y += 1;
  34. valor = 3;
  35. break;
  36. case ArrowUp:
  37. cursor_y -= 1;
  38. valor = 4;
  39. break;
  40. //default: valor=5;
  41. }
  42. }
  43.  
  44. term.clearScreen();
  45.  
  46.  
  47. for (int i = snake.length-1; i > 0; i--) {
  48. sis.add(i, sis.get(i - 1));
  49. }
  50. Position x = new Position(cursor_x, cursor_y);
  51. sis.add(0, x);
  52. term.applyForegroundColor(Terminal.Color.RED);
  53. /*if (valor == 2) {
  54. cursor_x += 1;
  55. }
  56.  
  57. else if (valor == 1)
  58. cursor_x -= 1;
  59. else if (valor == 3)
  60. cursor_y += 1;
  61. else
  62. cursor_y -= 1;*/
  63. for(int i=0;i<snake.length;i++) {
  64. show(snake, sis.get(i).point_x, sis.get(i).point_y,i);
  65. term.flush();
  66.  
  67. try {
  68. Thread.sleep(30);
  69. } catch (InterruptedException ie) {
  70. ie.printStackTrace();
  71. }
  72. }}
  73. }
  74.  
  75. private void show(char[] str, int x, int y,int l) {
  76. term.moveCursor(x, y);
  77.  
  78. int len = str.length;
  79.  
  80. //for (int i = 0; i < len; i++) {
  81. term.putCharacter(str[l]);
  82. //}
  83.  
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement