Assignment 4: Cards For Humanity

For this assignment, I wanted to create a generative text output. I was thinking of games that play with words and phrases and I immediately thought of Cards Against Humanity. So I wanted to create a PG version that just uses playful humor and thus called it “Cards For Humanity”:)

The way my game is played is that the user first clicks on the deck of black cards and reads the prompt. Then, they will click on the deck of white cards and read the comical response. They can then click the button below to try multiple rounds. It is based on the party game below:
The part of my code that I want to highlight shows how I fit the varying text within the card. Since I didn’t want to limit the length of the text or the size of the font, I could only make the text fit by separating it into different lines. Words are added to the same line until it exceeds the max length.

function drawSelectedCard(x, y, cardText, isBlack) {
  fill(isBlack ? 0 : 255); //checks if card is from black deck and assigns color
  rect(x, y, cardWidth, cardHeight, 10);

  fill(isBlack ? 255 : 0); //text color opposite to card

  // Split the card text into lines to fit within the card
  let lines = splitLines(cardText, 70); //Maximum characters per line set to 70

  // Display each line of text within the card
  for (let i = 0; i < lines.length; i++) {
    text(lines[i], x + cardWidth / 2, y + cardHeight / 2 - (lines.length - 1) * 10 + i * 20); //vertical centering
  }
}

function splitLines(text, maxLength) {
  let words = text.split(' '); //split text into array of words
  let lines = [];
  let currentLine = '';

  for (let i = 0; i < words.length; i++) {
    if (textWidth(currentLine + words[i]) <= maxLength) { //checks if adding the current word to the current line exceeds the maximum length
      currentLine += words[i] + ' '; }
    else {
      //if the max length is exceeded, push the current line to the array and start a new line
      lines.push(currentLine);
      currentLine = words[i] + ' ';
    }
  }

  lines.push(currentLine); //push the last line to the array
  return lines; 
}

Overall, I am happy with the output. I would like to work further on this by adding more amusing prompts and responses. I would also like to improve the game by letting the user choose the best response from their set of already-selected white cards, like the actual game. Lastly, I could organize my code better by using OOP and making the cards objects.

Leave a Reply