Open topic with navigation
Tutorial: Finding shapes
In this section of the tutorial we'll determine when a shape has been
found by the user and mark it's location in the bird's eye map.
First, let's create an OTF layer of points to use for marking the map:
#Add points for found shapes
viz.startLayer(viz.POINTS)
viz.pointSize(5)
viz.vertexColor(viz.RED)
points = viz.endLayer()
points.disable(viz.CULLING)
points.renderOnlyToWindows([BirdEyeWindow])
Proximity to a shape will be the factor that determines when it has
been found. For this we'll import the vizproximity
library:
import vizproximity
Next, a fading action is defined that will be applied to shapes that
are found by the user:
#Create action for when shape is found
fadeColor = vizact.fadeTo(viz.BLUE,time=2)
fadeAlpha = vizact.fadeTo(0.4,time=3)
fade = vizact.parallel(fadeColor,fadeAlpha)
Finally, code is added that sets up and handles proximity events. Proximity
sensors are applied to each shape and the viewpoint is made a proximity
target. When the target enters the range of one of the sensors the following
occurs:
- A vizproximity.ENTER_PROXIMITY_EVENT
is generated and the FoundShape function
is called
- The fade action
is applied to the shape
- A point is drawn in the bird's eye map at the
location of the shape
- A message is displayed to the user if all the
shapes have been found
def FoundShape(e):
"""Called when the viewpoint enters the proximity area of a shape"""
# Remove shape sensor from proximity manager
e.manager.removeSensor(e.sensor)
# Add fade action to shape
shape = e.sensor.getSourceObject()
shape.runAction(fade)
# Add found position to points
points.addVertex(shape.getPosition())
# Show message when all shapes have been found
if not e.manager.getSensors():
vizinfo.InfoPanel('Good Job! Game over.',align=viz.ALIGN_CENTER,icon=False)
# Create proximity manager
manager = vizproximity.Manager()
#Use main viewpoint as proximity target
manager.addTarget(vizproximity.Target(viz.MainView))
# Add a spherical proximity sensor for each shape
sphere = vizproximity.Sphere(2.0)
for s in shapes:
sensor = vizproximity.Sensor(shape=sphere, source=s)
manager.addSensor(sensor)
manager.onEnter(sensor,FoundShape)
Run the code now and see what happens when you get near a shape.