Home Lessons Apps Support About Consultations We create games Contact
Home Lessons Apps Support About Consultations We create games Facebook

Lesson 14: Ball in a labyrinth.

Select the device which you develop the application for:
 

The project contains:
- the algorithm of ball movement (considering collisions);
- a character that displays arrows of the movement trajectory;
- character Cat, with animation of eight changing pictures.

Start Game Pilot, select "Prepare demo project", then select the "14 Ball in a labyrinth" project. The project will load to your device into the Documents/Lesson014 folder.

The Lesson014.sc file describes the project scenario:

 

- - - - - CODE BEGIN - - - - -

Scene Start
Orientation(Landscape)
AutoRotate(0) // Lock screen rotation

Maska   = 2   // The number of the current mask
LLetters= 150*TypeDevice // The size of the letters
HLetters= 188*TypeDevice
YLetters= 160*TypeDevice
XLetters= 155*TypeDevice
YLetter =  67*TypeDevice
XLetterC= ScreenCX-XLetters-77*TypeDevice  
XLetterA= ScreenCX         -77*TypeDevice
XLetterT= ScreenCX+XLetters-77*TypeDevice

Png(Background,,,ScreenX,ScreenY)  

// Shaded letters
Png(letter001, ScreenCX-XLetters, YLetters, LLetters, HLetters)
Png(letter002, ScreenCX         , YLetters, LLetters, HLetters)
Png(letter003, ScreenCX+XLetters, YLetters, LLetters, HLetters)

Person(way, 1, 0, 0) // The path of the ball
// Person( ball, 1 ) // The way the character causes the character ball 
Person(cat, 0)       // The Cat Character
// Image for collision detection
PixelMask(ball000, 0, 0)
PixelMask(C, ScreenCX-XLetters- XLetterC, YLetters-YLetter)   // С-2
PixelMask(A, ScreenCX         - XLetterA, YLetters-YLetter)   // A-3
PixelMask(T, ScreenCX+XLetters- XLetterT, YLetters-YLetter)   // T-4

- - - - - CODE END - - - - -

 

Collision control is performed for the images uploaded to the scene with the PixelMask command. For collisions control, an image must not be scaled; so you should make different images for devices with different resolutions: 4x for iPad Retina; -ipad for iPad; -1x for Retina iPhone and -iphone for iPhone.

The algorithm of the ball movement is contained in the ball.stt file:

 

- - - - - CODE BEGIN - - - - -

// The Character ball
    Slot[Time0_ball] = 2 // The position of the ball calculated by the timer 
  Rest

1: Img=0    Z=20     // Initialization
   SpeedX=0 SpeedY=0 // Speed in X and Y
   Timer[0]=100      // Interval control the movement of the Ball 100 ms
  Rest

2:SpeedX = SpeedX+0.2*TiltX-0.05*SpeedX  // Determine the angle of inclination by accelerometer
  SpeedY = SpeedY+0.2*TiltY-0.05*SpeedY  // And computed components of the velocity vector along the axes
  Period = 100   Stop

3:If Period < 10 Rest
  Send=MoveBall
  LX= SpeedX*Period/100  
  LY= SpeedY*Period/100
  N = Trunc(Max(Abs(LX),Abs(LY)))+1
  I = 1 to N if Collision(1,Maska, X+I*LX/N, Y+I*LY/N) {
   AngleSpeed= AngleXY (SpeedX, SpeedY) // Determine the angle of the velocity vector 
   Speed     = Distance(SpeedX, SpeedY) // Define the module of the vector speed
   A=AngleRange(AngleSpeed-AngleCollision)
   B=Abs(A)
   if B>90 { // The ball is rolled from the rim
         Delay = Period*I/N   dX=I*LX/N dY=I*LY/N
         Delay = 0            Period=Period*(N-I)/N
         State = 3
      }
   if Speed*Cos(AngleCollision-AngleSpeed)>22 { // A heavy blow on the side
      // Determine the rebound angle from the side
      Rebound = 180 - AngleSpeed + 2*AngleCollision
      Speed   = 0.5*Speed*(1+Sin(B)) // 0.5 The repayment factor of the rebound speed
      SpeedX  = Speed*Cos( Rebound)
      SpeedY  = Speed*Sin(-Rebound)
      MP3     = 1
      Delay   = Period*I/N   dX=I*LX/N dY=I*LY/N
      Delay   = 0            Period=Period*(N-I)/N
      If Speed<1 Rest
      State   = 3
      }
   // The ball is rolling along the rim
   d_X = I*LX/N -Cos(AngleCollision)
   d_Y = I*LY/N +Sin(AngleCollision)
   Rebound = AngleCollision + Sign(A)*90 // The angle of rebound from the side
   Speed   = 0.9*Speed*Sin(B)       // 0.9 - The braking on the rim
   If Speed<3 And i=1 {
      dX=d_X  dY=d_Y  SpeedX=0  SpeedY=0
      if Collision(1,Maska, X, Y) { // Push from the second side
         dX = -Cos(AngleCollision)
         dY =  Sin(AngleCollision)
      }
      Rest }
   Delay   = Period*I/N   dX=d_X  dY=d_Y
   Delay   = 0
   Period  = Period*(N-I)/N
   SpeedX  = Speed*Cos( Rebound)
   SpeedY  = Speed*Sin(-Rebound)
   State   = 3
   }
   Delay=Period  dX=LX  dY=LY  Delay=0
 Rest

- - - - - CODE END - - - - -

 

The algorithm of the ball movement is called according to the timer with the 100ms period. The components of the speed vector along the axes of coordinates SpeedX and SpeedY are calculated from the previous value and the angle of inclination by the accelerometer. The speed value determines the line segment for moving the ball, for each 100 ms period. The line segment is divided into N equal parts by intermediate points of movement. For each point of movement in a cycle, we check the collision of the ball with the wall using the function Collision(). If there are no collisions, then the ball can move freely along the line segment, otherwise the value of the collision angle is assigned to the AngleCollision variable. The direction of rebound of the ball from the wall is calculated based on the moving direction and the collision angle.

The ball movement trajectories are contained in the file way.stt:

 

- - - - - CODE BEGIN - - - - -

// The character way to work with path
  Slot[Way]=1
  Rest

1: Delay=0
  Nomer =10*TypeDevice      // The number of path 
  Index =0
  Person[1]=ball State[1]=1 // The character ball
  State=Nomer

2: // MoveBall Changing the color of the trajectory when moving the ball
   i = Index+1
   If Distance(X[1]-X[i], Y[1]-Y[i])<20 Index=i
   Color[Index]=ColorGreen
   if Index<Count Rest
   Slot[MoveBall]
   I=2 To Count Color[I]=ColorBlue
   Nomer = Nomer + 1
   State = Nomer    // Moving on to the next path

3: // Initialization of the path
  X[1]=X[Index]    Y[1]=Y[Index]  SpeedX[1]=0 SpeedY[1]=0
   // The conclusion of the arrows path at a certain angle
  I=Index To Count-1 { Img[i]=0 oX[i]=0 Angle[i]=AngleXY(X[i+1]-X[i],Y[i+1]-Y[i]) }
  Img[Count]=0  Angle[Count]   =Angle[Count-1] oX[Count]=0
  Index=Index-1 Slot [MoveBall]=2
  State=2

10: Maska = 2    // The path of the letter C
   X = XLetterC  Y = YLetter
   Parent[ 2]=0 X[ 2]=129 Y[ 2]=158
   Parent[ 3]=0 X[ 3]=108 Y[ 3]=166
   Parent[ 4]=0 X[ 4]= 86 Y[ 4]=167
   Parent[ 5]=0 X[ 5]= 65 Y[ 5]=162
   Parent[ 6]=0 X[ 6]= 46 Y[ 6]=152
   Parent[ 7]=0 X[ 7]= 31 Y[ 7]=135
   Parent[ 8]=0 X[ 8]= 22 Y[ 8]=114
   Parent[ 9]=0 X[ 9]= 20 Y[ 9]= 93
   Parent[10]=0 X[10]= 22 Y[10]= 71
   Parent[11]=0 X[11]= 31 Y[11]= 50
   Parent[12]=0 X[12]= 45 Y[12]= 33
   Parent[13]=0 X[13]= 64 Y[13]= 22
   Parent[14]=0 X[14]= 87 Y[14]= 19
   Parent[15]=0 X[15]=111 Y[15]= 20
   Parent[16]=0 X[16]=133 Y[16]= 29
   Index=2 State=3

