moving the checker board pieces. Need help asap
in
Programming Questions
•
1 years ago
I have a checker board that i would like to the following. I just stuck and i don't wanna spend more time thinking about. I would appreciate any help.
a. Make the cells toggle between moving and not as the user clicks. First click = move; second click = stop; third click = move; fourth click = stop; etc.
b. Make the cells bounce off the edges of the canvas instead of sailing off into infinity.
c. Note that some cells never move. Fix that, so that all cells move.
here is the code
a. Make the cells toggle between moving and not as the user clicks. First click = move; second click = stop; third click = move; fourth click = stop; etc.
b. Make the cells bounce off the edges of the canvas instead of sailing off into infinity.
c. Note that some cells never move. Fix that, so that all cells move.
here is the code
- // ----------------------------------------------------------------------
- // CUSTOM CLASSES
- // ----------------------------------------------------------------------
- //CheckerCell is a class that draws a black or white rectangle, or cell
- class CheckerCell {
- /* properties */
- int x; // x-coordinate of the cell
- int y; // y-coordinate of the cell
- int mySize = 20; //the size of the cell
- color fillColor;
- color WHITE = color(255); //a constant property
- color BLACK = color(0); //a constant property
- float xSpeed; //how many pixels and in which direction (positive or negative) the cell will move in the x axis
- float ySpeed; //how many pixels and in which direction (positive or negative) the cell will move in the y axis
- /* methods */
- // constructor: simply sets the speeds to random amounts
- public CheckerCell() {
- xSpeed = (random(5))*randomPosNeg();
- ySpeed = (random(5))*randomPosNeg();
- }
- //moves the cell based on the values for xSpeed and ySpeed
- //does edge checks to 'bounce' the cell off the edge of the canvas
- void move() {
- x = x + int(xSpeed); //apply proper xSpeed to move in the x axis
- y = y + int(ySpeed); //apply proper ySpeed to move in the y axis.
- }
- // draws the cell
- void display() {
- fill(fillColor);
- //drawing the stroke in contrast makes for better visibility when they start moving
- if (fillColor == WHITE) {
- stroke(BLACK);
- }
- else {
- stroke(WHITE);
- }
- rect(x, y, mySize, mySize);
- }
- //switches the fill from white, if black, and vice-versa
- void switchFill(boolean newFill) {
- if (newFill) {
- fillColor = color(255);
- }
- else {
- fillColor = color(0);
- }
- }
- //set the fillColor property
- void set(color newFill) {
- fillColor = newFill;
- }
- }
- // ----------------------------------------------------------------------
- // GLOBAL VARIABLES
- // ----------------------------------------------------------------------
- //the size of our cells
- int cellSize = 20;
- //flags whether the cells are in motion or not
- boolean moving = false;
- boolean firstClick;
- boolean secondClick;
- boolean thirdClick;
- boolean fourthClick;
- //A 2-dimensional array for holding the cells of the checkerboard
- CheckerCell[][] cellArray;
- // ----------------------------------------------------------------------
- // BUILT-IN FUNCTIONS
- // ----------------------------------------------------------------------
- void setup() {
- size(500, 500);
- smooth();
- noStroke();
- //populate our board with cells
- initializeBoard();
- }
- void mousePressed() {
- //turn on the cell movement
- // moving = true;
-
- }
- void draw() {
- //refresh the background; this is an animation, after all
- background(0);
- //loop through the array, drawing the CheckerCells
- //first, walk across the columns...
- for (int column = 0; column < cellArray.length; column++) {
- //...for each column, walk down the rows
- for (int row = 0; row < cellArray[0].length; row++) {
- //tell each cell to draw itself
- cellArray[column][row].display();
- if (moving) {
- //if moving, tell each cell to move
- cellArray[column][row].move();
- }
- }
- }
- }
- // ----------------------------------------------------------------------
- // CUSTOM FUNCTIONS
- // ----------------------------------------------------------------------
- //Walk through the 2-D cellArray, filling it with CheckerCell objects with the correct fill to make a
- //checkerboard pattern
- void initializeBoard() {
- boolean whiteColor = true; //if true, then fill = white; if false, fill = black
- boolean startColor = true; //used to track the starting fill value for each column
- int numColumns = width/cellSize; //the number of columns is a function of the width of the canvas & the size of each cell
- int numRows = height/cellSize; //the number of rows is a function of the height of the canvas & the size of each cell
- //initialize the 2 dimensional array for holding all the cells
- cellArray = new CheckerCell[numColumns][numRows];
- //first, walk across the columns...
- for (int column = 0; column < cellArray.length; column++) {
- //...for each column, walk down the rows
- for (int row = 0; row < cellArray[column].length; row++) {
- cellArray[column][row] = new CheckerCell(); //make a new CheckerCell object
- cellArray[column][row].x = column*cellSize; //the x position of each cell is a function of which column it is in and the cell size
- cellArray[column][row].y = row*cellSize; //the x position of each cell is a function of which column it is in and the cell size
- cellArray[column][row].switchFill(whiteColor);
- whiteColor = !whiteColor;//alternate the fill color between white and black
- }
- //at bottom of column, flip the value of startColor to make sure the next colum
- //is filled with the opposite fill of the current column
- startColor = !startColor;
- //then set our fill color to that value
- whiteColor = startColor;
- }
- }
- int randomPosNeg() {
- float temp = random(0,1);
- if (temp > 0.5) {
- return 1;
- } else {
- return -1;
- }
- }
1