Advertisement
Guest User

Untitled

a guest
Nov 28th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. public class Game {
  2.  
  3. public static final int boardWidth = 20;
  4. public static final int squareSize = 30; //in pixels
  5.  
  6. GameObject[][] gameGrid;
  7. GameObject head;
  8. DIR direction;
  9.  
  10. ArrayList<GameObject> hadica;
  11.  
  12.  
  13. public enum DIR {
  14. UP,
  15. DOWN,
  16. LEFT,
  17. RIGHT;
  18. }
  19.  
  20.  
  21. Game() {
  22. Platno platno = Platno.dajPlatno("Snake", boardWidth * squareSize, boardWidth * squareSize, Color.WHITE);
  23. this.hadica = new ArrayList<GameObject>();
  24. this.direction = DIR.RIGHT;
  25.  
  26.  
  27. for (int i = 0; i < boardWidth; i++ ) {
  28. for (int j = 0; j < boardWidth; j++ ) {
  29. gameGrid[i][j] = new GameObject(ObjectType.EMPTY,i,j);
  30. }
  31. }
  32.  
  33. this.hadica.add(new GameObject(ObjectType.SNAKE,3,4));
  34. this.hadica.add(new GameObject(ObjectType.SNAKE,3,5));
  35. this.hadica.add(new GameObject(ObjectType.SNAKE,3,6));
  36. head = this.hadica.get(0);
  37. }
  38.  
  39. public void tik() {
  40. GameObject prdel = this.hadica.remove(this.hadica.size()-1);
  41.  
  42.  
  43. prdel.setX(this.hadica.get(0).getX() + 1); //V pripade ze DIR je RIGHT
  44. prdel.setY(this.hadica.get(0).getY())
  45.  
  46. prdel.presunStvorec(getX * squareSize, getY * squareSize);
  47. this.hadica.insert(prdel,0);
  48. }
  49.  
  50.  
  51. }
  52.  
  53.  
  54. //===
  55.  
  56.  
  57.  
  58. public class GameObject {
  59.  
  60. public enum ObjectType {
  61. EMPTY,
  62. SNAKE,
  63. APPLE;
  64. }
  65.  
  66. ObjectType druhObjektu;
  67. int x; //suradcnice na ploche
  68. int y;
  69. Stvorec stvorec //suradnice na okne
  70.  
  71. GameObject(ObjectType type, int x, int y) {
  72. this.druhObjektu = type;
  73. this.x = x;
  74. this.y = y;
  75. }
  76.  
  77. public int getX() {
  78. return this.x;
  79. }
  80.  
  81. public int getY() {
  82. return this.y;
  83. }
  84.  
  85. public int getObjectType() {
  86. return this.druhObjektu;
  87. }
  88. }
  89.  
  90.  
  91.  
  92. // ====
  93.  
  94. public static class main {
  95. private static Manazer manazer;
  96. private static Game game;
  97.  
  98.  
  99. main() {
  100. game = new Game();
  101. manazer = new Manazer();
  102. manazer.spravujObjekt(game);
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement