commit 530bd727ef297bc901b495e975acfd839e83a6a5 Author: iryupnik Date: Tue Feb 25 21:19:15 2020 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/gameOfProd.iml b/gameOfProd.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/gameOfProd.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7fbc5fd --- /dev/null +++ b/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + rpgEng + gameOfProd + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/src/main/java/Block.java b/src/main/java/Block.java new file mode 100644 index 0000000..20d96b1 --- /dev/null +++ b/src/main/java/Block.java @@ -0,0 +1,29 @@ +import java.awt.*; +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; + this.random = new Random(); + } + + public boolean isInBlock(int x, int y) { + if ((this. + getX() == x) && (this.getY() == y)) + return true; + return false; + } + + public void appear() { // show food at new x,y coordinates + int x, y; + do { + x = random.nextInt(20); + y = random.nextInt(20); + } while (game.isBusyCell(x, y)); + set(x, y); + } +} diff --git a/src/main/java/Cell.java b/src/main/java/Cell.java new file mode 100644 index 0000000..0599404 --- /dev/null +++ b/src/main/java/Cell.java @@ -0,0 +1,32 @@ +import java.awt.*; + +public class Cell { + protected int x, y; // object coordinates + protected int size; // object size in px + protected Color color; // object color + + public Cell(int x, int y, int size, Color color) { + set(x, y); + this.size = size; + this.color = color; + } + + public void set(int x, int y) { + this.x = x; + this.y = y; + } + + public int getX() { // get the X coordinate + return x; + } + + public int getY() { // get the Y coordinate + return y; + } + + public void paint(Graphics2D g) {// object rendering + g.setColor(color); + g.fillOval(x * size, y * size, size, size); + g.drawString("ahoj123", 0, 0); + } +} diff --git a/src/main/java/Game.java b/src/main/java/Game.java new file mode 100644 index 0000000..32d60c3 --- /dev/null +++ b/src/main/java/Game.java @@ -0,0 +1,84 @@ +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 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); + } + } +} \ No newline at end of file diff --git a/src/main/java/Map.java b/src/main/java/Map.java new file mode 100644 index 0000000..48cabf6 --- /dev/null +++ b/src/main/java/Map.java @@ -0,0 +1,19 @@ +public class Map { + private TypeOfCell[][] map; + private Person person; + private Block block; + private static Game game = new Game(); + + Map(int height, int width, Game game){ + map = new TypeOfCell[height][width]; + person = new Person(game); + block = new Block(game); + } + + Map(int height, int width, Game game, int xPerson, int yPerson){ + map = new TypeOfCell[height][width]; + person = new Person(game); + block = new Block(game); + // if (height < ) + } +} diff --git a/src/main/java/Person.java b/src/main/java/Person.java new file mode 100644 index 0000000..5811bdb --- /dev/null +++ b/src/main/java/Person.java @@ -0,0 +1,57 @@ +import java.awt.*; + +public class Person { + private Cell person; + private int direction; + private Game game; + + public Person(Game game){ + this.direction = 0; + this.game = game; + this.person = new Cell(10,10,game.CELL_SIZE,Color.RED); + } + + public void setDirection(int direction) { + if ((direction >= Game.KEY_LEFT) && (direction <= Game.KEY_DOWN)) + this.direction = direction; + } + + public void move(){ + int x = person.getX(); + int y = person.getY(); + switch (direction) { + case Game.KEY_LEFT: x--; + if (x < 0) + x = game.CANVAS_WIDTH - 1; + break; + case Game.KEY_RIGHT: x++; + if (x == game.CANVAS_WIDTH) + x = 0; + break; + case Game.KEY_UP: y--; + if (y < 0) + y = game.CANVAS_HEIGHT - 1; + break; + case Game.KEY_DOWN: y++; + if (y == game.CANVAS_HEIGHT) + y = 0; + break; + } + this.direction = 0; + if (!game.isBusyCell(x, y)){ + person = new Cell(x , y,20,Color.RED); + } + + } + + public void paint(Graphics2D g) { + person.paint(g); + } + + + public boolean isInPerson(int x, int y) { + if (this.person.getX() == x && this.person.getY() == y) + return true; + return false; + } +} diff --git a/src/main/java/TypeOfCell.java b/src/main/java/TypeOfCell.java new file mode 100644 index 0000000..a76fb2d --- /dev/null +++ b/src/main/java/TypeOfCell.java @@ -0,0 +1,3 @@ +public enum TypeOfCell { + EMPTY, BLOCK, PERSON +} diff --git a/target/classes/Block.class b/target/classes/Block.class new file mode 100644 index 0000000..ee9eaf5 Binary files /dev/null and b/target/classes/Block.class differ diff --git a/target/classes/Cell.class b/target/classes/Cell.class new file mode 100644 index 0000000..f433d6b Binary files /dev/null and b/target/classes/Cell.class differ diff --git a/target/classes/Game$1.class b/target/classes/Game$1.class new file mode 100644 index 0000000..8802fd7 Binary files /dev/null and b/target/classes/Game$1.class differ diff --git a/target/classes/Game$Canvas.class b/target/classes/Game$Canvas.class new file mode 100644 index 0000000..859e1a0 Binary files /dev/null and b/target/classes/Game$Canvas.class differ diff --git a/target/classes/Game.class b/target/classes/Game.class new file mode 100644 index 0000000..57df7d1 Binary files /dev/null and b/target/classes/Game.class differ diff --git a/target/classes/Map.class b/target/classes/Map.class new file mode 100644 index 0000000..a7ce07e Binary files /dev/null and b/target/classes/Map.class differ diff --git a/target/classes/Person.class b/target/classes/Person.class new file mode 100644 index 0000000..6dcbbd7 Binary files /dev/null and b/target/classes/Person.class differ diff --git a/target/classes/TypeOfCell.class b/target/classes/TypeOfCell.class new file mode 100644 index 0000000..6bcd3b8 Binary files /dev/null and b/target/classes/TypeOfCell.class differ