Must rect() return void?
in
Programming Questions
•
7 months ago
In my quest to make a simple roguelike, I now have 2 very rudimentary sketches. 1 makes random rectangles appear on the screen, and one lets a player move @ around the screen. Now I would like to work on having the random rectangles not overlap, and @ not be able to move out of the rectangles.
So, I am wondering if the shape functions keep track of their coordinates, and whether this can be accessed somehow. I.e., if I make a line from x1 --> x10, can I get the line function to return the xs 1 --> 10. Otherwise, I plan on making an array of the coordinates from the x,y,w,h arguments I supply when the rectangles are formed (the rectangles are currently room objects). This would be redundant if I could just use the information from the rect() function (if it exists), e.g., dump the perimeter of the rectangle into a list which would become a part of the particular room object. Then, I when @ moves to cross the boundary of the room object, I could check the x,y, first. I also plan to use this data to prevent the rooms from overlapping (as they currently do, since they are supplied random x, y coords). Below are the two sketches (very basic, but they do what I ask, and I understand the code contained).
So, I am wondering if the shape functions keep track of their coordinates, and whether this can be accessed somehow. I.e., if I make a line from x1 --> x10, can I get the line function to return the xs 1 --> 10. Otherwise, I plan on making an array of the coordinates from the x,y,w,h arguments I supply when the rectangles are formed (the rectangles are currently room objects). This would be redundant if I could just use the information from the rect() function (if it exists), e.g., dump the perimeter of the rectangle into a list which would become a part of the particular room object. Then, I when @ moves to cross the boundary of the room object, I could check the x,y, first. I also plan to use this data to prevent the rooms from overlapping (as they currently do, since they are supplied random x, y coords). Below are the two sketches (very basic, but they do what I ask, and I understand the code contained).
- Room[] map; //random rectangles
int roomnumber = 10;
float xloc;
float yloc;
float w;
float h;
String roomname;
void setup() {
size(1000, 1000);
frameRate(50);
map = new Room[10];
for (int i = 0; i < roomnumber; i++) {
xloc = random(width * 0.75);
yloc = random(height * 0.75);
w = random(20, 300);
h = random(20, 300);
roomname = "room" + String.valueOf(i);
map[i] = new Room(roomname, xloc, yloc, w, h);
}
}
void draw() {
background(0);
for (Room room : map) {
room.display();
}
}
class Room {
String name;
float xpos;
float ypos;
float xdim;
float ydim;
Room (String tempname, float tempx, float tempy, float tempxdim, float tempydim) {
name = tempname;
xpos = tempx;
ypos = tempy;
xdim = tempxdim;
ydim = tempydim;
}
void display() {
stroke(255);
fill('.');
rectMode(CENTER);
rect(xpos, ypos, xdim, ydim);
}
} - PFont f; //this is the @ walking
float playerX;
float playerY;
float rectangleX;
float rectangleY;
void setup() {
size(300,300);
f = createFont("Arial",16,true);
frameRate(30);
rectMode(CENTER);
rectangleX = width/2;
rectangleY = height/2;
playerX = rectangleX;
playerY = rectangleY;
}
void draw() {
background(0);
stroke(255);
fill(0);
rect(rectangleX, rectangleY, 100, 100);
textFont(f,16);
fill(255);
text("@", playerX, playerY);
}
void keyPressed() {
if (key == 'd') {
playerX = playerX + 1;
} else if (key == 'a') {
playerX = playerX - 1;
} else if (key == 'w') {
playerY = playerY - 1;
} else if (key == 's') {
playerY = playerY + 1;
}
}
1