setup()
setup() runs exactly once when the sketch starts. Use it to initialize your canvas, create all interactive elements (buttons, inputs), and set starting values for game variables. Everything you create here persists until you call resetGame() or the player closes the browser.
function setup() {
// Erstelle den Canvas, der die gesamte Fenstergröße einnimmt
createCanvas(windowWidth, windowHeight);
// Wähle eine zufällige ganze Zahl zwischen 1 und 100
secretNum = floor(random(1, 101));
// Erstelle das Eingabefeld
guessInput = createInput('');
guessInput.attribute('placeholder', 'Gib eine Zahl ein'); // Platzhaltertext
// Positioniere das Eingabefeld in der Mitte des Canvas
guessInput.position(width / 2 - guessInput.width / 2, height / 2 + 50);
// Erstelle den Raten-Button
guessButton = createButton('Raten!');
// Positioniere den Button neben dem Eingabefeld
guessButton.position(guessInput.x + guessInput.width + 10, height / 2 + 50);
guessButton.mousePressed(checkGuess); // Weist die Funktion checkGuess zu, die bei Klick aufgerufen wird
// Erstelle den Neues-Spiel-Button
resetButton = createButton('Neues Spiel');
// Positioniere den Button in der Mitte unter dem Feedback-Text
resetButton.position(width / 2 - resetButton.width / 2, height / 2 + 150);
resetButton.mousePressed(resetGame); // Weist die Funktion resetGame zu
resetButton.hide(); // Verstecke den Button am Anfang
// Zentriere den Text auf dem Canvas
textAlign(CENTER, CENTER);
textSize(24); // Setze die Textgröße
}
Line-by-line explanation (11 lines)
🔧 Subcomponents:
createCanvas(windowWidth, windowHeight);
Creates a canvas that stretches to fill the entire browser window, making the game responsive
secretNum = floor(random(1, 101));
Generates a random integer from 1 to 100 that the player must guess
guessInput = createInput('');
Creates an HTML input field where the player types their guess
guessInput.position(width / 2 - guessInput.width / 2, height / 2 + 50);
Centers the input field horizontally and places it below the middle of the canvas
guessButton.mousePressed(checkGuess);
Connects button clicks to functions that handle the game logic
createCanvas(windowWidth, windowHeight);- Creates a canvas that is exactly as wide and tall as your browser window, so the game fills the screen
secretNum = floor(random(1, 101));- Generates a random number: random(1, 101) picks a decimal between 1 and 101, and floor() rounds it down to an integer from 1 to 100
guessInput = createInput('');- Creates an HTML input box where the player can type a number; the empty string '' means it starts empty
guessInput.attribute('placeholder', 'Gib eine Zahl ein');- Adds placeholder text that appears inside the input box as a hint until the player starts typing
guessInput.position(width / 2 - guessInput.width / 2, height / 2 + 50);- Centers the input box horizontally (width/2 minus half its own width) and places it 50 pixels below the vertical center
guessButton = createButton('Raten!');- Creates a clickable button labeled 'Raten!' (German for 'Guess!')
guessButton.mousePressed(checkGuess);- Tells p5.js to call the checkGuess() function whenever someone clicks the guess button
resetButton.mousePressed(resetGame);- Tells p5.js to call the resetGame() function when the reset button is clicked
resetButton.hide();- Hides the reset button at the start; it will only appear when the game ends
textAlign(CENTER, CENTER);- Centers all text both horizontally and vertically from the point you specify in text() calls
textSize(24);- Sets the font size to 24 pixels for all text drawn after this line