#Region Project Attributes
#ApplicationLabel: Reaction
#VersionCode: 1
#VersionName:
#LibraryAuthor: RandomCoder
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
Private pulseTimer As Timer
Private colourList As List
Private targetColour As List
Private colouredSpots As Map
Type spotDetail(x1 As Int, y1 As Int, size As Int, colour As Int)
Private x1, y1, x2, y2 As Int ' x1,y1 = top left, x2,y2 = bottom right
Private level As Int
End Sub
Sub Globals
Private gameBoard As Canvas
Private gameLabel As Label
Private touchCount As Int
End Sub
Sub Activity_Create(FirstTime As Boolean)
initialise
If FirstTime Then
level = 0
End If
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Private Sub Activity_Touch (Action As Int, X As Float, Y As Float)
' This event if triggered several times, were only interested in the first touch
If Action = Activity.ACTION_DOWN Then
' Counter only added to enable touches to be recorded in gamelabel
touchCount = touchCount + 1
gameLabel.Text = touchCount
' Change label colour to show what colour was touched
gameLabel.Color = gameBoard.Bitmap.GetPixel(X, Y)
End If
End Sub
Private Sub initialise()
' Add canvas to avtivity
gameBoard.Initialize(Activity)
gameLabel.Initialize("gameLabel")
gameLabel.Gravity = Gravity.CENTER
gameLabel.Color = Colors.White
Activity.AddView(gameLabel, 0, Activity.Height - 30dip, 100%x, 30dip)
' Set game board limits
x1 = 50dip: y1 = 50dip
x2 = Activity.Width - 100dip ' 50dip spacing on both sides (subtract 100dip from activity width)
y2 = 60%y
' Load colour list
SetColours
' Initialise and display spots
InitialiseSpots
RedrawSpots
' Start timing
pulseTimer.Initialize("pulseTimer", 3000)
pulseTimer.Enabled = True
touchCount = 0
End Sub
Private Sub SetColours()
' Add more colours if required
colourList.Initialize
colourList.Add(Colors.Blue)
colourList.Add(Colors.Cyan)
colourList.Add(Colors.Green)
colourList.Add(Colors.LightGray)
colourList.Add(Colors.Magenta)
colourList.Add(Colors.Red)
colourList.Add(Colors.White)
colourList.Add(Colors.Yellow)
' Initialise and set target colour
targetColour.Initialize
setTarget(0)
End Sub
Private Sub setTarget(num As Int)
' Randomly select a new target colour
Dim newColour As Int
newColour = GetRandomColour
' Make sure that target colour is not already in use
Do Until targetColour.IndexOf(newColour) = -1
newColour = GetRandomColour
Loop
' If num is greater than list size add new target otherwise change existing target colour
If num > (targetColour.Size - 1) Then
targetColour.add(newColour)
Else
targetColour.Set(num, newColour)
End If
End Sub
Private Sub GetRandomColour() As Int
' Randomly select a colour from the list of colours
Return colourList.get(Rnd(0,colourList.Size))
End Sub
Private Sub InitialiseSpots()
' Each spot to have a fixed position in gameboard ### You could randomize if wanted?
Dim spacingX As Int = (x2 - x1) / 5
Dim spacingY As Int = (y2 - y1) / 5
' Populate the colouredSpots map with the location and colour of all 36 spots (6 x 6 grid)
colouredSpots.Initialize
Dim row, col, spotNumber As Int = 0
For row = 0 To 5
For col = 0 To 5
' Increment spot number and assign its position and random colour
spotNumber = spotNumber + 1
Dim detail As spotDetail
detail.size = 20dip
detail.x1 = x1 + (spacingX * col) + detail.size ' Offset by detail.size to centralise the grid
detail.y1 = y1 + (spacingY * row) + ((spacingY/2) * (col Mod 2)) ' Offset alternate columns
detail.colour = GetRandomColour
' Store details in the map
colouredSpots.Put(spotNumber,detail)
Next
Next
End Sub
Private Sub ChangeSpotColours()
' Randomly select a new colour for every spot
Dim i As Int
For i = 1 To colouredSpots.Size
Dim detail As spotDetail
detail = colouredSpots.get(i)
detail.colour = GetRandomColour
colouredSpots.Put(i, detail)
Next
' Randomly select a new colour for every target
For i = 0 To targetColour.Size - 1
setTarget(i)
Next
End Sub
Private Sub RedrawSpots()
' Update the gameBoard with the new spot colours
Dim i As Int
For i = 1 To colouredSpots.Size
Dim detail As spotDetail
detail = colouredSpots.get(i)
gameBoard.DrawCircle(detail.x1,detail.y1,detail.size,detail.colour,True,0)
Next
' Update the gameBoard with the new target colour(s) NB. New targets will be added as target list grows
For i = 0 To targetColour.Size - 1
gameBoard.DrawCircle(50dip + (60dip * i),Activity.Height - (gameLabel.Height + 50dip),25dip,targetColour.Get(i),True,0)
Next
' Refresh the display
Activity.Invalidate
End Sub
Private Sub pulseTimer_Tick()
' You can chose how the level system works
level = level + 1
' This will add another target every turn up to a maximum of 5 targets (0 to 4 list indices)
If targetColour.Size <= 4 Then
setTarget(targetColour.Size)
End If
' Update gameBoard with new coloured spots
ChangeSpotColours
RedrawSpots
End Sub