From 52f4c7379e7fce57bee547dac92d5dc0a023c3a1 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 15 Nov 2018 00:00:42 +0200 Subject: [PATCH] 3 --- src/Grid.java | 10 +++++-- src/RayCanvas.java | 75 +++++++++++++++++++++++++--------------------- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/Grid.java b/src/Grid.java index c120c70..c0dfe6c 100644 --- a/src/Grid.java +++ b/src/Grid.java @@ -36,6 +36,11 @@ public class Grid { this.height = height; } + public Grid(int max) { + this.width = getRandomInt(max)+10; + this.height = getRandomInt(max)+10; + } + public void newMaze() { mySquares = new int[width][height]; // initialize all of the squares to white except a lattice @@ -50,7 +55,8 @@ public class Grid { // the entrance to the maze is at (0,1). mySquares[0][1] = 0; createMaze(); - mySquares[mySquares.length-1][mySquares[0].length-2] = 5; + mySquares[mySquares.length-2][mySquares[0].length-3] = 5; + mySquares[mySquares.length-1][mySquares[0].length-3] = 5; } /** @@ -191,7 +197,7 @@ public class Grid { * @param upper the upper bound for the random int. * @return a random non-negative int less than the bound upper. */ - public int getRandomInt(int upper) { + private int getRandomInt(int upper) { int retVal = myRandom.nextInt() % upper; if (retVal < 0) { retVal += upper; diff --git a/src/RayCanvas.java b/src/RayCanvas.java index efd86c2..dfde177 100644 --- a/src/RayCanvas.java +++ b/src/RayCanvas.java @@ -6,8 +6,6 @@ public class RayCanvas extends Canvas {// implements Runnable { private Keyboard keyb; private Grid grid; - //private final int mapWidth = 24; - //private final int mapHeight = 24; private int gluk; private Image pr; @@ -22,19 +20,19 @@ public class RayCanvas extends Canvas {// implements Runnable { private double planeX, planeY; //the 2d raycaster version of camera plane private int w,h; private long lastTime; - private boolean showUI; - private final int size = 4; + private boolean showUI, end; + private int size = 4; public RayCanvas() { setFullScreenMode(true); w = getWidth(); h = getHeight(); - grid = new Grid(24, 24); + grid = new Grid(25); grid.newMaze(); - showUI = false; - posX = 7; - posY = 6.5; + end = showUI = false; + posX = 1; + posY = 1.1; dirX = -1; lastTime = 0; dirY = planeX = 0; @@ -45,22 +43,15 @@ public class RayCanvas extends Canvas {// implements Runnable { try { pr = Image.createImage("/2.png"); } catch (IOException ex) {} -// new Thread(this).start(); } -// public void run() { -// while (true) { -// try { -// repaint(); -// Thread.sleep(3); -// } catch (Exception e) {} -// } -// } - protected void keyPressed(int key) { if(key == KEY_STAR) showUI = !showUI; - else if (key == -6) grid.newMaze(); + else if (key == -6) restart(false); + else if (key == -7 || (end && getGameAction(key)==FIRE)) restart(true); else if (key == KEY_NUM0) Main.midlet.destroyApp(true); + else if(showUI && key == KEY_NUM1) uiSize(-1); + else if(showUI && key == KEY_NUM3) uiSize(1); else keyb.onPressed(key, getGameAction(key)); } @@ -76,7 +67,13 @@ public class RayCanvas extends Canvas {// implements Runnable { g.setColor(0); g.fillRect(0, 0, w, h); - for (int x = 0; x < w; x++) { + int yy = grid.width/3; + if (posX < yy) gluk = 1; + else if(posX > yy && posX < yy*2) gluk = 2; + else if (posX > yy*2 && posX < grid.width) gluk = 3; + else if ((posX > (yy+yy/2)) && posY < (grid.height/2)) gluk = 0; + + for (int x = 0; x < w; x+=1) { //calculate ray position and direction double cameraX = 2 * x / (double) w - 1; //x-coordinate in camera space double rayPosX = posX; @@ -154,14 +151,7 @@ public class RayCanvas extends Canvas {// implements Runnable { g.drawImage(pr, w / 2, h / 2, 3); //choose wall color - if (posX < 8) gluk = 1; - else if(posX > 8 && posX < 16) gluk = 2; - else if (posX > 16 && posX < 24) gluk = 3; - else if ((posX > 13) && posY < 11) gluk = 0; - else if (posY < 9 && posX < 20 && posY > 4 && posX > 17) { - g.setColor(0xff0000); - g.drawString("Вы выиграли‘", w/2, h/2, 33); - } + int color = 0; try { color = colors[gluk][getCell(mapX, mapY)]; @@ -221,17 +211,18 @@ public class RayCanvas extends Canvas {// implements Runnable { } if(showUI) showLabirinth(g, (int)posX, (int)posY); + if (end) { + g.setColor(0xFF6159); + g.drawString("Вы выиграли", w/2, h/2, 33); + } repaint(); } private void showLabirinth(Graphics g, int x, int y) { - System.out.println("x= "+x+" y= "+y+" id= "+grid.mySquares[x][y]); - g.setColor(0xFFFFFF); - g.fillRect(0, 0, grid.width, grid.height); g.setColor(0x00FF); for(int i=0; i10) size = 10; + } }