11:I=Count Downto 2 Destroy[i]  // Remove the path
  Maska = 3     // The path of the letter A
  X = XLetterA  Y = YLetter
  Parent[ 2]=0 X[ 2]= 18 Y[ 2]= 12
  Parent[ 3]=0 X[ 3]= 23 Y[ 3]= 33
  Parent[ 4]=0 X[ 4]= 27 Y[ 4]= 54
  Parent[ 5]=0 X[ 5]= 33 Y[ 5]= 73
  Parent[ 6]=0 X[ 6]= 38 Y[ 6]= 97
  Parent[ 7]=0 X[ 7]= 44 Y[ 7]=118
  Parent[ 8]=0 X[ 8]= 51 Y[ 8]=140
  Parent[ 9]=0 X[ 9]= 63 Y[ 9]=161
  Parent[10]=0 X[10]= 82 Y[10]=161
  Parent[11]=0 X[11]= 94 Y[11]=142
  Parent[12]=0 X[12]=103 Y[12]=120
  Parent[13]=0 X[13]=110 Y[13]= 99
  Parent[14]=0 X[14]=116 Y[14]= 75
  Parent[15]=0 X[15]=121 Y[15]= 55
  Parent[16]=0 X[16]=126 Y[16]= 35
  Parent[17]=0 X[17]=129 Y[17]= 17
  Index=2 State=3

12: // Horizontal stick letters A
  Parent[18]=0 X[18]= 50 Y[18]= 84
  Parent[19]=0 X[19]= 75 Y[19]= 84
  Parent[20]=0 X[20]=100 Y[20]= 84
  Index=18 State=3

13: I=Count Downto 2 Destroy[i]
  Maska = 4     // The path of the letter T
  X = XLetterT  Y = YLetter
  Parent[ 2]=0 X[ 2]= 72 Y[ 2]=153
  Parent[ 3]=0 X[ 3]= 72 Y[ 3]=132
  Parent[ 4]=0 X[ 4]= 72 Y[ 4]=109
  Parent[ 5]=0 X[ 5]= 72 Y[ 5]= 84
  Parent[ 6]=0 X[ 6]= 72 Y[ 6]= 60
  Parent[ 7]=0 X[ 7]= 72 Y[ 7]= 38
  Parent[ 8]=0 X[ 8]= 72 Y[ 8]= 14
  Index=2 State=3

14: // Horizontal stick letters T
  Parent[ 9]=0 X[ 9]= 11 Y[ 9]=162
  Parent[10]=0 X[10]= 35 Y[10]=162
  Parent[11]=0 X[11]= 57 Y[11]=162
  Parent[12]=0 X[12]= 81 Y[12]=162
  Parent[13]=0 X[13]=105 Y[13]=162
  Parent[14]=0 X[14]=128 Y[14]=162
  Index=9 State=3

15:I=Count Downto 1 Destroy[i]  // Remove the path
  Send=CatMove
  Rest

20: Maska = 2    // The path of the letter C in iPad (*2)  
   X = XLetterC  Y = YLetter            
   Parent[ 2]=0 X[ 2]=258 Y[ 2]=316
   Parent[ 3]=0 X[ 3]=216 Y[ 3]=332
   Parent[ 4]=0 X[ 4]=172 Y[ 4]=334
   Parent[ 5]=0 X[ 5]=130 Y[ 5]=324
   Parent[ 6]=0 X[ 6]= 92 Y[ 6]=304
   Parent[ 7]=0 X[ 7]= 62 Y[ 7]=270
   Parent[ 8]=0 X[ 8]= 44 Y[ 8]=228
   Parent[ 9]=0 X[ 9]= 40 Y[ 9]=186
   Parent[10]=0 X[10]= 44 Y[10]=142
   Parent[11]=0 X[11]= 62 Y[11]=100
   Parent[12]=0 X[12]= 90 Y[12]= 66
   Parent[13]=0 X[13]=128 Y[13]= 44
   Parent[14]=0 X[14]=174 Y[14]= 38
   Parent[15]=0 X[15]=222 Y[15]= 40
   Parent[16]=0 X[16]=266 Y[16]= 58
   Index=2 State=3

