here is my version
-
you can edit the map under the image (with the mouse) and save and load (s and l)
-
and hide / show grid with q
-
mover player with cursor
// https://discourse.processing.org/t/collision-detection-with-walls-and-objects/25856
Player player;
PImage level0;
int [][] myMap;
boolean showMap=true;
// mouse hold to edit grid
boolean hold;
void setup() {
size(900, 900);
level0 = loadImage("Capture.jpg");
println(level0.width) ;
println(level0.height) ;
// init myMap
myMap = new int [level0.width/10+1][level0.height/10];
for (int x = 0; x < myMap.length; x++) {
for (int y = 0; y < myMap[0].length; y++) {
myMap[x][y]=0;
}
}
println(myMap.length);
println(myMap[0].length);
player = new Player(300, 300);
}
void draw() {
background(0);
image(level0,
0, 0);
if (showMap) {
for (int x = 0; x < myMap.length; x++) {
for (int y = 0; y < myMap[0].length; y++) {
fill(255);
text( myMap[x][y], x*10, y*10+13);
if (hold) {
if (mouseX>= x*10 &&
mouseX<= (x+1)*10 &&
mouseY>= (y-1)*10+13 &&
mouseY<= (y)*10+13) {
if (keyPressed)
myMap[x][y]=0;
else
myMap[x][y]=1;
}
}
}
}
}
player.draw();
player.update();
String help="Help for Game\n\nThe walls are 1, ways are 0 \nl - load \ns - save\nq - show grid on /off\nMouse to set grid to 1 (save when you are ready!)\n click mouse plus hold key gives 0 \nplay player with cursor keys ";
fill(255);
text(help,
level0.width+110, 44);
}
//-------------------------------------------------------------------------------
void keyPressed() {
switch(key) {
case's':
//save
String[] s1 = new String [myMap[0].length];
int i=0;
// empty s1
for (String dummyString : s1) {
s1[i]="";
i++;
}
// make s1 from myMap
for (int y = 0; y < myMap[0].length; y++) {
for (int x = 0; x < myMap.length; x++) {
s1[ y ] += myMap[x][y];
}
}
saveStrings( "level0.txt", s1);
println("Saved");
break;
case 'l':
//load
// empty myMap
for (int x = 0; x < myMap.length; x++) {
for (int y = 0; y < myMap[0].length; y++) {
myMap[x][y]=0;
}
}
String[] sLoad = loadStrings( "level0.txt" );
// printArray(sLoad);
// fill myMap
for (int y = 0; y < myMap[0].length; y++) {
for (int x = 0; x < sLoad[y].length(); x++) {
myMap[x][y] = int( sLoad[y].charAt(x)+"" );
}
}
break;
case 'q':
//toggle
showMap=
! showMap;
break;
}//switch
//
}
void mousePressed() {
hold=true;
}
void mouseReleased() {
hold=false;
}
// ============================================================
class Player {
float x, y;
boolean collide;
color col = color(255);
Player(float x, float y) {
this.x = x;
this.y = y;
}
void draw() {
fill(255, 0, 0);
rect(x, y, 20, 40);
}
void update() {
int posInMyMapX=int(x/10);
int posInMyMapY=int(y/10);
fill(255, 0, 0); // RED
// myMap[posInMyMapX][posInMyMapY]=9;
text( myMap[posInMyMapX][posInMyMapY],
x*10, y*10+13);
if (keyPressed&&keyCode==LEFT) {
posInMyMapX=int((x-5)/10);
posInMyMapY=int(y/10);
if (myMap[posInMyMapX][posInMyMapY] == 0)
x-=5;
}
if (keyPressed&&keyCode==RIGHT) {
posInMyMapX=int((x+5)/10);
posInMyMapY=int(y/10);
if (myMap[posInMyMapX][posInMyMapY] == 0)
x+=5;
}
if (keyPressed&&keyCode==UP) {
posInMyMapX=int((x)/10);
posInMyMapY=int((y-5)/10);
if (myMap[posInMyMapX][posInMyMapY] == 0)
y-=5;
}
if (keyPressed&&keyCode==DOWN) {
posInMyMapX=int((x)/10);
posInMyMapY=int((y+5)/10);
if (myMap[posInMyMapX][posInMyMapY] == 0)
y+=5;
}
}//method
//
}//class
//
here is the level
111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111111
100000000000000000000010000000000000000000000000111
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100111111111111111111110000000000000000000000000011
100111111111111111111110000000000000000000000000011
100111111111111111111110000000000000000000000001111
100111111111111111111110000000000000000000001111011
100111111111111111111110000000000000000000011111111
100111111111111111111110000000000000000000011111111
100000000000000000000000000000000000000000011110011
100000000000000000000000000000000000000000011110011
100000000000000000000000000000000000000000111000011
100111111111111111111110000000000000000000000000001
100111111111111111111110000000000000000000000000001
100111111111111111111110000000000000000000000000001
100111111111111111111110000000000000000000000000001
100111111111111111111110000000000000000000000000001
100111111111111111111110000000000000000000000000001
100000000000000000000010000000000000000000000000001
100000000000000000000010000000000000000000000000001
100000000000000000000010000000000000000000011111001
100000000000000000000010000000000000000000011111001
100000000000000000000010000000000000000000000000001
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
100000000000000000000010000000000000000000000000011
111111111111111111111111111111000000000000000000011
111111111111111111111111111111111111111111111111111
Chrisir