How to make hold Tab appear the scoreboard in front of you in processing?

Viewed 25

The code below defines the placement and resolution of the scoreboard but I can't find out how to make the holding TAB appear the scoreboard:

def setup ():
    fullScreen()
    background(50)
def draw():
    scorebordRectWidth = width / 2
    scorebordRectHeight = height / 2
    scorebordRectX = (width - scorebordRectWidth) / 2
    scorebordRectY = (height - scorebordRectHeight) / 2

    fill(255, 255, 255)
    rect(scorebordRectX, scorebordRectY, scorebordRectWidth, scorebordRectHeight)
1 Answers

The trick is that you have to limit "drawing the scoreboard" to only the time when the TAB key is held down by the user. An easy way to achieve this would be to create a boolean global variable (let's call it showScoreBoard) and update it to True or False whenever the Tab key is pressed or released.

Processing has some helpful documentation that you can read about these operations. Here's a list of the most used tools which you'll need, and here's the keyPressed() and the keyReleased() pages specifically.

I modified your code snippet a little bit, mostly for my own comfort. Here is an example of what you asked for: a "scoreboard" which appears only while holding the TAB key down. Feel free to ask questions if my code is unclear of if there's any part that puzzles you.

# Global variables
showScoreBoard = False
scorebordRectWidth = 0
scorebordRectHeight = 0
scorebordRectX = 0
scorebordRectY = 0

def setup ():
    size(800, 600)
    background(50)
    
    #setting up the globals only once:
    global scorebordRectWidth
    global scorebordRectHeight
    global scorebordRectX
    global scorebordRectY
    scorebordRectWidth = width / 2
    scorebordRectHeight = height / 2
    scorebordRectX = (width - scorebordRectWidth) / 2
    scorebordRectY = (height - scorebordRectHeight) / 2
    
def draw():
    background(255)
    # Only show the scoreboard if this variable is set to True
    if showScoreBoard:
        drawScoreBoard()

def drawScoreBoard():
    fill(0)
    rect(scorebordRectX, scorebordRectY, scorebordRectWidth, scorebordRectHeight)
    text("SCOREBOARD", scorebordRectX, scorebordRectY)

# Manage the state of the Tab key and store it in the showScoreBoard global variable
def keyPressed():
    if key == TAB:
        global showScoreBoard
        showScoreBoard = True
def keyReleased():
    if key == TAB:
        global showScoreBoard
        showScoreBoard = False

Have fun!

Related