Created
January 27, 2015 13:23
-
-
Save CSchoel/ebfdaef6b0124a57c0ad to your computer and use it in GitHub Desktop.
Labyrinth game with rudimentary Labyrinth generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Autor: Christopher Schölzel | |
class Square { | |
boolean negotiable; //begehbar oder nicht? | |
boolean isExit; | |
Square(boolean negotiable, boolean isExit) { | |
this.negotiable = negotiable; | |
this.isExit = isExit; | |
} | |
boolean isExit() { | |
return isExit; | |
} | |
boolean isNegotiable() { | |
return negotiable; | |
} | |
void display(float x, float y, float size) { | |
stroke(0); | |
color c = isExit() ? #FFFF00 : (isNegotiable() ? 255 : 0); | |
fill(c); | |
rect(x*size,y*size,size,size); | |
} | |
} | |
class Labyrinth { | |
Square[] squares; | |
int labWidth,labHeight; | |
float squareWidth; | |
Labyrinth(int w, int h, float squareWidth) { | |
squares = new Square[w*h]; | |
//mit begehbaren Feldern füllen | |
for(int i = 0; i < squares.length; i++) | |
squares[i] = new Square(true,false); | |
labWidth = w; | |
labHeight = h; | |
this.squareWidth = squareWidth; | |
} | |
void generateCorridors(float pStop, float pFork) { | |
//fill with walls | |
for(int i = 0; i < squares.length; i++) { | |
squares[i] = new Square(false,false); | |
} | |
int dx = (int)random(3)-1; | |
int dy = dx != 0 ? 0 : (random(1) > 0.5) ? 1 : -1; | |
generateCorridors(labWidth/2,labHeight/2,dx,dy,pStop,pFork); | |
int goalIdx = 0; | |
while(!squares[goalIdx].isNegotiable()) goalIdx++; | |
squares[goalIdx] = new Square(true,true); | |
} | |
void generateCorridors(int startX, int startY, int dx, int dy, float pStop, float pFork) { | |
setSquare(startX,startY,new Square(true,false)); //aktuelles Feld begehbar machen | |
int leftX = startX + (dx != 0 ? 0 : 1); | |
int leftY = startY + (dy != 0 ? 0 : 1); | |
int rightX = startX + (dx != 0 ? 0 : -1); | |
int rightY = startY + (dy != 0 ? 0 : -1); | |
tryToFork(leftX,leftY,startX,startY,pStop,pFork); //Gang nach links beginnen | |
tryToFork(rightX,rightY,startX,startY,pStop,pFork); //Gang nach rechts beginnen | |
int nx = startX+dx; | |
int ny = startY+dy; | |
if(!isLegalIndex(nx,ny)) return; //Kartenende => aufhören zu graben | |
Square next = getSquare(nx,ny); | |
if(next.isNegotiable()) return; //Durchbruch => aufhören zu graben | |
if(random(1) > pStop) generateCorridors(nx,ny,dx,dy,pStop,pFork); | |
} | |
void tryToFork(int x, int y, int fromX, int fromY, float pStop, float pFork) { | |
if(isLegalIndex(x,y)) { | |
if(!getSquare(x,y).isNegotiable() && random(1) < pFork) { | |
generateCorridors(x,y,x-fromX,y-fromY,pStop,pFork); | |
} | |
} | |
} | |
Square getSquare(int x, int y) { | |
return squares[internalIndex(x,y)]; | |
} | |
void setSquare(int x, int y, Square s) { | |
squares[internalIndex(x,y)] = s; | |
} | |
int internalIndex(int x, int y) { | |
return y*labWidth+x; | |
} | |
boolean isLegalIndex(int x, int y) { | |
boolean withinX = x >= 0 && x < labWidth; | |
boolean withinY = y >= 0 && y < labHeight; | |
return withinX && withinY; | |
} | |
float getSquareWidth() { | |
return squareWidth; | |
} | |
void display() { | |
for(int i = 0; i < squares.length; i++) { | |
int x = i % labWidth; | |
int y = (int)(i / labWidth); //cast required for javascript version | |
squares[i].display(x,y,squareWidth); | |
} | |
} | |
} | |
class Player { | |
Labyrinth world; | |
int x,y; | |
Player(int x, int y, Labyrinth world) { | |
this.x = x; | |
this.y = y; | |
this.world = world; | |
} | |
void onKeyPressed() { | |
int nx = x; | |
int ny = y; | |
if (key == 'w') { | |
ny--; | |
} else if (key == 'a') { | |
nx--; | |
} else if (key == 's') { | |
ny++; | |
} else if (key == 'd') { | |
nx++; | |
} | |
if(!world.isLegalIndex(nx,ny)) return; //reached map end | |
Square next = world.getSquare(nx,ny); | |
if(!next.isNegotiable()) return; //cannot move to field | |
//field is free => move to field | |
x = nx; | |
y = ny; | |
} | |
void display() { | |
float px = world.getSquareWidth()*(x+0.5); | |
float py = world.getSquareWidth()*(y+0.5); | |
fill(0); | |
noStroke(); | |
ellipse(px,py,world.getSquareWidth()*0.8,world.getSquareWidth()*0.8); | |
} | |
Square getSquare() { | |
return world.getSquare(x,y); | |
} | |
} | |
//Steht generated auf true, wird ein Labyrinth zufällig | |
//generiert. Funktioniert im Moment noch nicht ideal. | |
boolean generated = false; | |
int[][] field = { | |
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2}, | |
{0,1,1,1,0,1,1,1,0,0,1,1,0,0,0,0,0,0,0,0}, | |
{0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1}, | |
{0,1,1,1,0,1,1,1,0,1,1,1,0,0,0,0,0,0,0,1}, | |
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,1,0,1}, | |
{0,1,1,1,0,1,1,1,0,1,1,1,0,0,0,1,0,1,0,1}, | |
{0,1,1,1,0,1,1,1,0,1,0,0,0,1,1,1,0,1,0,1}, | |
{0,0,0,0,0,1,1,1,1,1,0,1,0,1,1,1,0,1,1,1}, | |
{0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,0,1,1,1}, | |
{0,1,1,1,0,0,1,1,1,1,0,1,0,1,1,1,0,1,0,1}, | |
{0,1,1,1,1,0,0,0,0,0,0,1,0,1,1,1,0,1,0,1}, | |
{0,0,0,1,1,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1}, | |
{0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,0,0,0,1}, | |
{0,1,1,1,1,1,0,1,1,1,0,0,0,1,0,1,0,1,1,1}, | |
{0,0,0,0,0,1,0,1,1,1,0,1,1,1,0,1,0,1,1,0}, | |
{1,1,1,1,1,1,0,1,1,1,0,1,1,0,0,1,0,1,1,0}, | |
{1,1,0,0,0,0,0,1,1,1,0,1,1,0,1,1,0,0,0,0}, | |
{1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0}, | |
{1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0}, | |
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0}, | |
}; | |
int WALL = 1; //Code für eine Wand im Spielfeld-Array | |
int GOAL = 2; //Code für das Ziel im Spielfeld-Array | |
int FREE = 0; //Code für eine freie Stelle im Spielfeld-Array | |
Labyrinth lab; | |
Player player; | |
void setup() { | |
size(400,400); | |
lab = new Labyrinth(20,20,20); | |
if (generated) { | |
lab.generateCorridors(0.1,0.15); | |
} else { | |
for(int y = 0; y < field.length; y++) { | |
for(int x = 0; x < field[y].length; x++) { | |
lab.setSquare(x,y,new Square(field[y][x] != WALL,field[y][x] == GOAL)); | |
} | |
} | |
} | |
player = generated ? new Player(10,10,lab) : new Player(0,0,lab); | |
} | |
void draw() { | |
background(0); | |
lab.display(); | |
player.display(); | |
if(player.getSquare().isExit()) gameOver(); | |
} | |
void gameOver() { | |
background(0); | |
textAlign(CENTER); | |
textSize(30); | |
fill(255); | |
text("Du bist entkommen!",width/2.0,height/2.0); | |
noLoop(); | |
} | |
void keyPressed() { | |
player.onKeyPressed(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment