We are about to switch to a new forum software. Until then we have removed the registration on this forum.
can someone please help. When I run this it just continuously displays pigs until I stop it.
xCoord=250
yCoord=250
def setup():
size(500,500)
background(255)
def keyPressed():
global xCoord, yCoord
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)
#head
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 (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 draw():
keyPressed()
Answers
Why are you defining
pigFunction
insidekeyPressed
, rather than it being separate?Why are you calling
keyPressed
fromdraw
, rather than it being called automatically?From the sketch you posted, line:
generates an error:
You have some very strange notions about which functions do what.
setup()
is called once when your sketch starts. You should use this function to do any one-time things that you only need to do when your sketch starts. It's literally the functions that should set up things. The first function insetup()
should be a call tosize()
to set the sketch size.draw()
is where you should do ALL your drawing. The first thing indraw()
should be a call tobackground()
to clear away what was drawn on a previous frame. If you need to know some things to properly draw your sketch, those things should be stored in global variables. This would include, for example, the number of animals you want to draw, and their positions.keyPressed()
is called when a key is pressed. It will run once. You can use this function to change the global variables thatdraw()
is using. That way - since what is drawn depends on the value in those variables - you can change what is being drawn by changing simply changing variables' values.jeremydouglas, if i put int(key)+1 this fixes the error. The reason i did keyPressed in draw is because if i dont then nothing happens when i press keys. the reason I did pigFunction inside keyPressed is because this was what I was told to do in my assignment question. This is how my professor told me to format the question..
TfGuy44, I did not know that keyPressed was run automatically thank you. How come without the draw function nothing happens when I press a key? Im not sure what you mean by
"That way - since what is drawn depends on the value in those variables - you can change what is being drawn by changing simply changing variables' values."
Here is an example that shows what I mean. There are some rectangles drawn in
draw()
, and the number of rectangles drawn depends on the value of the variablecount
. If you change the value ofcount
, you also change the number of rectangles drawn.This example also shows you how you can generate random locations... well, random y locations. You can generate the random x locations yourself in a similar way.
ok thank you very much!!