Comparison Operators!
Goals:
- Use comparison operators to compare numeric values and make your program react to its current state
Coding Snippets:
- Compare numeric values using the comparison operators
>,>=,<,<=,==, and!=. The result of each of these operations will be a Boolean value which can be used as input in an if statement. distancereturns the distance from a sprite to a coordinate or to another sprite.t.direction()returns spritet's current bearing (e.g.,0for "north",90for "east").
Math and Computer Concepts:
- As in most coding languages, CoffeeScript uses
=as an assignment operator. Hencex=5assigns (i.e., sets) the variablexto5. - The comparison operator
==is used to test for equality.x==5tests whether the value referenced by the variablexequals5. - The operator
!=tests for inequality.x!=5evaluates totruewhenxis not assigned the value5. distanceanddirectionreturn information about the current state of animation. Precede calls to these functions withawait done defer()to ensure that they are evaluated in the right place.
Activities:
Save each of the following in a folder called ComparisonOps.
- Draw the largest dot that can fit in the visible screen, regardless of the size of your browser window, creating a PerfectFit.
- InvisibleRoundFence: confine one or more sprites to a confined circular area, but without showing a fence. Draw a dot every time a sprite reaches the boundary.
- InvisibleRectangularFence: Repeat the previous activity, but this time confine sprites to a rectangular region.
-
RaceWithEnd: Make a copy of your RaceWithEnd program from Conditional Logic! and modify it so that it works without calls to touches. Instead, track how for each sprite moves and use that information to determine when one reaches the finish line first.
Swarm: Create an array of many "bees" (perhaps simply yellow turtles) that move around a queen bee using conditional logic. If they are close to the queen, they move randomly. If they are beyond a certain distance from the queen, then they turn back towards her.
