pastebin - collaborative debugging tool
alicesucks.kpaste.net RSS


Untitled
Posted by Anonymous on Wed 15th Dec 2010 13:59
raw | new post

  1. '''
  2. 1. Create an image in paint or something with aspect ratio 4:3.
  3. 2. Make a billboard with it using: File > Make Billboard... in Alice.
  4. 3. Move and resize it so it covers the whole screen.
  5. 4. Place the script "main(<billboard name>)" in world.my_first_method. If the name starts with a number, place an underscore before it.
  6. 5. Create a new world method, handleMouseClick
  7. 6. Create a "When mouse is clicked on" event with the billboard and world.handleMouseClick
  8. 7. In the handleMouseClick method, call the script "handleMouseClick()".
  9. 8 (Optional). Set the ambientLightColor world variable to "no color" to have white cells instead of grey.
  10. '''
  11. # apparently nested functions can't see outside variables without this, silly jython
  12. from __future__ import nested_scopes
  13.  
  14. # core classes
  15. from edu.cmu.cs.stage3.alice.core import *
  16. from edu.cmu.cs.stage3.alice.core.event import *
  17. from edu.cmu.cs.stage3.alice.core.response import *
  18. from edu.cmu.cs.stage3.alice.core.property import *
  19. from edu.cmu.cs.stage3.alice.core.question import *
  20.  
  21. # constants
  22. from edu.cmu.cs.stage3.alice.core.Direction import *
  23. from edu.cmu.cs.stage3.alice.core.style.TraditionalAnimationStyle import *
  24.  
  25. # image
  26. from java.awt import *
  27. from java.awt.image import *
  28. from java.awt.geom import *
  29.  
  30. # for profiling
  31. import time
  32.  
  33. # misc
  34. from edu.cmu.cs.stage3.alice.authoringtool import *
  35. from edu.cmu.cs.stage3.alice.authoringtool.util import *
  36.  
  37. def main(screen):
  38.     global screenObj
  39.     global screenComponent
  40.     screenObj = screen
  41.     screenComponent = world.camera.renderTarget.getAWTComponent()
  42.  
  43.     # set dimension constants
  44.     _size = screenComponent.getSize()
  45.  
  46.     dims.WIDTH = _size.width
  47.     dims.HEIGHT = _size.height
  48.     # make a square grid
  49.     dims.GRID_WIDTH = dims.HEIGHT
  50.     # get as close to 20 px per cell as possible, is a float
  51.     dims.CELL_WIDTH = dims.GRID_WIDTH / round(dims.GRID_WIDTH / 20)
  52.  
  53.     dims.MENU_WIDTH = dims.WIDTH - dims.GRID_WIDTH
  54.  
  55.     # store images
  56.     makeImage('main', dims.WIDTH, dims.HEIGHT)
  57.     makeGrids()
  58.     makeImage('menu', dims.MENU_WIDTH, dims.HEIGHT)
  59.  
  60.     # initialize grids
  61.     for gridX in xrange(int(dims.GRID_WIDTH / dims.CELL_WIDTH)):
  62.         for gridY in xrange(int(dims.HEIGHT / dims.CELL_WIDTH)):
  63.             changeCell(gridX, gridY, OFF)
  64.             changeCell(gridX, gridY, OFF, false)
  65.     updateGrid()
  66.  
  67. ### CLASSES
  68. class Generic: # TODO: find a better way to have dicts that work with . instead of []
  69.     pass
  70.  
  71. ### VARIABLES
  72. # the Billboard object that represents the screen
  73. screenObj = None
  74.  
  75. # the actual Component of the screen
  76. screenComponent = None
  77.  
  78. ### CONSTANTS
  79. dims = Generic()
  80.  
  81. ### FUNCTIONS
  82. # images
  83.  
  84. images = {}
  85.  
  86. def makeImage(name, w, h):
  87.     '''
  88.     adds an (empty BufferedImage, its Graphics2D) tuple to images if doesn't already exist
  89.     '''
  90.     if name not in images.keys():
  91.         image = AuthoringTool.getHack().getJAliceFrame().getGraphicsConfiguration().createCompatibleImage(w, h)
  92.         modifiable = image.createGraphics()
  93.         images[name] = (image, modifiable)
  94.  
  95. def getImage(name):
  96.     return images[name][0]
  97.  
  98. def getModifiable(name):
  99.     return images[name][1]
  100.  
  101. # textures
  102. def setTexture(object, image):
  103.     '''
  104.     takes java.awt.Image, makes it texture of object
  105.  
  106.     0 - 0.001
  107.     '''
  108.     texture = TextureMap()
  109.     texture.propertyChanged(PropertyEvent(texture.image, image))
  110.     object.propertyChanged(PropertyEvent(object._diffuseColorMap, texture))
  111.     del texture
  112.  
  113. def getTextureImage(object):
  114.     '''
  115.     returns java.awt.Image
  116.     '''
  117.     return object.diffuseColorMap.image
  118.  
  119. # grid
  120.  
  121. def makeGrids():
  122.     makeImage('grid0', dims.GRID_WIDTH, dims.HEIGHT)
  123.     makeImage('grid1', dims.GRID_WIDTH, dims.HEIGHT)
  124.     getModifiable('grid0').setStroke(BasicStroke(1))
  125.  
  126. curGrid = 0
  127.  
  128. def getCurGridName():
  129.     return 'grid' + str(curGrid)
  130.  
  131. def getOtherGridName():
  132.     return 'grid' + str(1 - curGrid)
  133.  
  134. def switchGrids():
  135.     global curGrid
  136.     curGrid = 1 - curGrid
  137.  
  138. def isOn(gridX, gridY, isCurGrid = true):
  139.     '''
  140.     0.0000333 over 10000 runs
  141.     '''
  142.     if isCurGrid:
  143.         name = getCurGridName()
  144.     else:
  145.         name = getOtherGridName()
  146.     x, y = gridX * dims.CELL_WIDTH, gridY * dims.CELL_WIDTH
  147.     return getImage(name).getRGB(int(x + dims.CELL_WIDTH / 2), int(y + dims.CELL_WIDTH / 2)) == Color.BLACK.getRGB()
  148.  
  149. TOGGLE = 0
  150. ON = 1
  151. OFF = 2
  152. def changeCell(gridX, gridY, setting = TOGGLE, isCurGrid = true):
  153.     '''
  154.     0 - 0.001, 0.000071 over 10000 runs
  155.     '''
  156.     if isCurGrid:
  157.         name = getCurGridName()
  158.     else:
  159.         name = getOtherGridName()
  160.     x, y = gridX * dims.CELL_WIDTH, gridY * dims.CELL_WIDTH
  161.     rect = Rectangle2D.Double(x, y, dims.CELL_WIDTH, dims.CELL_WIDTH)
  162.     g2d = getModifiable(name)
  163.     if setting == ON:
  164.         g2d.setPaint(Color.BLACK)
  165.     elif setting == OFF:
  166.         g2d.setPaint(Color.WHITE)
  167.     else:
  168.         if isOn(gridX, gridY, isCurGrid):
  169.             # it's on
  170.             g2d.setPaint(Color.WHITE)
  171.         else:
  172.             g2d.setPaint(Color.BLACK)
  173.     g2d.fill(rect)
  174.     g2d.setPaint(Color.BLACK)
  175.     g2d.draw(rect)
  176.  
  177. def updateGrid():
  178.     '''
  179.     blit current grid to main image, then update it as the texture
  180.  
  181.     0.001 - 0.002
  182.     '''
  183.     g2d = getModifiable('main')
  184.     g2d.drawImage(getImage(getCurGridName()), None, 0, 0)
  185.     setTexture(screenObj, getImage('main'))
  186.  
  187. def runOneStep():
  188.     start = time.time()
  189.     gridW = int(dims.GRID_WIDTH / dims.CELL_WIDTH)
  190.     gridH = int(dims.HEIGHT / dims.CELL_WIDTH)
  191.     pxArray = getImage(getCurGridName()).getRaster().getDataBuffer().getData()
  192.     def _isOn(gridX, gridY):
  193.         x, y = gridX * dims.CELL_WIDTH, gridY * dims.CELL_WIDTH
  194.         return pxArray[int(x + dims.CELL_WIDTH / 2) + int(y + dims.CELL_WIDTH / 2) * dims.GRID_WIDTH] == Color.BLACK.getRGB()
  195.     for gridX in xrange(gridW):
  196.         for gridY in xrange(gridH):
  197.             count = 0
  198.             curIsOn = _isOn(gridX, gridY)
  199.             if curIsOn:
  200.                 for offX in xrange(-1, 2):
  201.                     for offY in xrange(-1, 2):
  202.                         if offX or offY:
  203.                             if _isOn((gridX + offX) % gridW, (gridY + offY) % gridH):
  204.                                 count += 1
  205.                                 if count == 4:
  206.                                     break
  207.                     if count == 4:
  208.                         break
  209.                 if count <= 1 or count == 4:
  210.                     changeCell(gridX, gridY, OFF, false)
  211.                 else:
  212.                     changeCell(gridX, gridY, ON, false)
  213.             else:
  214.                 for offX in xrange(-1, 2):
  215.                     for offY in xrange(-1, 2):
  216.                         if offX or offY:
  217.                             if _isOn((gridX + offX) % gridW, (gridY + offY) % gridH):
  218.                                 count += 1
  219.                                 if count == 4:
  220.                                     break
  221.                     if count == 4:
  222.                         break
  223.                 if count == 3:
  224.                     changeCell(gridX, gridY, ON, false)
  225.                 else:
  226.                     changeCell(gridX, gridY, OFF, false)
  227.     switchGrids()
  228.     updateGrid()
  229.     del pxArray
  230.     print time.time() - start
  231.  
  232. # mouse
  233. def handleMouseClick():
  234.     pos = screenComponent.getMousePosition()
  235.     changeCell(int(pos.x / dims.CELL_WIDTH), int(pos.y / dims.CELL_WIDTH))
  236.     updateGrid()
  237.  
  238. # misc
  239. def onScreen(obj):
  240.     world.bool = camera.canSee(obj, false)

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at