Processing 3.3.6 - Printing letters when they are hit by a circle

Hello, very new programmer needing help with printing a letter(s) at the bottom of the window.

The premise of the program is this

1) User controls a "gun" that moves left to right with arrow keys, and shoots a stream of bullets with mouse press. At the top of the window, there is an alphabet looping from left to right.

2) What I need help with: When the stream of circles touches a letter at the top, the letter that was "hit" will appear at the bottom of the window. Each successive letter hit will appear next to one before.

How should I do this? The way I have it right now, I am making the letters with a for loop, and it is made with a string, so I'm not sure how I would use something like charAt without turning it into a string.

Here is my code. Any help is greatly appreciated, thank you!

float windowHeight;

float windowY;

// shooter

float shooterSize;

float shooterX;

float shooterY;

// shooter tip
float shooterTriX1 = shooterX;
float shooterTriY1 = shooterY;
float shooterTriX2 = shooterX + shooterSize / 2;
float shooterTriY2 = shooterY - shooterSize;
float shooterTriX3 = shooterX + shooterSize;
float shooterTriY3 = shooterY;
// shooter speed
final float shooter_SPEED = 2.0;
//colors
final int BLACK = 0;
final int WHITE = 255;


// bullets
int bullets = 1;
float BY;
float BX;
int bulletSize = width/12;
final float BULLET_SPACING = 50;
final float BULLET_SPEED = 4.0;
boolean shoot = false;
float nextBullet;

// chars
char char1 = 'Z';
char nextchar;
float charX;
float charMoveSpeed = 2;
float charSpacing;
float ALPHABET = 26;

