diff --git a/src/main/java/Block.java b/src/main/java/Block.java index 20d96b1..0722127 100644 --- a/src/main/java/Block.java +++ b/src/main/java/Block.java @@ -3,11 +3,14 @@ import java.util.Random; public class Block extends Cell { Random random; - Game game; - public Block(Game game){ - super(-1, - 1, game.CELL_SIZE, Color.BLUE); - this.game = game; + public Block(){ + super(-1, - 1, WindowOfFreeMove.CELL_SIZE, Color.BLUE); + this.random = new Random(); + } + + public Block(int x, int y){ + super(x,y, WindowOfFreeMove.CELL_SIZE, Color.BLUE); this.random = new Random(); } @@ -18,12 +21,12 @@ public class Block extends Cell { return false; } - public void appear() { // show food at new x,y coordinates + public void randomAppear() { int x, y; do { - x = random.nextInt(20); - y = random.nextInt(20); - } while (game.isBusyCell(x, y)); + x = random.nextInt(WindowOfFreeMove.CANVAS_HEIGHT); + y = random.nextInt(WindowOfFreeMove.CANVAS_WIDTH); + } while (WindowOfFreeMove.map.isBusyCell(x, y)); set(x, y); } } diff --git a/src/main/java/Game.java b/src/main/java/Game.java index 32d60c3..2ea6563 100644 --- a/src/main/java/Game.java +++ b/src/main/java/Game.java @@ -1,84 +1,5 @@ -import javax.swing.*; -import java.awt.*; -import java.awt.event.*; - -public class Game extends JFrame { - - final int CELL_SIZE = 20; // size of cell in pix - final int CANVAS_WIDTH = 20; // width in cells - final int CANVAS_HEIGHT = 20; // height in cells - - - static final int KEY_LEFT = 37; // codes - static final int KEY_UP = 38; // of - static final int KEY_RIGHT = 39; // cursor - static final int KEY_DOWN = 40; // keys - - Canvas canvas; - Block block; - Person person; - +public class Game { public static void main(String[] args) { - new Game().start(); - } - - public Game() { - setTitle("Ahoj"); - setDefaultCloseOperation(EXIT_ON_CLOSE); - - canvas = new Canvas(); - canvas.setBackground(Color.white); - canvas.setPreferredSize(new Dimension( - CELL_SIZE * CANVAS_WIDTH, - CELL_SIZE * CANVAS_HEIGHT)); - - addKeyListener(new KeyAdapter() { - @Override - public void keyPressed(KeyEvent e) { - person.setDirection(e.getKeyCode()); - } - }); - - add(canvas); - pack(); - setLocationRelativeTo(null); - setResizable(false); - setVisible(true); - } - - public boolean isBusyCell(int x, int y){ - return block.isInBlock(x,y) || person.isInPerson(x,y); - } - - private void start() { - person = new Person(this); - block = new Block(this); - block.appear(); - - while (true) { - person.move(); - canvas.repaint(); - sleep(); - } - } - - private void sleep() { // method for suspending - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - class Canvas extends JPanel { // class for rendering (drawing) - @Override - public void paint(Graphics g) { - super.paint(g); - Graphics2D g2D = (Graphics2D) g; - g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); - block.paint(g2D); - person.paint(g2D); - } + new WindowOfFreeMove().start(); } } \ No newline at end of file diff --git a/src/main/java/Map.java b/src/main/java/Map.java index 327b561..ab23de5 100644 --- a/src/main/java/Map.java +++ b/src/main/java/Map.java @@ -6,17 +6,14 @@ public class Map { private TypeOfCell[][] map; private Person person; private LinkedList block; - private Game game; - Map(int height, int width, Game game){ - block = new LinkedList(); + Map(int height, int width){ + block = new LinkedList<>(); map = new TypeOfCell[height][width]; - this.game = game; } - Map(int height, int width, Game game, int xPerson, int yPerson){ - block = new LinkedList(); - this.game = game; + Map(int height, int width, int xPerson, int yPerson){ + block = new LinkedList<>(); map = new TypeOfCell[height][width]; if ((height < xPerson || xPerson < 0) || (width < yPerson || yPerson < 0)){ map[0][0] = TypeOfCell.PERSON; @@ -24,9 +21,8 @@ public class Map { else map [xPerson][yPerson] = TypeOfCell.PERSON; } - Map(int height, int width, Game game,int xPerson, int yPerson, TypeOfCell type){ - block = new LinkedList(); - this.game = game; + Map(int height, int width,int xPerson, int yPerson, TypeOfCell type){ + block = new LinkedList<>(); map = new TypeOfCell[height][width]; if ((height - 1 < xPerson || xPerson <= 0) || (width - 1 < yPerson || yPerson <= 0)){ map[1][1] = TypeOfCell.PERSON; @@ -36,8 +32,10 @@ public class Map { for (int i = 0; i < map.length; i++){ for (int j = 0; j < map.length; j++){ map[0][j] = type; + map[map.length - 1][j] = type; } map[i][0] = type; + map[i][map.length - 1] = type; } } @@ -45,15 +43,17 @@ public class Map { return person; } + public LinkedList getBlocks() { return block; } + public void createMap(){ for (int i = 0; i < map.length; i++){ for (int j = 0; j < map.length; j++){ if (map[i][j] == TypeOfCell.BLOCK){ - block.add(new Block(i,j,game)); + block.add(new Block(i,j)); } if (map[i][j] == TypeOfCell.PERSON){ - person = new Person(i,j,game); + person = new Person(i,j); } } } diff --git a/src/main/java/Person.java b/src/main/java/Person.java index 5811bdb..7aaa678 100644 --- a/src/main/java/Person.java +++ b/src/main/java/Person.java @@ -3,16 +3,19 @@ import java.awt.*; public class Person { private Cell person; private int direction; - private Game game; - public Person(Game game){ + public Person(){ this.direction = 0; - this.game = game; - this.person = new Cell(10,10,game.CELL_SIZE,Color.RED); + this.person = new Cell(10,10, WindowOfFreeMove.CELL_SIZE,Color.RED); + } + + public Person(int x, int y){ + this.direction = 0; + this.person = new Cell(x,y, WindowOfFreeMove.CELL_SIZE,Color.RED); } public void setDirection(int direction) { - if ((direction >= Game.KEY_LEFT) && (direction <= Game.KEY_DOWN)) + if ((direction >= WindowOfFreeMove.KEY_LEFT) && (direction <= WindowOfFreeMove.KEY_DOWN)) this.direction = direction; } @@ -20,25 +23,25 @@ public class Person { int x = person.getX(); int y = person.getY(); switch (direction) { - case Game.KEY_LEFT: x--; + case WindowOfFreeMove.KEY_LEFT: x--; if (x < 0) - x = game.CANVAS_WIDTH - 1; + x = WindowOfFreeMove.CANVAS_WIDTH - 1; break; - case Game.KEY_RIGHT: x++; - if (x == game.CANVAS_WIDTH) + case WindowOfFreeMove.KEY_RIGHT: x++; + if (x == WindowOfFreeMove.CANVAS_WIDTH) x = 0; break; - case Game.KEY_UP: y--; + case WindowOfFreeMove.KEY_UP: y--; if (y < 0) - y = game.CANVAS_HEIGHT - 1; + y = WindowOfFreeMove.CANVAS_HEIGHT - 1; break; - case Game.KEY_DOWN: y++; - if (y == game.CANVAS_HEIGHT) + case WindowOfFreeMove.KEY_DOWN: y++; + if (y == WindowOfFreeMove.CANVAS_HEIGHT) y = 0; break; } this.direction = 0; - if (!game.isBusyCell(x, y)){ + if (!WindowOfFreeMove.map.isBusyCell(x, y)){ person = new Cell(x , y,20,Color.RED); } diff --git a/src/main/java/WindowOfFreeMove.java b/src/main/java/WindowOfFreeMove.java new file mode 100644 index 0000000..2dbd105 --- /dev/null +++ b/src/main/java/WindowOfFreeMove.java @@ -0,0 +1,76 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; + +public class WindowOfFreeMove extends JFrame { + static final int CELL_SIZE = 20; + static final int CANVAS_WIDTH = 20; + static final int CANVAS_HEIGHT = 20; + + + static final int KEY_LEFT = 37; + static final int KEY_UP = 38; + static final int KEY_RIGHT = 39; + static final int KEY_DOWN = 40; + + Canvas canvas; + static Map map; + + public WindowOfFreeMove() { + setTitle("Ahoj"); + setDefaultCloseOperation(EXIT_ON_CLOSE); + + canvas = new Canvas(); + canvas.setBackground(Color.white); + canvas.setPreferredSize(new Dimension( + CELL_SIZE * CANVAS_WIDTH - 10, + CELL_SIZE * CANVAS_HEIGHT - 10)); + + addKeyListener(new KeyAdapter() { + @Override + public void keyPressed(KeyEvent e) { + map.getPerson().setDirection(e.getKeyCode()); + } + }); + + add(canvas); + pack(); + setLocationRelativeTo(null); + setResizable(false); + setVisible(true); + } + + public void start() { + map = new Map(CANVAS_HEIGHT,CANVAS_WIDTH, 10, 10, TypeOfCell.BLOCK); + map.createMap(); + + while (true) { + map.getPerson().move(); + canvas.repaint(); + } + } + + class Canvas extends JPanel { + @Override + public void paint(Graphics g) { + super.paint(g); + Graphics2D g2D = (Graphics2D) g; + g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + for (Cell cell : map.getBlocks()) { + cell.paint(g2D); + } + map.getPerson().paint(g2D); + } + } + + private void sleep() { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file