Initial commit
This commit is contained in:
commit
530bd727ef
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.idea
|
||||||
2
gameOfProd.iml
Normal file
2
gameOfProd.iml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="JAVA_MODULE" version="4" />
|
||||||
12
pom.xml
Normal file
12
pom.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>rpgEng</groupId>
|
||||||
|
<artifactId>gameOfProd</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
29
src/main/java/Block.java
Normal file
29
src/main/java/Block.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/main/java/Cell.java
Normal file
32
src/main/java/Cell.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
84
src/main/java/Game.java
Normal file
84
src/main/java/Game.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/main/java/Map.java
Normal file
19
src/main/java/Map.java
Normal file
@ -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 < )
|
||||||
|
}
|
||||||
|
}
|
||||||
57
src/main/java/Person.java
Normal file
57
src/main/java/Person.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
3
src/main/java/TypeOfCell.java
Normal file
3
src/main/java/TypeOfCell.java
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
public enum TypeOfCell {
|
||||||
|
EMPTY, BLOCK, PERSON
|
||||||
|
}
|
||||||
BIN
target/classes/Block.class
Normal file
BIN
target/classes/Block.class
Normal file
Binary file not shown.
BIN
target/classes/Cell.class
Normal file
BIN
target/classes/Cell.class
Normal file
Binary file not shown.
BIN
target/classes/Game$1.class
Normal file
BIN
target/classes/Game$1.class
Normal file
Binary file not shown.
BIN
target/classes/Game$Canvas.class
Normal file
BIN
target/classes/Game$Canvas.class
Normal file
Binary file not shown.
BIN
target/classes/Game.class
Normal file
BIN
target/classes/Game.class
Normal file
Binary file not shown.
BIN
target/classes/Map.class
Normal file
BIN
target/classes/Map.class
Normal file
Binary file not shown.
BIN
target/classes/Person.class
Normal file
BIN
target/classes/Person.class
Normal file
Binary file not shown.
BIN
target/classes/TypeOfCell.class
Normal file
BIN
target/classes/TypeOfCell.class
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user