pastebin - collaborative debugging tool
alicesucks.kpaste.net RSS


Untitled
Posted by Anonymous on Mon 13th Dec 2010 12:54
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. Create the world variables mouseX and mouseY.
  9. 8. In the handleMouseClick method, set these to the mouse position, then call the script "handleMouseClick()".
  10. 9 (Optional). Set the ambientLightColor world variable to "no color" to have white cells instead of grey.
  11. '''
  12. # core classes
  13. from edu.cmu.cs.stage3.alice.core import *
  14. from edu.cmu.cs.stage3.alice.core.event import *
  15. from edu.cmu.cs.stage3.alice.core.response import *
  16. from edu.cmu.cs.stage3.alice.core.property import *
  17. from edu.cmu.cs.stage3.alice.core.question import *
  18.  
  19. # constants
  20. from edu.cmu.cs.stage3.alice.core.Direction import *
  21. from edu.cmu.cs.stage3.alice.core.style.TraditionalAnimationStyle import *
  22.  
  23. # image
  24. from java.awt import *
  25. from java.awt.image import *
  26. from java.awt.geom import *
  27.  
  28. # misc
  29. from edu.cmu.cs.stage3.alice.authoringtool import *
  30. from edu.cmu.cs.stage3.alice.authoringtool.util import *
  31.  
  32. def main(screen):
  33.     global screenObj
  34.     screenObj = screen
  35.  
  36.     # set dimension constants
  37.     _size = world.camera.renderTarget.getAWTComponent().getSize()
  38.  
  39.     dims.WIDTH = _size.width
  40.     dims.HEIGHT = _size.height
  41.     # make a square grid
  42.     dims.GRID_WIDTH = dims.HEIGHT
  43.     # get as close to 20 px per cell as possible, is a float
  44.     dims.CELL_WIDTH = dims.GRID_WIDTH / round(dims.GRID_WIDTH / 20)
  45.  
  46.     dims.MENU_WIDTH = dims.WIDTH - dims.GRID_WIDTH
  47.  
  48.     # store images
  49.     makeImage('main', dims.WIDTH, dims.HEIGHT)
  50.     makeGrids()
  51.     makeImage('menu', dims.MENU_WIDTH, dims.HEIGHT)
  52.  
  53.     # initialize grid
  54.     for x in xrange(int(dims.GRID_WIDTH / dims.CELL_WIDTH)):
  55.         for y in xrange(int(dims.HEIGHT / dims.CELL_WIDTH)):
  56.             changeCell(x, y, OFF)
  57.     updateGrid()
  58.  
  59. ### CLASSES
  60. class Generic: # TODO: find a better way to have dicts that work with . instead of []
  61.     pass
  62.  
  63. ### VARIABLES
  64. # the Billboard object that represents the screen
  65. screenObj = None
  66.  
  67. ### CONSTANTS
  68. dims = Generic()
  69.  
  70. ### FUNCTIONS
  71. # images
  72.  
  73. images = {}
  74.  
  75. def makeImage(name, w, h):
  76.     '''
  77.     adds an (empty BufferedImage, its Graphics2D) tuple to images
  78.     '''
  79.     image = AuthoringTool.getHack().getJAliceFrame().getGraphicsConfiguration().createCompatibleImage(w, h)
  80.     modifiable = image.createGraphics()
  81.     images[name] = (image, modifiable)
  82.  
  83. def getImage(name):
  84.     return images[name][0]
  85.  
  86. def getModifiable(name):
  87.     return images[name][1]
  88.  
  89. # textures
  90. def setTexture(object, image):
  91.     '''
  92.     takes java.awt.Image, makes it texture of object
  93.     '''
  94.     texture = TextureMap()
  95.     texture.propertyChanged(PropertyEvent(texture.image, image))
  96.     object.propertyChanged(PropertyEvent(object._diffuseColorMap, texture))
  97.  
  98. def getTextureImage(object):
  99.     '''
  100.     returns java.awt.Image
  101.     '''
  102.     return object.diffuseColorMap.image
  103.  
  104. # grid
  105.  
  106. def makeGrids():
  107.     makeImage('grid0', dims.GRID_WIDTH, dims.HEIGHT)
  108.     makeImage('grid1', dims.GRID_WIDTH, dims.HEIGHT)
  109.     getModifiable('grid0').setStroke(BasicStroke(1))
  110.  
  111. curGrid = 0
  112.  
  113. def getCurGridName():
  114.     return 'grid' + str(curGrid)
  115.  
  116. def getOtherGridName():
  117.     return 'grid' + str(1 - curGrid)
  118.  
  119. TOGGLE = 0
  120. ON = 1
  121. OFF = 2
  122. def changeCell(gridX, gridY, setting = TOGGLE, isCurGrid = true):
  123.     if isCurGrid:
  124.         name = getCurGridName()
  125.     else:
  126.         name = getOtherGridName()
  127.     x, y = gridX * dims.CELL_WIDTH, gridY * dims.CELL_WIDTH
  128.     rect = Rectangle2D.Double(x, y, dims.CELL_WIDTH, dims.CELL_WIDTH)
  129.     g2d = getModifiable(name)
  130.     if setting == ON:
  131.         g2d.setPaint(Color.BLACK)
  132.     elif setting == OFF:
  133.         g2d.setPaint(Color.WHITE)
  134.     else:
  135.         if getImage(name).getRGB(int(x + dims.CELL_WIDTH / 2), int(y + dims.CELL_WIDTH / 2)) == Color.BLACK.getRGB():
  136.             # it's on
  137.             g2d.setPaint(Color.WHITE)
  138.         else:
  139.             g2d.setPaint(Color.BLACK)
  140.     g2d.draw(rect)
  141.  
  142. def updateGrid():
  143.     '''
  144.     blit current grid to main image, then update it as the texture
  145.     '''
  146.     g2d = getModifiable('main')
  147.     g2d.drawImage(getImage(getCurGridName()), None, 0, 0)
  148.     setTexture(screenObj, getImage('main'))
  149.  
  150. # mouse
  151. def handleMouseClick():
  152.     changeCell(int(mouseX.getValue() / dims.CELL_WIDTH), int(mouseY.getValue() / dims.CELL_WIDTH))
  153.     updateGrid()
  154.  
  155. # misc
  156. def onScreen(obj):
  157.     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