SGladys / mediacomp-jes

Automatically exported from code.google.com/p/mediacomp-jes
0 stars 0 forks source link

Code in program area can be outside of a function #40

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Code not placed in a function in the program area is executed when the program 
is loaded.  Is 
this a bug? Feature? Based on the corresponding text, it seems like it's a very 
misleading bug 
which can promote bad practices!

Student example:

def main():
  loadPictures()
  saveComic()

setMediaPath()
titlePic=makePicture(getMediaPath("pictureTitle.jpg"))
firstPic=makePicture(getMediaPath("pictureOne.jpg"))
secondPic=makePicture(getMediaPath("pictureTwo.jpg"))
thirdPic=makePicture(getMediaPath("pictureThree.jpg"))
fourthPic=makePicture(getMediaPath("pictureFour.jpg"))
bg=makePicture(getMediaPath("background.jpg"))

def loadPictures():
  posterize(thirdPic)
  chromakey(titlePic,bg)
  drawBorder(titlePic)
  drawBorder(firstPic)
  drawBorder(secondPic)
  drawBorder(thirdPic)
  drawBorder(fourthPic)
  addText(titlePic,234,411,"Bench Hunting: A Comic Strip by XXXXXX")
  addText(firstPic,21,445,"This bench is great! Come try it!")
  addText(firstPic,435,131,"Okay!")
  addText(secondPic,94,86,"Let's try to find a more comfortable one!")
  addText(secondPic,486,177,"As long as the bench is fierce")
  addText(thirdPic,42,139,"This bench is comfortable!")
  addText(thirdPic,260,68,"This isn't a bench")
  addText(thirdPic,525,147,"Not fierce enough")
  addText(fourthPic,162,38,"You know what the most comfortable bench is? A couch.")
  addText(fourthPic,502,89,"I hope it's animal print")
  layoutComic()

def posterize(thirdPic):
  for p in getPixels(thirdPic):
    red=getRed(p)
    green=getGreen(p)
    blue=getBlue(p)

    if(red<64):
      setRed(p,31)
    if(red>63 and red<128):
      setRed(p,95)
    if(red>127 and red<192):
      setRed(p,192)
    if(red>191 and red<256):
      setRed(p,223)

    if(green<64):
      setGreen(p,31)
    if(green>63 and green<128):
      setGreen(p,95)
    if(green>127 and green<192):
      setGreen(p,192)
    if(green>191 and green<256):
      setGreen(p,223)

    if(blue<64):
      setBlue(p,31)
    if(blue>63 and blue<128):
      setBlue(p,95)
    if(blue>127 and blue<192):
      setBlue(p,192)
    if(blue>191 and blue<256):
      setBlue(p,223)

def chromakey(titlePic,bg):
  for x in range(0,getWidth(titlePic)):
    for y in range(0,getHeight(titlePic)):
      p=getPixel(titlePic,x,y)
      if (getRed(p)+getBlue(p)<getGreen(p)):
        setColor(p,getColor(getPixel(bg,x,y)))
  return titlePic
  show(titlePic)

def drawBorder(titlePic):
  w = getWidth(titlePic)
  h = getHeight(titlePic)
  for x in range(0,w):
    pix = getPixel(titlePic,x,0)
    setColor(pix,black)
    pix = getPixel(titlePic,x,1)
    setColor(pix,black)
    pix = getPixel(titlePic,x,h-1)
    setColor(pix,black)
    pix = getPixel(titlePic,x,h-2)
    setColor(pix,black)
  for y in range(0,h):
    pix = getPixel(titlePic,0,y)
    setColor(pix,black)
    pix = getPixel(titlePic,1,y)
    setColor(pix,black)
    pix = getPixel(titlePic,w-2,y)
    setColor(pix,black)
    pix = getPixel(titlePic,w-1,y)
    setColor(pix,black)

def drawBorder(firstPic):
  w = getWidth(firstPic)
  h = getHeight(firstPic)
  for x in range(0,w):
    pix = getPixel(firstPic,x,0)
    setColor(pix,black)
    pix = getPixel(firstPic,x,1)
    setColor(pix,black)
    pix = getPixel(firstPic,x,h-1)
    setColor(pix,black)
    pix = getPixel(firstPic,x,h-2)
    setColor(pix,black)
  for y in range(0,h):
    pix = getPixel(firstPic,0,y)
    setColor(pix,black)
    pix = getPixel(firstPic,1,y)
    setColor(pix,black)
    pix = getPixel(firstPic,w-2,y)
    setColor(pix,black)
    pix = getPixel(firstPic,w-1,y)
    setColor(pix,black)

def drawBorder(secondPic):
  w = getWidth(secondPic)
  h = getHeight(secondPic)
  for x in range(0,w):
    pix = getPixel(secondPic,x,0)
    setColor(pix,black)
    pix = getPixel(secondPic,x,1)
    setColor(pix,black)
    pix = getPixel(secondPic,x,h-1)
    setColor(pix,black)
    pix = getPixel(secondPic,x,h-2)
    setColor(pix,black)
  for y in range(0,h):
    pix = getPixel(secondPic,0,y)
    setColor(pix,black)
    pix = getPixel(secondPic,1,y)
    setColor(pix,black)
    pix = getPixel(secondPic,w-2,y)
    setColor(pix,black)
    pix = getPixel(secondPic,w-1,y)
    setColor(pix,black)

