We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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
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:
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
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.