Rendering type on a grid derived from key input
in
Programming Questions
•
11 months ago
I am trying to have user key input written to a 2d array, and render type on screen based on the contents of that array. The idea is that the contents of the array can be modified later, and the screen will update, so it is dynamic. If you run my code, it will make much more sense. If you run it, you will see that there are obvious issues, but I think I could probably approach the problem in a fundamentally different way...I just don't know how. Any advice at all would be really appreciated; I am somewhat new to programming.
I know my code is probably kind of weird...I tried to clean it up a bit before posting it though. :)
- int rendlocx = 80;
- int rendlocy = 40;
- int gridx = 0;
- int gridy = 0;
- char letter;
- char[][] grid;
- int cols = 2;
- int rows = 255;
- boolean gate = false;
- boolean keygate = false;
- PFont myFont;
- void setup() {
- size (330, 760);
- myFont = createFont("Courier New", 85); // initializing font shit
- textFont(myFont);
- grid = new char[cols+1][rows+1];
- println("gridx is " + gridx);
- println("gridy is " + gridy);
- println("-----------");
- println(Character.toString(grid[0][0]) + ' ' + Character.toString(grid[1][0]) + ' ' + Character.toString(grid[2][0]));
- println(Character.toString(grid[0][1]) + ' ' + Character.toString(grid[1][1]) + ' ' + Character.toString(grid[2][1]));
- println(Character.toString(grid[0][2]) + ' ' + Character.toString(grid[1][2]) + ' ' + Character.toString(grid[2][2]));
- println(Character.toString(grid[0][3]) + ' ' + Character.toString(grid[1][3]) + ' ' + Character.toString(grid[2][3]));
- }
- void draw() {
- if(keygate == true){
- keygate = false;
- letter = key;
- if ((letter >= 'A' && letter <= 'z') || letter == ' ' || letter >= '0' && letter <= '9') { //If key pressed is a character, write it
- grid[gridx][gridy] = letter;
- gridx = gridx + 1;
- gate = true;
- }
- }
- if (gate == true) {
- // Two nested loops allow us to visit every spot in a 2D array.
- // For every column I, visit every row J.
- for (int i = 0; i < cols; i++) {
- for (int j = 0; j < rows; j++) {
- text(Character.toString(grid[i][j]), rendlocx, rendlocy + 40);
- }
- }
- gate = false;
- rendlocx = rendlocx + 60;
- if (gridx >= 3) { //If line reaches 3, reset line
- gridx = 0;
- gridy = gridy+1;
- rendlocx = 80;
- rendlocy = rendlocy + 70;
- }
- println("-----------"); //all this shit is to inform you in real time
- println("gridx is " + gridx);
- println("gridy is " + gridy);
- println("You typed: " + key);
- println("letter is " + letter);
- println("-----------");
- println(Character.toString(grid[0][0]) + ' ' + Character.toString(grid[1][0]) + ' ' + Character.toString(grid[2][0]));
- println(Character.toString(grid[0][1]) + ' ' + Character.toString(grid[1][1]) + ' ' + Character.toString(grid[2][1]));
- println(Character.toString(grid[0][2]) + ' ' + Character.toString(grid[1][2]) + ' ' + Character.toString(grid[2][2]));
- println(Character.toString(grid[0][3]) + ' ' + Character.toString(grid[1][3]) + ' ' + Character.toString(grid[2][3]));
- }
- }
- void keyPressed() {
- keygate = true;
- }
1