def drawBorder(thirdPic):
  w = getWidth(thirdPic)
  h = getHeight(thirdPic)
  for x in range(0,w):
    pix = getPixel(thirdPic,x,0)
    setColor(pix,black)
    pix = getPixel(thirdPic,x,1)
    setColor(pix,black)
    pix = getPixel(thirdPic,x,h-1)
    setColor(pix,black)
    pix = getPixel(thirdPic,x,h-2)
    setColor(pix,black)
  for y in range(0,h):
    pix = getPixel(thirdPic,0,y)
    setColor(pix,black)
    pix = getPixel(thirdPic,1,y)
    setColor(pix,black)
    pix = getPixel(thirdPic,w-2,y)
    setColor(pix,black)
    pix = getPixel(thirdPic,w-1,y)
    setColor(pix,black)

def drawBorder(fourthPic):
  w = getWidth(fourthPic)
  h = getHeight(fourthPic)
  for x in range(0,w):
    pix = getPixel(fourthPic,x,0)
    setColor(pix,black)
    pix = getPixel(fourthPic,x,1)
    setColor(pix,black)
    pix = getPixel(fourthPic,x,h-1)
    setColor(pix,black)
    pix = getPixel(fourthPic,x,h-2)
    setColor(pix,black)
  for y in range(0,h):
    pix = getPixel(fourthPic,0,y)
    setColor(pix,black)
    pix = getPixel(fourthPic,1,y)
    setColor(pix,black)
    pix = getPixel(fourthPic,w-2,y)
    setColor(pix,black)
    pix = getPixel(fourthPic,w-1,y)
    setColor(pix,black)

canvas=makeEmptyPicture((getWidth(titlePic)+getWidth(firstPic)+getWidth(secondPi
c)+getWidth(t
hirdPic)+getWidth(fourthPic)),getHeight(firstPic),lightGray)

def layoutComic():
  targetX=0
  for sourceX in range(0,getWidth(titlePic)):
    targetY=getHeight(canvas)-getHeight(titlePic)-30
    for sourceY in range(0,getHeight(titlePic)):
      px=getPixel(titlePic,sourceX,sourceY)
      cx=getPixel(canvas,targetX,targetY)
      setColor(cx,getColor(px))
      targetY=targetY+1
    targetX=targetX+1
  targetX=getWidth(titlePic)
  for sourceX in range(0,getWidth(firstPic)):
    targetY=getHeight(canvas)-getHeight(firstPic)
    for sourceY in range(0,getHeight(firstPic)):
      px=getPixel(firstPic,sourceX,sourceY)
      cx=getPixel(canvas,targetX,targetY)
      setColor(cx,getColor(px))
      targetY=targetY+1
    targetX=targetX+1
  targetX=(getWidth(titlePic)+getWidth(firstPic))
  for sourceX in range(0,getWidth(secondPic)):
    targetY=getHeight(canvas)-getHeight(secondPic)
    for sourceY in range(0,getHeight(secondPic)):
      px=getPixel(secondPic,sourceX,sourceY)
      cx=getPixel(canvas,targetX,targetY)
      setColor(cx,getColor(px))
      targetY=targetY+1
    targetX=targetX+1
  targetX=(getWidth(titlePic)+getWidth(firstPic)+getWidth(secondPic))
  for sourceX in range(0,getWidth(thirdPic)):
    targetY=getHeight(canvas)-getHeight(thirdPic)
    for sourceY in range(0,getHeight(thirdPic)):
      px=getPixel(thirdPic,sourceX,sourceY)
      cx=getPixel(canvas,targetX,targetY)
      setColor(cx,getColor(px))
      targetY=targetY+1
    targetX=targetX+1
  targetX=(getWidth(titlePic)+getWidth(firstPic)+getWidth(secondPic)+getWidth(thirdPic))
  for sourceX in range(0,getWidth(fourthPic)):
    targetY=getHeight(canvas)-getHeight(fourthPic)
    for sourceY in range(0,getHeight(fourthPic)):
      px=getPixel(fourthPic,sourceX,sourceY)
      cx=getPixel(canvas,targetX,targetY)
      setColor(cx,getColor(px))
      targetY=targetY+1
    targetX=targetX+1  
  show(canvas)
  return(canvas)

def saveComic():
  writePictureTo(canvas,"brill3.jpg")

Original issue reported on code.google.com by peter.de...@gmail.com on 14 Apr 2010 at 6:36

GoogleCodeExporter commented 8 years ago
Peter,

Thank you for your report.  This is in fact a "feature" and not a bug.  The JES
program pane does allow any valid python code, not just definitions.  While 
various
IDEs handle this in different ways, the behavior of JES is consistent with 
systems
like IDLE and DrScheme.  

Cheers,
-Brian

Original comment by bjdorn@gmail.com on 14 Apr 2010 at 7:03