Help: Load New Level When Player is Within Specific Area of Pixels
in
Programming Questions
•
10 months ago
I'm very new to Processing and am trying to create a game based off the basic workings of another maze game for a project. Basically, you control a dot through the maze, and when you get to the "end" (or a specific area of pixels), I want another level to load. These levels consist of a collision map that defines the walls and walkable areas, and level image that shows the walls and background image. You can see where the levels are loaded under "voidDrawLevel". If you guys could take a look at my code below and point me in the right direction, it'd be much appreciated.
EDIT: I'm assuming the solution would be if else statements referencing the x & y location.
int x; // player's x-position
int y; // player's y-position
int s; // player's visual size
int half_size; // // half player's visual size
int speed; // the speed you move
boolean started = false;
int levelNum = 0;
// displayed level
PImage level;
PFont f;
// this matrix will have the same dimension as the level image
boolean[][] collisionMap;
void setup() {
f = createFont ("Century Gothic",14,true);
size(600, 600);
background(0);
loadPixels();
// Loop through every pixel
for (int i = 0; i < pixels.length; i++) {
// Pick a random number, 0 to 255
float rand = random(255);
// Create a grayscale color based on random number
color c = color(rand);
// Set pixel at that location to random color
pixels[i] = c;
}
// draw our player from the center
rectMode(CENTER);
// start in the center of the canvas
x = width/2;
y = height/2;
// size of 10, half_size will be 5
s = 10;
half_size = s / 2;
// moving 2 pixels each time we press a key
speed = 6;
drawLevel(1);
drawLevel(2);
drawLevel(3);
}
void drawLevel(int levelNum) {
String levelImage ="";
String levelMap = "";
if (levelNum == 1) {
levelImage = "level1.png";
levelMap = "collisionMap1.png";
}
else if (levelNum == 2) {
levelImage = "level2.jpg";
levelMap = "collisionMap2.png";
}
else if (levelNum == 3) {
levelImage = "level.jpg";
levelMap = "collisionMap.png";
}
// load level image
level = loadImage(levelImage);
// load black & white pixel collision map image
PImage colMapImage = loadImage(levelMap);
// generate our collision map as an two-dimensional boolean array
collisionMap = new boolean[colMapImage.width][colMapImage.height];
// our collision map is encoded only in black & white pixels
// so we get them for comparison
color black = color(0);
color white = color(255);
// go through each row in our collision map image
for (int i = 0; i < colMapImage.width; i++) {
// go through each column in our collision map image
for (int j = 0; j < colMapImage.height; j++) {
// get the color value of the pixel at our current position
color c = colMapImage.get(i, j);
// if the pixel is black
if (c == black) {
// we can't go there
collisionMap[i][j] = false;
}
// if it is white
else if (c == white) {
// it's good to go
collisionMap[i][j] = true;
}
}
}
}
void draw() {
if (started == false) {
String a = "// A Curious Journey //";
String b = "Guide the Curiosity rover to its mission on Mars.";
String c = "Use the arrow keys to navigate the mazes and make it to your objective.";
fill(198,226,255);
textFont(f);
textSize(25);
text(a, 315, 250, 300, 100); // Text wraps within text box
textSize(13);
text(b, 353, 400, 400, 200); // Text wraps within text box
textSize(13);
text(c, 330, 430, 500, 200); // Text wraps within text box
}
else {
// show our level
image(level, 0, 0);
// draw our player at it's current position
fill(255);
noStroke();
rect(x, y, 5, 5);
}
if (levelNum == 1){
drawLevel(1);
} else if (levelNum == 2){
drawLevel(2);
} else if (levelNum == 3){
drawLevel(3);
}
}
/*
* - then check if at this new position, all 4 corners
* of our rectangle would be within our allowed area
* - if that's the case: move
*/
void mousePressed() {
if (started == false) {
started = true;
}
}
void keyPressed() {
// would each corner of the next step be within our boundaries?
// default: no!
boolean up_left = false;
boolean up_right = false;
boolean down_right = false;
boolean down_left = false;
if (keyCode == LEFT) {
// check first if we are still within our canvas
if (x >= half_size + speed)
{
// check all four corners to see if they would be with the allowed area
up_left = collisionMap[x - speed - half_size][y - half_size];
up_right = collisionMap[x - speed + half_size][y - half_size];
down_right = collisionMap[x - speed + half_size][y + half_size];
down_left = collisionMap[x - speed - half_size][y + half_size];
// if that's the case for each corner
if (up_left && up_right && down_right && down_left) {
// move
x -= speed;
}
}
}
// this basically is repeated for all possible ways to go…
if (keyCode == RIGHT) {
if (x <= width - half_size - speed)
{
up_left = collisionMap[x + speed - half_size][y - half_size];
up_right = collisionMap[x + speed + half_size][y - half_size];
down_right = collisionMap[x + speed + half_size][y + half_size];
down_left = collisionMap[x + speed - half_size][y + half_size];
if (up_left && up_right && down_right && down_left) {
x += speed;
}
}
}
if (keyCode == UP) {
if (y >= half_size + speed)
{
up_left = collisionMap[x - half_size][y - speed - half_size];
up_right = collisionMap[x + half_size][y - speed - half_size];
down_right = collisionMap[x + half_size][y - speed + half_size];
down_left = collisionMap[x - half_size][y - speed + half_size];
if (up_left && up_right && down_right && down_left) {
y -= speed;
}
}
}
if (keyCode == DOWN) {
if (y <= height - half_size - speed)
{
up_left = collisionMap[x - half_size][y + speed - half_size];
up_right = collisionMap[x + half_size][y + speed - half_size];
down_right = collisionMap[x + half_size][y + speed + half_size];
down_left = collisionMap[x - half_size][y + speed + half_size];
if (up_left && up_right && down_right && down_left) {
y += speed;
}
}
}
}
1