How to make a key to become a integer and use the key on keyPressed?

[Python] I am a beginner
I’ve been having troubles trying to first convert the value of a key (any key 0-9) returning an integer value so I can use this value whenever I press the key.
I am supposed to make a square increase the size by the value of the key.
my code so far is:

def calculate_size(diameter, key):
    diameter = 50
    key = int(key)
    diameter = diameter ** key
    return diameter ** int('key')

Then, I have no idea how to input this function in the keyPressed function.

1 Like
ZERO, NINE = ord('0'), ord('9')
diam = 50

def setup():
    size(700, 700)
    noLoop()


def draw():
    background(int(random(PImage.ALPHA_MASK)))
    circle(width >> 1, height >> 1, diam)
    print diam


def keyPressed():
    global diam
    if key.isdigit(): diam += keyCode - ZERO
    redraw()
3 Likes

I have tried many ways to fix this issue, I feel it’s the only thing missing. the size of the circle should increase whenever I press a numerical key, and the size must remain whenever I click somewhere on canvas. So far, the program doesn’t crash when I press a key, but nothing happens to its size either. My work is:

circle_x = 250
circle_y = 250
circle_size = 50
diameter = 50

def calculate_size(diameter, key):
    return diameter ** key

def setup():
    size(500, 500)
    return 

def draw():
    global circle_x, circle_y, circle_size
    background(0, 0, 40)
    ellipse(circle_x,circle_y, circle_size, circle_size)

def mouseClicked():
    global circle_x, circle_y
    circle_x = mouseX
     circle_y = mouseY

def keyPressed():
    global circle_size, circle_x, circle_y, diameter
    if key == int(key):
        circle_size(circle_x, circle_y, calculate_size(diameter, key), calculate_size(diameter, key))
1 Like

How about this:

def calculate_size(diameter, key):
    return diameter * key

...
 
def keyPressed():
    global circle_size, circle_x, circle_y, diameter
    if key.isdigit():
        circle_size = calculate_size(diameter, int(key))

I had to modify the calculate_size() function because the circles were massive.

3 Likes

Thanks for the help. It worked now!!