#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# if a number key is pressed display that same number of a picture on the screan

edited October 2017

Hi, I have an assignment where I have to draw any object and then if you press a number key (from 1 to 9) that object will appear the number of times as the number pressed (in random places). Can someone help me with what I would put in this keyPressed function. (Function1 is my drawing)

• This forum generally only helps those that have made an attempt at the program and are stuck with their current method. Do you have any code where you attempted to do this?

• ``````xCoord=250
yCoord=250

def setup():
size(500,500)
background(255)

def keyPressed():

global xCoord, yCoord, numbers

def pigFunction(xCoord,yCoord):
#legs
fill(250,81,214)
rect(xCoord-15,yCoord+30,5,10)
rect(xCoord+15,yCoord+30,5,10)

#body
fill(250,81,214)
ellipse(xCoord,yCoord,70,70)

#ears
triangle(xCoord-40,yCoord-15,xCoord-35,yCoord-35,xCoord-30,yCoord-15)
triangle(xCoord-25,yCoord-15,xCoord-20,yCoord-35,xCoord-15,yCoord-15)

ellipse(xCoord-25,yCoord-5,50,40)

#nose
ellipse(xCoord-25,yCoord,25,15)
line(xCoord-30,yCoord+3,xCoord-30,yCoord-3)
line(xCoord-22,yCoord+3,xCoord-22,yCoord-3)

#eyes and pupils
fill(255)
ellipse(xCoord-32,yCoord-15,10,10)
ellipse(xCoord-18,yCoord-15,10,10)
fill(0,0,255)
ellipse(xCoord-32,yCoord-15,5,5)
ellipse(xCoord-18,yCoord-15,5,5)

if keyPressed():
if (key=="1" or key=="2" or key=="3" or key=="4" or key=="5" or key=="6" or key=="7" or key=="8" or key=="9"):
for key in range("key"):
xCoord=random(width)
yCoord=random(height)
#positions specified as [xCoord, yCoord]
print pigFunction(xCoord,yCoord)

elif (key=="C" or key=="c"):
background(255)
``````
• Are you defining pigFunction() inside mousePressed()? It is hard to tell.

Have you looked into using `setup()` and `draw()`?

• ``````xCoord=250
yCoord=250

def setup():
size(500,500)
background(255)

def keyPressed():

global xCoord, yCoord

if (key=="1" or key=="2" or key=="3" or key=="4" or key=="5" or key=="6" or key=="7" or key=="8" or key=="9"):
for i in range(key+1):
xCoord=random(width)
yCoord=random(height)
#positions specified as [xCoord, yCoord]
print pigFunction(xCoord,yCoord)

elif (key=="C" or key=="c"):
background(255)

def pigFunction(xCoord,yCoord):
#legs
fill(250,81,214)
rect(xCoord-15,yCoord+30,5,10)
rect(xCoord+15,yCoord+30,5,10)

#body
fill(250,81,214)
ellipse(xCoord,yCoord,70,70)

#ears
triangle(xCoord-40,yCoord-15,xCoord-35,yCoord-35,xCoord-30,yCoord-15)
triangle(xCoord-25,yCoord-15,xCoord-20,yCoord-35,xCoord-15,yCoord-15)

ellipse(xCoord-25,yCoord-5,50,40)

#nose
ellipse(xCoord-25,yCoord,25,15)
line(xCoord-30,yCoord+3,xCoord-30,yCoord-3)
line(xCoord-22,yCoord+3,xCoord-22,yCoord-3)

#eyes and pupils
fill(255)
ellipse(xCoord-32,yCoord-15,10,10)
ellipse(xCoord-18,yCoord-15,10,10)
fill(0,0,255)
ellipse(xCoord-32,yCoord-15,5,5)
ellipse(xCoord-18,yCoord-15,5,5)

def draw():
keyPressed()
``````
• use int(key) to parse your key into a number. Currently, you are concatenating a string to a number. That said, you don't need to add one to it (on line 16). Also, don't call keyPressed in your draw. It's a callback function that is called automatically if you put it there. I would also move the def pigFunction outside of your keyPressed and put it right after the setup. Keep in mind that the pressed function will be called repeatedly while the key is pressed. You could take care of that by implementing a "debounce" or just fill the background first thing inside your if-statement (at line 16, before the loop).