void printHitLetters() {
line(0,windowY, width, windowY);

void drawchars() {
float charX2 = charX;

for(int i = 0; i < ALPHABET; i++) {
nextchar = char(char1 - i);
textSize(charSpacing);
fill(BLACK);
// reset alphabet
text(nextchar, (charX2 - (charSpacing * i)) % (ALPHABET * charSpacing) - charSpacing, charSpacing);
}

void movechars() {
charX += charMoveSpeed;

}


void moveshooter() {
if (keyPressed) {
if ( keyCode == LEFT ) {
shooterX -= shooter_SPEED;
if ( shooterX < 0 ) {
// reset
shooterX = max(shooterX, 0);
}
} // LEFT MOVEMENT

else if ( keyCode == RIGHT ) {
shooterX += shooter_SPEED;
if (shooterX > (width - shooterSize)) {
// reset
shooterX = min(width - shooterSize, shooterX);
}
} // RIGHT MOVEMENT

} // KEY PRESSED

} // move shooter

void drawshooter() {
shooterTriX1 = shooterX;
shooterTriY1 = shooterY;
shooterTriX2 = shooterX + shooterSize / 2;
shooterTriY2 = shooterY - shooterSize;
shooterTriX3 = shooterX + shooterSize;
shooterTriY3 = shooterY;
fill(255);
stroke(BLACK);
rect(shooterX, shooterY, shooterSize, shooterSize);
triangle(shooterTriX1, shooterTriY1, shooterTriX2, shooterTriY2, shooterTriX3, shooterTriY3);
} // draw shooter

void mousePressed() {
if (!shoot) {
BX = shooterTriX2;
BY = shooterTriY2;
} // set bullet position
shoot = true;
} // mouse pressed

void drawShot(){

if (shoot) {
int i = 0;
while (i < 10) {
nextBullet = min((BY + (i * BULLET_SPACING)), shooterTriY2);
fill(BLACK);
stroke(BLACK);
ellipse(BX, BY, bulletSize, bulletSize);
ellipse(BX, nextBullet, bulletSize, bulletSize);
i++;
} // while loop to draw bullets

if (BY < 0) {
shoot = false;
} // stop bullets when top of window is hit

} // if there is shooting

} // draw shot function

void moveShot(){

if (shoot && BY >= 0) {
BY -= BULLET_SPEED;
} // move bullets
} // move bullets function

void setup() {
size(700,700);
windowHeight = height/12;
windowY = height - windowHeight;
shooterSize = width / 15;
shooterX = (width / 2) - (shooterSize / 2);
shooterY = height - (height / 15) - windowHeight;
bulletSize = width/50;
charSpacing = width/25;
charX = -charSpacing;
} // setup


void draw() {
background(255);
printHitLetters();
movechars();
drawchars();
moveshooter();
drawshooter();
drawShot();
moveShot();

} // draw

[original post vandalised, restored from backup. might not be exact.]

Answers

  • Answer ✓

    This next code is by no means complete but shows a slightly different approach only for generating the letters. Check it out and if you like what you see, then follow the next steps:

    1. Create a class similar to LetterContainer but describing the bullets.
    2. Draw the bullets
    3. Check one a bullet hit a letter.You need to study collision examples (Not difficult, you just need to detect when a collision between a bullet and a letter happens

    I commented some code in your original code and in your new code. You might want to check the commented code in the new code or you can see the original code here: https://forum.processing.org/two/discussion/26724/how-can-i-get-multiple-iterations-of-the-same-object-to-independently-change-colour#latest

    Kf

    float windowHeight;
    
    float windowY;
    
    // shooter
    
    float shooterSize;
    
    float shooterX;
    
    float shooterY;
    
    // shooter tip
    float shooterTriX1 = shooterX;
    float shooterTriY1 = shooterY;
    float shooterTriX2 = shooterX + shooterSize / 2;
    float shooterTriY2 = shooterY - shooterSize;
    float shooterTriX3 = shooterX + shooterSize;
    float shooterTriY3 = shooterY;
    // shooter speed
    final float shooter_SPEED = 2.0;
    //colors
    final int BLACK = 0;
    final int WHITE = 255;
    
    
    // bullets
    int bullets = 1;
    float BY;
    float BX;
    int bulletSize = width/12;
    final float BULLET_SPACING = 50;
    final float BULLET_SPEED = 4.0;
    boolean shoot = false;
    float nextBullet;
    
    // chars
    char char1 = 'Z';
    char nextchar;
    float charX;
    float charMoveSpeed = 2;
    float charSpacing;
    float ALPHABET = 26;
    
    
    ArrayList<LetterContainer> con;
    int ctr=0;
    
    void setup() {
      size(700, 700);
      textAlign(CENTER,CENTER);
      rectMode(CENTER);
    
      windowHeight = height/12;
      windowY = height - windowHeight;
      shooterSize = width / 15;
      shooterX = (width / 2) - (shooterSize / 2);
      shooterY = height - (height / 15) - windowHeight;
      bulletSize = width/50;
      charSpacing = width/25;
      charX = -charSpacing;
    
      con=new ArrayList<LetterContainer>();
    
    
    } // setup
    
    
    void draw() { 
      background(255);
    
      //Add one letter every 30 frames
      if(frameCount%30==0)
      con.add(new LetterContainer(ctr++));
    
      //Update pos and display letter
      for(LetterContainer let:con){
        let.update();
      }
    
      //remove dead letters (when they have reach the end of the screen on the right)
      for(int i=con.size()-1; i>=0;i--){
    
        if(con.get(i).done)
        con.remove(con.get(i));
    
      }
    
    
      //printHitLetters();
      //movechars();
      //drawchars();
      moveshooter();
      drawshooter();
      drawShot();
      moveShot();
    }
    
    
    
    
    
    //void printHitLetters() {
    //  line(0, windowY, width, windowY);
    //}
    
    //void drawchars() {
    //  float charX2 = charX; 
    
    //  for (int i = 0; i < ALPHABET; i++) { 
    //    nextchar = char(char1 - i);
    //    textSize(charSpacing);    
    //    fill(BLACK);
    //    // reset alphabet
    //    text(nextchar, (charX2 - (charSpacing * i)) % (ALPHABET * charSpacing) - charSpacing, charSpacing);
    //  }
    //}
    
    //void movechars() {
    //  charX += charMoveSpeed;
    //}
    
    
    void moveshooter() {
      if (keyPressed) {
        if ( keyCode == LEFT ) {
          shooterX -= shooter_SPEED;
          if ( shooterX < 0 ) {
            // reset
            shooterX = max(shooterX, 0);
          }
        } // LEFT MOVEMENT
    
        else if ( keyCode == RIGHT ) {
          shooterX += shooter_SPEED;
          if (shooterX > (width - shooterSize)) {
            // reset
            shooterX = min(width - shooterSize, shooterX);
          }
        } // RIGHT MOVEMENT
      } // KEY PRESSED
    } // move shooter
    
    void drawshooter() {
      shooterTriX1 = shooterX;
      shooterTriY1 = shooterY;
      shooterTriX2 = shooterX + shooterSize / 2;
      shooterTriY2 = shooterY - shooterSize;
      shooterTriX3 = shooterX + shooterSize;
      shooterTriY3 = shooterY;
      fill(255);
      stroke(BLACK);
      rect(shooterX+shooterSize/2, shooterY+shooterSize/2, shooterSize, shooterSize);
      triangle(shooterTriX1, shooterTriY1, shooterTriX2, shooterTriY2, shooterTriX3, shooterTriY3);
    } // draw shooter
    
    void mousePressed() {
      if (!shoot) {
        BX = shooterTriX2; 
        BY = shooterTriY2;
      } // set bullet position
      shoot = true;
    } // mouse pressed
    
    void drawShot() {
    
      if (shoot) {
        int i = 0;
        while (i < 10) {
          nextBullet = min((BY + (i * BULLET_SPACING)), shooterTriY2);
          fill(BLACK);
          stroke(BLACK);
          ellipse(BX, BY, bulletSize, bulletSize);
          ellipse(BX, nextBullet, bulletSize, bulletSize);
          i++;
        } // while loop to draw bullets
    
        if (BY < 0) {
          shoot = false;
        } // stop bullets when top of window is hit
      } // if there is shooting
    } // draw shot function
    
    void moveShot() {
    
      if (shoot && BY >= 0) {
        BY -= BULLET_SPEED;
      } // move bullets
    } // move bullets function
    
    
    
    
    class LetterContainer {
    
      final int LETTERS_IN_ALPHABET=26;
    
      //length and height of the squares
      float w = 30;
      float h = 30;
      color c;
      String letter;
      boolean done;
    
      //x and y locations of the squares
      float xPos;
      float yPos;
    
      //direction and velocity of squares
      //(negatives determine which direction)
      float xDirection;
      float yDirection;
    
      //CONSTRUCTOR
      LetterContainer(int n) {
        xPos = w;//random(0, width-w);
        yPos = h/2;;
    
        xDirection = 1;//random(-1, 1);
        yDirection = 0;//random(-1, 1);
    
        c=color(250,150,25);
        done=false;
    
        letter=str(char('A'+n%LETTERS_IN_ALPHABET));
      }
    
      void changeColor() {
        c=color(random(50, 255), random(0, 255), random(200, 255));
      }
    
    
      void update() {
    
    
        //updating the original position by the user-defined
        //distance or travel (speed)
        xPos = xPos + xDirection;
        yPos = yPos + yDirection;
    
        //telling the square to reverse its x or y value
        //if its edge collides with the edge of the screen
        //and change colour when it does
        if (xPos > width-w)
        {
          //xDirection = -1;
          //changeColor();
          done=true;
        }
    
     //   if (xPos < 0)
     //   {
     //     xDirection = 1;
     //     changeColor();
     //   }
    
     //   if (yPos > height-w)
     //   {
     //     yDirection = -.5;
     //     changeColor();
     //   }
    
     //   if (yPos < 0)
     //   {
     //     yDirection = .5;
     //     changeColor();
     //   }
    
        fill(c);
        rect (xPos, yPos, w,h);
        fill(0);
        text(letter,xPos,yPos);
      }
    }
    
  • Thanks Kf! really appreciate the help!

  • the original poster has replaced the question with another, completely unrelated. which is why the answer seems completely unrelated. i'm going to close this and try and restore the original.

This discussion has been closed.