3
This commit is contained in:
parent
57e16a639d
commit
52f4c7379e
@ -36,6 +36,11 @@ public class Grid {
|
|||||||
this.height = height;
|
this.height = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Grid(int max) {
|
||||||
|
this.width = getRandomInt(max)+10;
|
||||||
|
this.height = getRandomInt(max)+10;
|
||||||
|
}
|
||||||
|
|
||||||
public void newMaze() {
|
public void newMaze() {
|
||||||
mySquares = new int[width][height];
|
mySquares = new int[width][height];
|
||||||
// initialize all of the squares to white except a lattice
|
// 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).
|
// the entrance to the maze is at (0,1).
|
||||||
mySquares[0][1] = 0;
|
mySquares[0][1] = 0;
|
||||||
createMaze();
|
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.
|
* @param upper the upper bound for the random int.
|
||||||
* @return a random non-negative int less than the bound upper.
|
* @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;
|
int retVal = myRandom.nextInt() % upper;
|
||||||
if (retVal < 0) {
|
if (retVal < 0) {
|
||||||
retVal += upper;
|
retVal += upper;
|
||||||
|
@ -6,8 +6,6 @@ public class RayCanvas extends Canvas {// implements Runnable {
|
|||||||
|
|
||||||
private Keyboard keyb;
|
private Keyboard keyb;
|
||||||
private Grid grid;
|
private Grid grid;
|
||||||
//private final int mapWidth = 24;
|
|
||||||
//private final int mapHeight = 24;
|
|
||||||
private int gluk;
|
private int gluk;
|
||||||
private Image pr;
|
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 double planeX, planeY; //the 2d raycaster version of camera plane
|
||||||
private int w,h;
|
private int w,h;
|
||||||
private long lastTime;
|
private long lastTime;
|
||||||
private boolean showUI;
|
private boolean showUI, end;
|
||||||
private final int size = 4;
|
private int size = 4;
|
||||||
|
|
||||||
public RayCanvas() {
|
public RayCanvas() {
|
||||||
setFullScreenMode(true);
|
setFullScreenMode(true);
|
||||||
w = getWidth();
|
w = getWidth();
|
||||||
h = getHeight();
|
h = getHeight();
|
||||||
grid = new Grid(24, 24);
|
grid = new Grid(25);
|
||||||
grid.newMaze();
|
grid.newMaze();
|
||||||
|
|
||||||
showUI = false;
|
end = showUI = false;
|
||||||
posX = 7;
|
posX = 1;
|
||||||
posY = 6.5;
|
posY = 1.1;
|
||||||
dirX = -1;
|
dirX = -1;
|
||||||
lastTime = 0;
|
lastTime = 0;
|
||||||
dirY = planeX = 0;
|
dirY = planeX = 0;
|
||||||
@ -45,22 +43,15 @@ public class RayCanvas extends Canvas {// implements Runnable {
|
|||||||
try {
|
try {
|
||||||
pr = Image.createImage("/2.png");
|
pr = Image.createImage("/2.png");
|
||||||
} catch (IOException ex) {}
|
} 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) {
|
protected void keyPressed(int key) {
|
||||||
if(key == KEY_STAR) showUI = !showUI;
|
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 (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));
|
else keyb.onPressed(key, getGameAction(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +67,13 @@ public class RayCanvas extends Canvas {// implements Runnable {
|
|||||||
g.setColor(0);
|
g.setColor(0);
|
||||||
g.fillRect(0, 0, w, h);
|
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
|
//calculate ray position and direction
|
||||||
double cameraX = 2 * x / (double) w - 1; //x-coordinate in camera space
|
double cameraX = 2 * x / (double) w - 1; //x-coordinate in camera space
|
||||||
double rayPosX = posX;
|
double rayPosX = posX;
|
||||||
@ -154,14 +151,7 @@ public class RayCanvas extends Canvas {// implements Runnable {
|
|||||||
|
|
||||||
g.drawImage(pr, w / 2, h / 2, 3);
|
g.drawImage(pr, w / 2, h / 2, 3);
|
||||||
//choose wall color
|
//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;
|
int color = 0;
|
||||||
try {
|
try {
|
||||||
color = colors[gluk][getCell(mapX, mapY)];
|
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(showUI) showLabirinth(g, (int)posX, (int)posY);
|
||||||
|
if (end) {
|
||||||
|
g.setColor(0xFF6159);
|
||||||
|
g.drawString("Âû âûèãðàëè", w/2, h/2, 33);
|
||||||
|
}
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showLabirinth(Graphics g, int x, int y) {
|
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);
|
g.setColor(0x00FF);
|
||||||
for(int i=0; i<grid.width; i++) {
|
for(int i=0; i<grid.width; i++) {
|
||||||
for(int j=0; j<grid.height; j++) {
|
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);
|
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) {
|
private int getCell(int x, int y) {
|
||||||
try {
|
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) {
|
}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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user