This commit is contained in:
Victor 2018-11-15 00:00:42 +02:00
parent 57e16a639d
commit 52f4c7379e
2 changed files with 49 additions and 36 deletions

View File

@ -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;

View File

@ -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; i<grid.width; i++) {
for(int j=0; j<grid.height; j++) {
if(grid.mySquares[i][j]==1)
if(grid.mySquares[i][j]==0)
g.fillRect(i*size, j*size, size, size);
}
}
@ -241,9 +232,25 @@ public class RayCanvas extends Canvas {// implements Runnable {
private int getCell(int x, int y) {
try {
return grid.mySquares[x][y];
int yy = Math.abs(1-grid.mySquares[x][y]);
if(yy==4) end = true;
return yy;
}catch(Exception ex) {
return 1;
return 0;
}
}
private void restart(boolean newsize) {
if(newsize) grid = new Grid(25);
grid.newMaze();
end = false;
posX = 1;
posY = 1.1;
}
private void uiSize(int num) {
size+=num;
if(size<2) size = 2;
else if(size>10) size = 10;
}
}