21:I=Count Downto 2 Destroy[i]  // Remove the path
  Maska = 3     // The path of the letter A in iPad 
  X = XLetterA  Y = YLetter
  Parent[ 2]=0 X[ 2]= 36 Y[ 2]= 24
  Parent[ 3]=0 X[ 3]= 46 Y[ 3]= 66
  Parent[ 4]=0 X[ 4]= 54 Y[ 4]=108
  Parent[ 5]=0 X[ 5]= 66 Y[ 5]=146
  Parent[ 6]=0 X[ 6]= 76 Y[ 6]=194
  Parent[ 7]=0 X[ 7]= 88 Y[ 7]=236
  Parent[ 8]=0 X[ 8]=102 Y[ 8]=280
  Parent[ 9]=0 X[ 9]=126 Y[ 9]=322
  Parent[10]=0 X[10]=164 Y[10]=322
  Parent[11]=0 X[11]=188 Y[11]=284
  Parent[12]=0 X[12]=206 Y[12]=240
  Parent[13]=0 X[13]=220 Y[13]=198
  Parent[14]=0 X[14]=232 Y[14]=150
  Parent[15]=0 X[15]=242 Y[15]=110
  Parent[16]=0 X[16]=252 Y[16]= 70
  Parent[17]=0 X[17]=258 Y[17]= 34
  Index=2 State=3

22: // Horizontal stick letters А
  Parent[18]=0 X[18]=100 Y[18]=168
  Parent[19]=0 X[19]=150 Y[19]=168
  Parent[20]=0 X[20]=200 Y[20]=168
  Index=18 State=3

23: I=Count Downto 2 Destroy[i]
  Maska = 4     // The path of the letter T in iPad 
  X = XLetterT  Y = YLetter
  Parent[ 2]=0 X[ 2]=144 Y[ 2]=306
  Parent[ 3]=0 X[ 3]=144 Y[ 3]=264
  Parent[ 4]=0 X[ 4]=144 Y[ 4]=218
  Parent[ 5]=0 X[ 5]=144 Y[ 5]=168
  Parent[ 6]=0 X[ 6]=144 Y[ 6]=120
  Parent[ 7]=0 X[ 7]=144 Y[ 7]= 76
  Parent[ 8]=0 X[ 8]=144 Y[ 8]= 28
  Index=2 State=3

24: // Horizontal stick letters T
  Parent[ 9]=0 X[ 9]= 22 Y[ 9]=324
  Parent[10]=0 X[10]= 70 Y[10]=324
  Parent[11]=0 X[11]=114 Y[11]=324
  Parent[12]=0 X[12]=162 Y[12]=324
  Parent[13]=0 X[13]=210 Y[13]=324
  Parent[14]=0 X[14]=256 Y[14]=324
  Index=9 State=3

25:I=Count Downto 1 Destroy[i]  // Remove the path
  Send=CatMove
  Rest

- - - - - CODE END - - - - -

 

Character way creates subordinate character ball as well as several child sprites with the arrow image for showing the path. The rotation angle of each arrow of the path is determined by the function AngleXY() so that the arrow points to the next sprite.

The number of the current path arrow is stored in the Index variable.

As the ball moves, the distance from it to the current arrow is determined by the Distance() function. If the ball is next to the current arrow, then the color of the arrow changes from white to green.

After the ball moved successfully through the maze, the child sprites and the subordinate character ball are be deleted, and the CatMove message is sent.

File cat.stt contains the description of character cat:

 

- - - - - CODE BEGIN - - - - -

// The Character Cat 
 Scale = 50
 Slot[CatMove]=2
 Rest

// The cat went
2: X=ScreenX Y=100 Opacity=100
   Step  = 1

// The gait of the cat is formed by the change of pictures 1..8
3: Img=Step Delay=200 dX=-15 
   Delay=0  Step=Step+1  if Step>8 Step=1
   If X>0 State=3
// The cat has gone
 Opacity=0
 Send   =Way 
Rest 

- - - - - CODE END - - - - -

 

The CatMove message calls state 2 and the movement begins. For animation of the Cat character, eight pictures are used. Each picture shows different position of the legs: cat001.png .. cat008.png. In state 3, the pictures changing order is set up, and the character moves smoothly to the left.

When the character reaches the left screen border, it disappears and sends the Way message for "Ball in a labyrinth" algorithm.
 

- Download the archive with project files for this lesson -


Privacy Policy    Terms of Use     Copyright © Rais Garifullin