Categories

Interactive Programming: Grid Drawing

For homework on 8/1/2018 I already knew how interactive functions such as mouseClicked() worked. So I spoke to my faculty member teaching code (Jackie Wu) and she allowed me to work on a project towards my final. For my final one of the portions will have a processing program that allows users to make tetrominoes. So I created a grid that the user can move a selector box around and then fill by pressing the “k” key.

Here is my code:

int windowScale = 24;
int cols, rows;
int x = 0;
int y = 0;
int colPos = 0;
int rowPos = 0;
int paintBoxX = 0;
int paintBoxY = 0;
boolean yesPress = false;

void setup(){
 size(1920, 1440);
 background(0);
 
 //Size the cells of the grid based off of the window size
 cols = width;
 rows = height;
 
 //Loop for columns 
 for (int i = 0; i < cols; i = i+windowScale){
 //Loop for rows 
 for (int j = 0; j < rows; j = j+windowScale){
 


block(i, j, false);
 }
 }
}

void block(int colPos, int rowPos, boolean isFilled){
 //color grid squares 
 if (isFilled == false){
 fill(255);
 }else{
 fill(0);
 }
 stroke(0);
 rect(colPos, rowPos, windowScale, windowScale);
 println("isFilled ==" + isFilled);
}

void draw(){
 
 
 
 //area check for the movement of the paintbox selector
 
 if (paintBoxX < 0){
 paintBoxX = 0;
 }
 
 if (paintBoxX > 1920){
 paintBoxX = 1920;
 }
 
 if (paintBoxY < 0){
 paintBoxY = 0; 
 }
 
 if (paintBoxY > 1440){
 paintBoxY = 1440; 
 }
 
 
 //display the paintbox
 stroke (255, 0, 0);
 rect(paintBoxX, paintBoxY, windowScale, windowScale);
 
 
 
}

void keyPressed(){
 //movement of the paintbox
 
 
 if (yesPress == false){
 
 
 if (key == CODED){
 if (keyCode == RIGHT){
 stroke(0);
 rect(paintBoxX, paintBoxY, windowScale, windowScale);
 paintBoxX += windowScale;
 yesPress = true;
 println("I'm printing!");
 }
 if (keyCode == LEFT){
 stroke(0);
 rect(paintBoxX, paintBoxY, windowScale, windowScale);
 paintBoxX -= windowScale;
 yesPress = true;
 }
 if (keyCode == UP){
 stroke(0);
 rect(paintBoxX, paintBoxY, windowScale, windowScale);
 paintBoxY -= windowScale;
 yesPress = true;
 }
 if (keyCode == DOWN){
 stroke(0);
 rect(paintBoxX, paintBoxY, windowScale, windowScale);
 paintBoxY += windowScale;
 yesPress = true;
 }
 
 }
 if (key == 'k') {
 block(paintBoxX, paintBoxY, true);
 }else{
 block(paintBoxX, paintBoxY, false);
 }
 }
}

//limits the paintbox to one movement per key press
void keyReleased(){
 yesPress = false;
}

 

 

No Comments

Sorry, the comment form is closed at this time.