Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import com.googlecode.lanterna.*;
- import com.googlecode.lanterna.terminal.*;
- import com.googlecode.lanterna.input.*;
- public class Snake {
- private Terminal term;
- private int cursor_x = 10, cursor_y = 10;
- ArrayList<Position> sis = new ArrayList<Position>();
- char snake[] = { '*', '*', '*' };
- public Snake() {
- term = TerminalFacade.createTerminal();
- term.enterPrivateMode();
- int valor = 2;
- while (true) {
- Key k = term.readInput();
- if (k != null) {
- switch (k.getKind()) {
- case Escape:
- term.exitPrivateMode();
- return;
- case ArrowLeft:
- cursor_x -= 1;
- valor = 1;
- break;
- case ArrowRight:
- cursor_x += 1;
- valor = 2;
- break;
- case ArrowDown:
- cursor_y += 1;
- valor = 3;
- break;
- case ArrowUp:
- cursor_y -= 1;
- valor = 4;
- break;
- //default: valor=5;
- }
- }
- term.clearScreen();
- for (int i = snake.length-1; i > 0; i--) {
- sis.add(i, sis.get(i - 1));
- }
- Position x = new Position(cursor_x, cursor_y);
- sis.add(0, x);
- term.applyForegroundColor(Terminal.Color.RED);
- /*if (valor == 2) {
- cursor_x += 1;
- }
- else if (valor == 1)
- cursor_x -= 1;
- else if (valor == 3)
- cursor_y += 1;
- else
- cursor_y -= 1;*/
- for(int i=0;i<snake.length;i++) {
- show(snake, sis.get(i).point_x, sis.get(i).point_y,i);
- term.flush();
- try {
- Thread.sleep(30);
- } catch (InterruptedException ie) {
- ie.printStackTrace();
- }
- }}
- }
- private void show(char[] str, int x, int y,int l) {
- term.moveCursor(x, y);
- int len = str.length;
- //for (int i = 0; i < len; i++) {
- term.putCharacter(str[l]);
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement