-
Notifications
You must be signed in to change notification settings - Fork 2
Week 04: Data
- constructors & inheritance review
- string splitting, using a local vs. external string to store data, loadStrings()
- external data for Breakout with CSV, Table object
- external data workshop
- loading vs. saving data
Remember that classes and objects are constructs defined by you - what information they have and what they do is based on your needs. In the example from class, we explored two possibilities for putting together a constructor for a "Block" object.
In the first possibility, we took for granted that Block objects would be created by iterating through a grid structure, and the data that was relevant for the constructor, the position, would be based on that iteration.
in the Block class:
Block(int i, int j) {
pos = new PVector(i * blockSize, j* blockSize);
}
in the setup() function:
// calling the Block constructor for each object
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
blocks[i][j] = new Block(i, j);
}
}
In this version, the Block constructor takes two integers, then calculates the position based on a blockSize variable.
In the second possibility, we decided that we also wanted to be able to create an object at an arbitrary non-grid position, re-creating one of the objects at the mouse position. This means that we probably wouldn't want to supply the constructor with a row and column and rather set up the constructor to take the actual onscreen pixel values of x and y.
in the Block class:
Block(int x, int y) {
pos = new PVector(x, y);
}
in the setup() function:
// calling the Block constructor for each object
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
blocks[i][j] = new Block(i * blockSize, j * blockSize);
}
}
this means that in mousePressed() we could re-create one of the blocks, using the pixel values of mouseX and mouseY to directly set the position without being constrained by the grid.
void mousePressed() {
blocks[0][0] = new Block(mouseX, mouseY);
}
Try to get a solid grasp on the distinction between these two approaches, and imagine what other kinds of needs might call for a different constructor with different arguments.
In class, we used the String method split() to get data out of an individual string stored in program memory and with loadStrings() to get data out of a string stored in an external file.
// splits string myStr into an array of values { 1, 2, 3, 4, 5 }
String myStr = "1,2,3,4,5"
String[] splitStr = myStr.split(",");
// loads an array of strings from a text file called mydata.txt in the folder
loadStrings('mydata.txt');
If we want to store data externally, strings in a text file is a pretty sloppy way to do things. A CSV is a little better, as we can create one using software like Excel or Google Sheets, and a framework like Processing has objects and methods set up for us to parse those files.
-
Tableobject - a representation of a table loaded into your sketch's memory, with lots of methods and other classes to assist with reading the data. -
loadTable(),saveTable()- loads and saves, respectively, a csv file from a path relative to the sketch folder. -
getRow()- get a row by the index of the row in the table (0, 1, 2). The row is represented by its own object, aTableRow. -
getInt(),getFloat(),getString(), etc. - get a value out of aTableRowby type of value. The argument is the index of the column or by the column name (e.g. "x", "y", "w", "h"), if labeled and set up with the "header" argument inloadTable().
See the Processing reference for complete information: https://processing.org/reference/Table.html
Starting point code is here: https://github.com/whoisbma/Code_2_SP19/tree/master/Week_04
Submit your work via git/github and canvas as per usual please.
-
w04_00_loadStrings is the code from class that we used to load strings from a .txt file. Change the code to create an array of objects instead of only drawing ellipses. Each class should have a constructor that takes the value stored in the string as an argument, for example
myObjects[i] = new myObject(splitData[i]);What the class does with that information is up to you. Instead of drawing ellipses it could be anything you like, as long as you use the data supplied in the constructor in some way. -
w04_01_usingData is the same kind of example as above, except it stores data in memory as an array. Do the same as above, using the values to create an array of objects, but also store the array data externally in a csv format. Pull the data out from the csv to supply to your objects in their constructor, same as above.
-
w04_02_saveTable is the example from class, where we are both loading and saving CSV data. In
setup(), ellipses are drawn based on values from the CSV. InmousePressed(), a new ellipse is drawn and the csv is saved with new data. During class you were asked to add a color field to the csv and had the ellipses display those colors. If you didn't finish it in class, do it now. -
There are a few steps to this task - try to finish and submit at least the first two to complete the assignment.
- w04_03_blockLevels is the example we continued with from the inheritance example. Notice the use of arrays to store "level" data. Convert this data to use CSV.
- Apply this general approach to the block breaker game code from last week. Have the finished block breaker code use CSV data to determine some aspect of the blocks (for example color or size). You don't need to use child block classes, you could just change a variable of the standard block class if you wish.
- If you succeed with the previous, try creating multiple 'levels' of the block breaker game. Try making the levels change from one to another based on some criteria you set.