Skip to content

Instantly share code, notes, and snippets.

@rors
Forked from gretaf007/homework03_greta
Last active September 22, 2020 00:03
Show Gist options
  • Save rors/3089ae55aa3106112aff721a1588b50d to your computer and use it in GitHub Desktop.
Save rors/3089ae55aa3106112aff721a1588b50d to your computer and use it in GitHub Desktop.
"""
Greta Villani
9/16/2020
Homework 1
"""
c = color(255,150,150)
fishTailLine = 60
bodyLength = 180
bodyHeight = 80
eyeX = 285
fish2FinPointx = 227
fish2FinPointy = 273
fish1x = 0
def setup():
size(640*2,350*2)
#RasterImage
img = loadImage("sea copy.jpg")
image(img, 0, 0)
strokeWeight(10)
stroke(155,155,255,50)
frameRate(4)
def draw():
size(640*2,350*2)
#RasterImage
img = loadImage("sea copy.jpg")
image(img, 0, 0)
strokeWeight(10)
stroke(155,155,255,50)
#TailTriangle
fill(100,200,210)
triangle(490+fish1x,95, 425+fish1x,145, 490+fish1x,195)
#EllipseFrontBody
fill(100,170,255)
ellipse(340+fish1x,145, bodyLength,bodyHeight)
#eye
fill(150,100,215)
circle(eyeX+fish1x, 135, 10)
#FinLine1
stroke(204, 102, 0)
strokeWeight(5)
line(325+fish1x,150, 350+fish1x,175)
#FinLine2
stroke(204, 102, 0)
strokeWeight(5)
line(370+fish1x,150, 350+fish1x,175)
#TailTriangle 2
fill(100,200,210)
triangle(fishTailLine,195, 145,245, fishTailLine,295 )
#EllipseFrontBody 2
fill(100,170,255)
ellipse(225,245, bodyLength,bodyHeight)
#eye 2
fill(150,100,215)
circle(eyeX, 235, 10)
#FinLine1 2
stroke(204, 102, 0)
strokeWeight(5)
line(205,250, fish2FinPointx,fish2FinPointy)
#FinLine2 2
stroke(204, 102, 0)
strokeWeight(5)
line(250,250, fish2FinPointx,fish2FinPointy)
@rors
Copy link
Author

rors commented Sep 21, 2020

Hi @gretaf007
On line 14, I have added a new variable called fish1x to control the first fish's horizontal position. I assigned that variable initially to 0.

Then, I used that variable when drawing the fish on lines 36, 39, 42, 46, and 50.

Note the different ways I used the variable, which corresponds to horizontal position. With ellipse() for example (line 39), you only need to add the new variable to the first parameter, since that corresponds to the horizontal position. But with line() (line 46 & 50) you need to add the variable to the first and third parameters, since they correspond to the x positions of the start and end points. And in triangle() (line 36) you need to add the variable to the first, third, and fifth parameters, because they all correspond to the x position of the various points.

After doing this, fish1x will control the first fish's horizontal position. You could see this by changing line 14 to say fish1x = random(5,1000) and re-running your sketch a few times to see different fish positions. Or, at line 28 you could insert a new line that says fish1x = mouseX to make it controlled by the mouse, and then maybe you could start experimenting with map() for this week's homework.

Also, make sure you comment out lines 20 and 29. You want to load the image in setup() but draw the image in draw()

Hope that helps!

@rors
Copy link
Author

rors commented Sep 21, 2020

By the way, click "Revisions" above (or click here) to see a "diff" of my changes.

@rors
Copy link
Author

rors commented Sep 21, 2020

Please let me know if you were able to receive this and if it was helpful. Thanks.

@gretaf007
Copy link

@rors
I got your edits! I will look at them tonight after class. Thanks so much!!

@rors
Copy link
Author

rors commented Sep 21, 2020

👍

@gretaf007
Copy link

@rors
Oh! I forgot, I couldn't figure out how to make the jpg bigger in the background. Is this not possible because the image file is a set size?
Thanks

@rors
Copy link
Author

rors commented Sep 21, 2020

Check out the documentation for image(). You can use fourth and fifth arguments to change the display size of the image. But keep in mind that since it's a raster image, changing the size will potentially distort the image. If you want to make sure the image is not distorted, you can check the dimensions of the original image, and multiply both width and height by the same amount.

For example, if the original image was 200 pixels wide by 500 pixels tall, you could scale the image without distortion to 400,1000, but changing width and height by different amounts (for example to 400,500) would distort the image.

@rors
Copy link
Author

rors commented Sep 22, 2020

Oh, one other things. I mentioned that you should comment out lines 20 and 29. That is true, but will cause another problem – perhaps that problem is why you did things this way to begin. The problem has to do with scope, and the solution is to use global. Like this:

    # put these lines in the setup() block:
    global img
    img = loadImage("some-image.jpg")

    # put these lines in the draw() block:
    global img
    image(img,0,0)

We'll talk about this more on Weds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment