A couple of unsigned integers to store row and column numbers where to place the fruit. in .NET Drawer Code 128 Code Set C in .NET A couple of unsigned integers to store row and column numbers where to place the fruit.

A couple of unsigned integers to store row and column numbers where to place the fruit. using visual .net todraw code 128b on web,windows application EAN-13 var point_to_watch:Point;. A Point variable c alled point_to_watch. Point represents a location in a two-dimensional coordinate system and its constructor is Point(x,y) where x represents the horizontal axis and y represents the vertical axis..

var children:Array;. Simply creating a new array. It"s called children because it will contain all DisplayObjects that lie under a given point..

while (!placed) { ...

}. This while loop re peats the code until placed variable becomes true, which means we successfully placed the fruit. The concept is similar to the one we used to place mines during the creation of Minesweeper game. We keep on trying to place fruits, or mines, until we randomly choose a legal position.

. col=Math.floor(Mat barcode 128 for .NET h.

random()*FIELD_WIDTH)*TILE_SIZE; row=Math.floor(Math.random()*FIELD_HEIGHT)*TILE_SIZE;.

[ 135 ]. Snake Generating the can didate row and column where the fruit is to be placed. Notice I multiplied the result by TILE_SIZE because I am not working with arrays so I need to know the pixel where to place the fruit rather than the position in an array..

point_to_watch=new VS .NET USS Code 128 Point(col+TILE_SIZE/2,row+TILE_SIZE/2);. Constructing point _to_watch variable and assigning it the coordinate of the center of the hypothetical tile in the row-th row and the col-th column. A hypothetical tile is a square whose sides are TILE_SIZE long, so you will find its center adding TILE_SIZE/2 to the coordinates of its upper-left point..

children=stage.get USS Code 128 for .NET ObjectsUnderPoint(point_to_watch);.

This is the core o f the function. getObjectsUnderPoint method returns an array of objects that lie under the specified point and are children (or children of children, and so on) of the DisplayObjects Container which invoked the method..

if (children.lengt .NET Code128 h<2&&manhattan_dist(the_head.

x,col,the_head. y,row)>60) { ..

. }. This if statement checks the length of children array to be less than 2. If there is only one DisplayObjects under point_to_watch, it must be the ground, so the tile is free. Also, the if checks for the Manhattan distance to be greater than 60 pixels.

If both conditions are true, then this line is executed:. placed=true;. this means we foun d a legal position where to place the fruit, so we set placed to true to exit the while loop. The remaining lines:. fruit =new fruit_m .net framework Code128 c(); fruit.x=col; fruit.

y=row; addChild(fruit);"fruit";. just construct and add the fruit to Display List, placing it in the chosen position. Also, the fruit has a name, fruit. This will help us later.

. [ 136 ]. 4 . To work with Point Code 128B for .NET variables, we need to import a new class, so add it:. import import impo rt import flash.display.Sprite; flash.


KeyboardEvent; flash.geom.Point;.

Test the game, and a juicy fruit will appear on the stage. Now the snake has a reason to live. Eating fruits Once the fruit is placed, eating is easy, let"s say a piece of cake. A fruit cake, of course. The idea: In the same way we checked for an empty tile to place the fruit in, we"ll check for the tile occupied by the snake"s head looking for a fruit.

If we find a fruit, we have to remove it and place a new one elsewhere. The development: Once the head has moved, we have to retrieve its middle point and see if there"s a fruit under such point. Before the end of onEnterFr function, add this code:.

var point_to_watch code128b for .NET :Point=new Point(the_head.x+TILE_SIZE/2,the_head.

y+TILE_SIZE/2); var children_in_that_point:Array=stage.getObjectsUnderPoint(point_to_ watch); for (var i:uint=0; i<children_in_that_point.length; i++) { switch (children_in_that_point[i]. { case "fruit" : removeChild(fruit); placeStuff(); break; } }. [ 137 ]. Snake Test the movie and eat a fruit: it will disappear and a new fruit will appear in another location. In the picture, th Visual Studio .NET USS Code 128 e snake eats a fruit and suddenly a new one is generated in a random position (with the principles explained before). Let"s see how it works:.

var point_to_watch :Point=new Point(the_head.x+TILE_SIZE/2,the_head. y+TILE_SIZE/2);.

Creates a Point variable with the coordinates of the center of the snake"s head. var children_in_th at_point:Array=stage.getObjectsUnderPoint(point_to_ watch);. Retrieves the children under such point with getObjectsUnderPoint method. for (var i:uint=0; i<children_in_that_point.length; i++) { ..

. }. This for loop scan Code 128 for .NET s through the array filled with DisplayObjects that lie under point_to_watch point..

switch (children_i n_that_point[i] { .

.. }.

The switch stateme visual .net code-128c nt checks the name of the i-th DisplayObject in children"s array. Notice I had to write:.

Copyright © . All rights reserved.