how do I make it so that when pressing a number have that number of an animal displayed !?

edited October 2017 in Python Mode

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 inside keyPressed, rather than it being separate?

    Why are you calling keyPressed from draw, rather than it being called automatically?

    From the sketch you posted, line:

    for i in range(key + 1):
    

    generates an error:

    TypeError: cannot concatenate 'unicode' and 'int' objects

  • 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 in setup() should be a call to size() to set the sketch size.

    draw() is where you should do ALL your drawing. The first thing in draw() should be a call to background() 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 that draw() 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..

    def setup():
    
        #code for setup()
    
    def keyPressed():
    
        #what should happen when a key is pressed
    
        #this is where your code for drawing your object will go
    
        #in here you check to see which key is pressed.
    
    def draw():
    
         #what should happen over and over again
    

    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."

  • edited November 2017
    # Put global variables here.
    # How many pigs to draw? This is a number.
    # Where are the pigs (X positions)? This is an array of numbers.
    # Where are the pigs (Y positions)? This is an array of numbers.
    
    def setup(): 
        # Set sketch size.
    
    def draw(): 
        # Clear previous frame by drawing a solid background.
        # Draw some number of pigs by calling the function that draws pigs.
        # How many pigs? This number should be stored in a global variable.
        # Where are the pigs? The locations should be stored in global variables.
    
    def keyPressed():
        # Remember the number of the key that was pressed.
        # Generate new random locations.
    
    def drawPig():
        # Draws one pig. Where?
    
  • edited November 2017
    int count;
    int[] y_locations = new int[10];
    
    void setup() {
      size(400, 400);
      count = 3;
    }
    
    void draw() {
      background(0);
      fill(255);
      for ( int i = 0; i < count; i++ ) {
        rect(20+30*i, 20+y_locations[i], 20, 20);
      }
    }
    
    void keyPressed() {
      count = 1 + int(random(9));
      for ( int i = 0; i < 10; i++) {
        y_locations[i] = int(random(height-40));
      }
    }
    

    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 variable count. If you change the value of count, 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!!

Sign In or Register to comment.