Fungujici mapa a jejich zapnuti

This commit is contained in:
iryupnik 2020-02-26 09:38:39 +02:00
parent 427f27eef3
commit 5616d7196a
5 changed files with 118 additions and 115 deletions

View File

@ -3,11 +3,14 @@ import java.util.Random;
public class Block extends Cell { public class Block extends Cell {
Random random; Random random;
Game game;
public Block(Game game){ public Block(){
super(-1, - 1, game.CELL_SIZE, Color.BLUE); super(-1, - 1, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
this.game = game; this.random = new Random();
}
public Block(int x, int y){
super(x,y, WindowOfFreeMove.CELL_SIZE, Color.BLUE);
this.random = new Random(); this.random = new Random();
} }
@ -18,12 +21,12 @@ public class Block extends Cell {
return false; return false;
} }
public void appear() { // show food at new x,y coordinates public void randomAppear() {
int x, y; int x, y;
do { do {
x = random.nextInt(20); x = random.nextInt(WindowOfFreeMove.CANVAS_HEIGHT);
y = random.nextInt(20); y = random.nextInt(WindowOfFreeMove.CANVAS_WIDTH);
} while (game.isBusyCell(x, y)); } while (WindowOfFreeMove.map.isBusyCell(x, y));
set(x, y); set(x, y);
} }
} }

View File

@ -1,84 +1,5 @@
import javax.swing.*; public class Game {
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) { public static void main(String[] args) {
new Game().start(); new WindowOfFreeMove().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);
}
} }
} }

View File

@ -6,17 +6,14 @@ public class Map {
private TypeOfCell[][] map; private TypeOfCell[][] map;
private Person person; private Person person;
private LinkedList<Block> block; private LinkedList<Block> block;
private Game game;
Map(int height, int width, Game game){ Map(int height, int width){
block = new LinkedList<Block>(); block = new LinkedList<>();
map = new TypeOfCell[height][width]; map = new TypeOfCell[height][width];
this.game = game;
} }
Map(int height, int width, Game game, int xPerson, int yPerson){ Map(int height, int width, int xPerson, int yPerson){
block = new LinkedList<Block>(); block = new LinkedList<>();
this.game = game;
map = new TypeOfCell[height][width]; map = new TypeOfCell[height][width];
if ((height < xPerson || xPerson < 0) || (width < yPerson || yPerson < 0)){ if ((height < xPerson || xPerson < 0) || (width < yPerson || yPerson < 0)){
map[0][0] = TypeOfCell.PERSON; map[0][0] = TypeOfCell.PERSON;
@ -24,9 +21,8 @@ public class Map {
else map [xPerson][yPerson] = TypeOfCell.PERSON; else map [xPerson][yPerson] = TypeOfCell.PERSON;
} }
Map(int height, int width, Game game,int xPerson, int yPerson, TypeOfCell type){ Map(int height, int width,int xPerson, int yPerson, TypeOfCell type){
block = new LinkedList<Block>(); block = new LinkedList<>();
this.game = game;
map = new TypeOfCell[height][width]; map = new TypeOfCell[height][width];
if ((height - 1 < xPerson || xPerson <= 0) || (width - 1 < yPerson || yPerson <= 0)){ if ((height - 1 < xPerson || xPerson <= 0) || (width - 1 < yPerson || yPerson <= 0)){
map[1][1] = TypeOfCell.PERSON; map[1][1] = TypeOfCell.PERSON;
@ -36,8 +32,10 @@ public class Map {
for (int i = 0; i < map.length; i++){ for (int i = 0; i < map.length; i++){
for (int j = 0; j < map.length; j++){ for (int j = 0; j < map.length; j++){
map[0][j] = type; map[0][j] = type;
map[map.length - 1][j] = type;
} }
map[i][0] = type; map[i][0] = type;
map[i][map.length - 1] = type;
} }
} }
@ -45,15 +43,17 @@ public class Map {
return person; return person;
} }
public LinkedList<Block> getBlocks() { return block; }
public void createMap(){ public void createMap(){
for (int i = 0; i < map.length; i++){ for (int i = 0; i < map.length; i++){
for (int j = 0; j < map.length; j++){ for (int j = 0; j < map.length; j++){
if (map[i][j] == TypeOfCell.BLOCK){ 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){ if (map[i][j] == TypeOfCell.PERSON){
person = new Person(i,j,game); person = new Person(i,j);
} }
} }
} }

View File

@ -3,16 +3,19 @@ import java.awt.*;
public class Person { public class Person {
private Cell person; private Cell person;
private int direction; private int direction;
private Game game;
public Person(Game game){ public Person(){
this.direction = 0; this.direction = 0;
this.game = game; this.person = new Cell(10,10, WindowOfFreeMove.CELL_SIZE,Color.RED);
this.person = new Cell(10,10,game.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) { 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; this.direction = direction;
} }
@ -20,25 +23,25 @@ public class Person {
int x = person.getX(); int x = person.getX();
int y = person.getY(); int y = person.getY();
switch (direction) { switch (direction) {
case Game.KEY_LEFT: x--; case WindowOfFreeMove.KEY_LEFT: x--;
if (x < 0) if (x < 0)
x = game.CANVAS_WIDTH - 1; x = WindowOfFreeMove.CANVAS_WIDTH - 1;
break; break;
case Game.KEY_RIGHT: x++; case WindowOfFreeMove.KEY_RIGHT: x++;
if (x == game.CANVAS_WIDTH) if (x == WindowOfFreeMove.CANVAS_WIDTH)
x = 0; x = 0;
break; break;
case Game.KEY_UP: y--; case WindowOfFreeMove.KEY_UP: y--;
if (y < 0) if (y < 0)
y = game.CANVAS_HEIGHT - 1; y = WindowOfFreeMove.CANVAS_HEIGHT - 1;
break; break;
case Game.KEY_DOWN: y++; case WindowOfFreeMove.KEY_DOWN: y++;
if (y == game.CANVAS_HEIGHT) if (y == WindowOfFreeMove.CANVAS_HEIGHT)
y = 0; y = 0;
break; break;
} }
this.direction = 0; this.direction = 0;
if (!game.isBusyCell(x, y)){ if (!WindowOfFreeMove.map.isBusyCell(x, y)){
person = new Cell(x , y,20,Color.RED); person = new Cell(x , y,20,Color.RED);
} }

View File

@ -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();
}
}
}